phenomic
Version:
Modern website generator based on the React and Webpack ecosystem.
106 lines (78 loc) • 3.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.offlineEntry = exports.offlinePlugin = undefined;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _path = require("path");
var _globby = require("globby");
var _offlinePlugin = require("offline-plugin");
var _offlinePlugin2 = _interopRequireDefault(_offlinePlugin);
var _urlJoin = require("url-join");
var _urlJoin2 = _interopRequireDefault(_urlJoin);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
var runtimeEntry = (0, _path.resolve)(__dirname, "runtime.js");
var offlinePlugin = exports.offlinePlugin = function offlinePlugin(config) {
if (!config.offline) {
return [];
}
var assets = [];
if (_typeof(config.assets) === "object") {
var configAssets = config.assets;
assets = (0, _globby.sync)(["**/*"], {
cwd: configAssets.path,
nodir: true
}).map(function (asset) {
return (0, _urlJoin2.default)(configAssets.route, asset);
});
}
function preparePatterns(patterns) {
if (!patterns) {
return [];
}
return patterns.reduce(function (acc, pattern) {
return pattern === ":assets:" ? [].concat(_toConsumableArray(acc), _toConsumableArray(assets)) : [].concat(_toConsumableArray(acc), [pattern]);
}, []);
}
return [new _offlinePlugin2.default({
publicPath: config.baseUrl.pathname,
relativePaths: false,
responseStrategy: "network-first",
// Use this option to explicitely cache files not generated by webpack
externals: ["/"].concat(_toConsumableArray(assets)),
// every webpack generated assets have hashes, so we can safely
// inform OfflinePlugin about that
safeToUseOptionalCaches: true,
caches: {
// `main` files will be loaded during SW install
main: preparePatterns(config.offlineConfig.cachePatterns.onInstall),
// `additional` files are loaded after main section
// and do not prevent SW to install.
additional: preparePatterns(config.offlineConfig.cachePatterns.afterInstall),
// `optional` files will be cached only when requested
optional: preparePatterns(config.offlineConfig.cachePatterns.onDemand)
},
// for more advanced usage, see documentation of the plugin
// https://github.com/NekR/offline-plugin/blob/master/docs/caches.md
excludes: config.offlineConfig.cachePatterns.excludes,
ServiceWorker: {
events: true,
navigateFallbackURL: "/"
},
// Appcache Fallback for browser that does not support Service Worker
// (eg: Safari, IE, Edge...)
AppCache: {
events: true,
FALLBACK: {
"/": "/index.html"
}
}
})];
};
var offlineEntry = exports.offlineEntry = function offlineEntry(config) {
if (!config.offline) {
return [];
}
return [runtimeEntry];
};