Skip to content

Commit

Permalink
Merge pull request #2 from jmcriffey/pwa
Browse files Browse the repository at this point in the history
Unify the configs for manifest and runtime caching.
  • Loading branch information
aweary authored Nov 15, 2016
2 parents cd8b236 + 92e02dd commit 42d28a0
Showing 1 changed file with 36 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,46 +1,59 @@
"use strict";
var path = require('path');
var path = require("path");
var assign = require("lodash/assign");
var archetype = require("../../archetype");
var mergeWebpackConfig = archetype.devRequire("webpack-partial").default;
var fileLoader = archetype.devRequire.resolve("file-loader");
var webAppManifestLoader = require.resolve("web-app-manifest-loader");
var SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin');
var FaviconsWebpackPlugin = require('favicons-webpack-plugin');
var SWPrecacheWebpackPlugin = require("sw-precache-webpack-plugin");
var FaviconsWebpackPlugin = require("favicons-webpack-plugin");


var runtimeCachePath = path.resolve(process.cwd(), 'client/pwa-runtime-cache.json');
var swConfigPath = path.resolve(process.cwd(), "config/sw-config.js");

function getRuntimeCacheJSON() {
var runtimeCacheJSON;
function getSWConfig() {
var swConfig;

try {
runtimeCacheJSON = require(runtimeCachePath);
swConfig = require(swConfigPath);
} catch(err) {
runtimeCacheJSON = [];
swConfig = {};
}

return runtimeCacheJSON;
return swConfig;
}

module.exports = function () {
return function (config) {
var runtimeCacheJSON = getRuntimeCacheJSON();
var swConfig = getSWConfig();

if (!swConfig.manifest) {
return mergeWebpackConfig(config, {});
}

var manifestConfig = assign({
background: "#FFFFFF",
logo: "./images/electrode.png",
title: "Electrode",
statsFilename: "../server/iconstats.json"
}, swConfig.manifest);
var cacheConfig = assign({}, swConfig.cache);
var precacheConfig = {
staticFileGlobs: [
'dist/js/*.{js,css,png,jpg,svg}',
'dist/js/icons**/*.png'
"dist/js/*.{js,css,png,jpg,svg}",
"dist/js/icons**/*.png"
],
stripPrefix: 'dist',
cacheId: 'electrode',
filepath: 'dist/sw.js',
stripPrefix: "dist",
cacheId: "electrode",
filepath: "dist/sw.js",
maximumFileSizeToCacheInBytes: 4194304
};

if (runtimeCacheJSON && runtimeCacheJSON.length) {
precacheConfig.runtimeCaching = runtimeCacheJSON.map(function(runtimeCache) {
if (cacheConfig.runtimeCaching) {
precacheConfig.runtimeCaching = cacheConfig.runtimeCaching.map(function(runtimeCache) {
return {
handler: runtimeCache.handler,
urlPattern: new RegExp(runtimeCache.urlPattern)
urlPattern: runtimeCache.urlPattern
}
});
}
Expand All @@ -50,18 +63,18 @@ module.exports = function () {
loaders: [
{
test: /manifest.json$/,
loader: fileLoader + '?name=manifest.json!' + webAppManifestLoader
loader: fileLoader + "?name=manifest.json!" + webAppManifestLoader
}
]
},
plugins: [
new FaviconsWebpackPlugin({
logo: './images/electrode.png',
logo: manifestConfig.logo,
emitStats: true,
inject: false,
background: '#FFFFFF',
title: 'Electrode',
statsFilename: '../server/iconstats.json',
background: manifestConfig.background,
title: manifestConfig.title,
statsFilename: manifestConfig.statsFilename,
icons: {
android: true,
appleIcon: true,
Expand Down

0 comments on commit 42d28a0

Please sign in to comment.