UNPKG

5.01 kBJavaScriptView Raw
1'use strict';
2
3var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
4
5var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
6
7var _path = require('path');
8
9var _path2 = _interopRequireDefault(_path);
10
11var _del = require('del');
12
13var _del2 = _interopRequireDefault(_del);
14
15var _swPrecache = require('sw-precache');
16
17var _swPrecache2 = _interopRequireDefault(_swPrecache);
18
19function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20
21function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
22
23var DEFAULT_CACHE_ID = 'sw-precache-webpack-plugin',
24 DEFAULT_WORKER_FILENAME = 'service-worker.js',
25 DEFAULT_OUTPUT_PATH = '';
26
27var DEFAULT_OPTIONS = {
28 cacheId: DEFAULT_CACHE_ID,
29 filename: DEFAULT_WORKER_FILENAME
30};
31
32/**
33 * SWPrecacheWebpackPlugin - A wrapper for sw-precache to use with webpack
34 * @param {object} options - All parameters should be passed as a single options object
35 *
36 * // sw-precache options:
37 * @param {string} [options.cacheId]
38 * @param {string} [options.directoryIndex]
39 * @param {object|array} [options.dynamicUrlToDependencies]
40 * @param {boolean} [options.handleFetch]
41 * @param {array} [options.ignoreUrlParametersMatching]
42 * @param {array} [options.importScripts]
43 * @param {function} [options.logger]
44 * @param {number} [options.maximumFileSizeToCacheInBytes]
45 * @param {array} [options.navigateFallbackWhitelist]
46 * @param {string} [options.replacePrefix]
47 * @param {array} [options.runtimeCaching]
48 * @param {array} [options.staticFileGlobs]
49 * @param {string} [options.stripPrefix]
50 * @param {string} [options.templateFilePath]
51 * @param {boolean} [options.verbose]
52 *
53 * // plugin options:
54 * @param {string} [options.filename] - Service worker filename, default is 'service-worker.js'
55 * @param {string} [options.filepath] - Service worker path and name, default is to use webpack.output.path + options.filename
56 */
57
58var SWPrecacheWebpackPlugin = function () {
59 function SWPrecacheWebpackPlugin(options) {
60 _classCallCheck(this, SWPrecacheWebpackPlugin);
61
62 this.options = _extends({}, DEFAULT_OPTIONS, options);
63 }
64
65 _createClass(SWPrecacheWebpackPlugin, [{
66 key: 'apply',
67 value: function apply(compiler) {
68 var _this = this;
69
70 compiler.plugin('done', function (stats) {
71
72 // get the output path specified in webpack config
73 var outputPath = compiler.options.output.path || DEFAULT_OUTPUT_PATH;
74
75 // get all assets outputted by webpack
76 var assetGlobs = getAssetGlobs(stats.compilation).map(function (f) {
77 return _path2.default.join(outputPath, f);
78 });
79
80 var ignorePatterns = _this.options.staticFileGlobsIgnorePatterns || [];
81
82 // filter staticFileGlobs from ignorePatterns
83 var staticFileGlobs = assetGlobs.filter(function (text) {
84 return !ignorePatterns.some(function (regex) {
85 return regex.test(text);
86 });
87 });
88
89 var config = {
90 staticFileGlobs: staticFileGlobs,
91 verbose: true
92 };
93
94 if (outputPath) {
95 // strip the webpack config's output.path
96 config.stripPrefix = outputPath + '/';
97 }
98
99 if (compiler.options.output.publicPath) {
100 // prepend the public path to the resources
101 config.replacePrefix = compiler.options.output.publicPath;
102 }
103
104 _this.writeServiceWorker(compiler, config);
105 });
106 }
107 }, {
108 key: 'writeServiceWorker',
109 value: function writeServiceWorker(compiler, config) {
110 var fileDir = compiler.options.output.path || DEFAULT_OUTPUT_PATH,
111
112 // default to options.filepath for writing service worker location
113 filepath = this.options.filepath || _path2.default.join(fileDir, this.options.filename),
114 workerOptions = _extends({}, config, this.options);
115
116 return (0, _del2.default)(filepath).then(function () {
117 return _swPrecache2.default.write(filepath, workerOptions);
118 });
119 }
120 }]);
121
122 return SWPrecacheWebpackPlugin;
123}();
124
125function getAssetGlobs(compilation) {
126 var assets = [];
127 for (var asset in compilation.assets) {
128 assets.push(asset);
129 }
130 return assets;
131}
132
133module.exports = SWPrecacheWebpackPlugin;
\No newline at end of file