1 | 'use strict';
|
2 |
|
3 | var _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 |
|
5 | var _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 |
|
7 | var _path = require('path');
|
8 |
|
9 | var _path2 = _interopRequireDefault(_path);
|
10 |
|
11 | var _del = require('del');
|
12 |
|
13 | var _del2 = _interopRequireDefault(_del);
|
14 |
|
15 | var _swPrecache = require('sw-precache');
|
16 |
|
17 | var _swPrecache2 = _interopRequireDefault(_swPrecache);
|
18 |
|
19 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
20 |
|
21 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
22 |
|
23 | var DEFAULT_CACHE_ID = 'sw-precache-webpack-plugin',
|
24 | DEFAULT_WORKER_FILENAME = 'service-worker.js',
|
25 | DEFAULT_OUTPUT_PATH = '';
|
26 |
|
27 | var DEFAULT_OPTIONS = {
|
28 | cacheId: DEFAULT_CACHE_ID,
|
29 | filename: DEFAULT_WORKER_FILENAME
|
30 | };
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | var 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 |
|
73 | var outputPath = compiler.options.output.path || DEFAULT_OUTPUT_PATH;
|
74 |
|
75 |
|
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 |
|
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 |
|
96 | config.stripPrefix = outputPath + '/';
|
97 | }
|
98 |
|
99 | if (compiler.options.output.publicPath) {
|
100 |
|
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 |
|
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 |
|
125 | function getAssetGlobs(compilation) {
|
126 | var assets = [];
|
127 | for (var asset in compilation.assets) {
|
128 | assets.push(asset);
|
129 | }
|
130 | return assets;
|
131 | }
|
132 |
|
133 | module.exports = SWPrecacheWebpackPlugin; |
\ | No newline at end of file |