-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy pathindex.js
More file actions
731 lines (565 loc) · 22.7 KB
/
index.js
File metadata and controls
731 lines (565 loc) · 22.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
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; }; })();
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var _appCache = require('./app-cache');
var _appCache2 = _interopRequireDefault(_appCache);
var _serviceWorker = require('./service-worker');
var _serviceWorker2 = _interopRequireDefault(_serviceWorker);
var _defaultOptions = require('./default-options');
var _defaultOptions2 = _interopRequireDefault(_defaultOptions);
var _miscUtils = require('./misc/utils');
var _path2 = require('path');
var _path3 = _interopRequireDefault(_path2);
var _url = require('url');
var _url2 = _interopRequireDefault(_url);
var _deepExtend = require('deep-extend');
var _deepExtend2 = _interopRequireDefault(_deepExtend);
var _minimatch = require('minimatch');
var _minimatch2 = _interopRequireDefault(_minimatch);
var _loaderUtils = require('loader-utils');
var _loaderUtils2 = _interopRequireDefault(_loaderUtils);
var _slash = require('slash');
var _slash2 = _interopRequireDefault(_slash);
var _require = require('../package.json');
var pluginVersion = _require.version;
var hasOwn = ({}).hasOwnProperty;
var updateStrategies = ['all', 'hash', 'changed'];
var OfflinePlugin = (function () {
function OfflinePlugin(options) {
var _this = this;
_classCallCheck(this, OfflinePlugin);
var AppCacheOptions = options ? options.AppCache : void 0;
this.options = (0, _deepExtend2['default'])({}, _defaultOptions2['default'], options, {
AppCache: false
});
this.hash = null;
this.assets = null;
this.hashesMap = null;
this.externals = null;
this.publicPath = this.options.publicPath;
this.strategy = this.options.updateStrategy;
this.responseStrategy = this.options.responseStrategy;
this.relativePaths = this.options.relativePaths;
this.pluginVersion = pluginVersion;
this.warnings = [];
this.errors = [];
this.__tests = this.options.__tests;
this.flags = {};
var appCacheEnabled = !!(AppCacheOptions || this.__tests.appCacheEnabled);
if (appCacheEnabled) {
this.options.AppCache = (0, _deepExtend2['default'])({}, _defaultOptions.AppCacheOptions, AppCacheOptions);
}
if (this.__tests.pluginVersion) {
this.pluginVersion = this.__tests.pluginVersion;
}
var autoUpdate = this.options.autoUpdate;
if (autoUpdate === true) {
this.autoUpdate = _defaultOptions.DEFAULT_AUTO_UPDATE_INTERVAL;
} else if (typeof autoUpdate === 'number' && autoUpdate) {
this.autoUpdate = autoUpdate;
}
if (this.options.responseStrategy !== "cache-first" && this.options.responseStrategy !== "network-first") {
throw new Error('OfflinePlugin: `responseStrategy` option must use ' + '`cache-first` or `network-first` (or be undefined).');
}
if (typeof this.publicPath !== 'string') {
this.publicPath = null;
}
if (updateStrategies.indexOf(this.strategy) === -1) {
throw new Error('Update strategy must be one of [' + updateStrategies + ']');
} else if (this.strategy === 'hash') {
this.warnings.push(new Error('OfflinePlugin: `hash` update strategy is deprecated, use `all` strategy and { version: "[hash]" } instead'));
this.strategy = 'all';
this.options.version = '[hash]';
}
if (!Array.isArray(this.options.externals)) {
this.options.externals = [];
}
var rewrites = this.options.rewrites || _defaultOptions2['default'].rewrites;
if (typeof rewrites === 'function') {
this.rewrite = function (asset) {
if (asset.indexOf(_this.entryPrefix) === 0) {
return '';
}
return rewrites(asset);
};
} else {
this.rewrite = function (asset) {
if (asset.indexOf(_this.entryPrefix) === 0) {
return '';
}
if (!hasOwn.call(rewrites, asset)) {
return asset;
}
return rewrites[asset];
};
}
if (this.options.appShell && typeof this.options.appShell === 'string') {
this.appShell = this.options.appShell;
}
var cacheMaps = this.options.cacheMaps;
if (this.appShell) {
// Make appShell the latest in the chain so it could be overridden
cacheMaps = (cacheMaps || []).concat({
match: 'function(url) {\n if (url.pathname === location.pathname) {\n return;\n }\n\n return new URL(' + JSON.stringify(this.appShell) + ', location);\n }',
requestTypes: ['navigate']
});
}
this.cacheMaps = this.stringifyCacheMaps(cacheMaps);
this.REST_KEY = ':rest:';
this.EXTERNALS_KEY = ':externals:';
this.entryPrefix = '__offline_';
this.tools = {};
this.addTool(_serviceWorker2['default'], 'ServiceWorker');
this.addTool(_appCache2['default'], 'AppCache');
if (!Object.keys(this.tools).length) {
throw new Error('You should have at least one cache service to be specified');
}
}
_createClass(OfflinePlugin, [{
key: 'apply',
value: function apply(compiler) {
var _this2 = this;
var runtimePath = _path3['default'].resolve(__dirname, '../runtime.js');
var compilerOptions = compiler.options;
if (this.relativePaths === true) {
this.publicPath = null;
}
if (typeof this.publicPath !== 'string' && compilerOptions && compilerOptions.output && compilerOptions.output.publicPath && this.relativePaths !== true) {
this.publicPath = compilerOptions.output.publicPath;
this.relativePaths = false;
}
if (this.publicPath) {
this.publicPath = this.publicPath.replace(/\/$/, '') + '/';
}
if (this.relativePaths === true && this.publicPath) {
this.errors.push(new Error('OfflinePlugin: `publicPath` is used in conjunction with `relativePaths`,\n' + 'choose one of it'));
this.relativePaths = false;
}
if (this.relativePaths === _defaultOptions2['default'].relativePaths) {
this.relativePaths = !this.publicPath;
}
this.useTools(function (tool, key) {
_this2.resolveToolPaths(tool, key, compiler);
});
var afterResolveFn = function afterResolveFn(result, callback) {
var resource = _path3['default'].resolve(compiler.context, result.resource);
if (resource !== runtimePath) {
callback(null, result);
return;
}
var data = {
autoUpdate: _this2.autoUpdate
};
_this2.useTools(function (tool, key) {
data[key] = tool.getConfig(_this2);
});
result.loaders.push({
loader: _path3['default'].join(__dirname, 'misc/runtime-loader.js'),
options: JSON.stringify(data)
});
callback(null, result);
};
var makeFn = function makeFn(compilation, callback) {
if (_this2.warnings.length) {
[].push.apply(compilation.warnings, _this2.warnings);
}
if (_this2.errors.length) {
[].push.apply(compilation.errors, _this2.errors);
}
_this2.useTools(function (tool) {
return tool.addEntry(_this2, compilation, compiler);
}).then(function () {
callback();
})['catch'](function (e) {
throw e || new Error('Something went wrong');
});
};
var emitFn = function emitFn(compilation, callback) {
var runtimeTemplatePath = _path3['default'].resolve(__dirname, '../tpls/runtime-template.ejs');
var hasRuntime = true;
if (compilation.fileDependencies.indexOf) {
hasRuntime = compilation.fileDependencies.indexOf(runtimeTemplatePath) !== -1;
} else if (compilation.fileDependencies.has) {
hasRuntime = compilation.fileDependencies.has(runtimeTemplatePath);
}
if (!hasRuntime && !_this2.__tests.ignoreRuntime) {
compilation.errors.push(new Error('OfflinePlugin: Plugin\'s runtime wasn\'t added to one of your bundle entries. See this https://goo.gl/YwewYp for details.'));
callback();
return;
}
var stats = compilation.getStats().toJson();
// By some reason errors raised here are not fatal,
// so we need manually try..catch and exit with error
try {
_this2.setAssets(compilation);
_this2.setHashesMap(compilation);
// Generate bundle hash manually (from what we have)
_this2.hash = _loaderUtils2['default'].getHashDigest(Object.keys(_this2.hashesMap).join(''), 'sha1');
// Not used yet
// this.setNetworkOptions();
} catch (e) {
callback(e);
return;
}
_this2.useTools(function (tool) {
return tool.apply(_this2, compilation, compiler);
}).then(function () {
callback();
}, function () {
callback(new Error('Something went wrong'));
});
};
if (compiler.hooks) {
(function () {
var plugin = { name: 'OfflinePlugin' };
compiler.hooks.normalModuleFactory.tap(plugin, function (nmf) {
nmf.hooks.afterResolve.tapAsync(plugin, afterResolveFn);
});
compiler.hooks.make.tapAsync(plugin, makeFn);
compiler.hooks.emit.tapAsync(plugin, emitFn);
})();
} else {
compiler.plugin('normal-module-factory', function (nmf) {
nmf.plugin('after-resolve', afterResolveFn);
});
compiler.plugin('make', makeFn);
compiler.plugin('emit', emitFn);
}
}
}, {
key: 'setAssets',
value: function setAssets(compilation) {
var _this3 = this;
var caches = this.options.caches || _defaultOptions2['default'].caches;
if (this.options.safeToUseOptionalCaches !== true && (caches.additional && caches.additional.length || caches.optional && caches.optional.length)) {
compilation.warnings.push(new Error('OfflinePlugin: Cache sections `additional` and `optional` could be used ' + 'only when each asset passed to it has unique name (e.g. hash or version in it) and ' + 'is permanently available for given URL. If you think that it\'s your case, ' + 'set `safeToUseOptionalCaches` option to `true`, to remove this warning.'));
}
var excludes = this.options.excludes;
var assets = Object.keys(compilation.assets);
var externals = this.options.externals;
if (Array.isArray(excludes) && excludes.length) {
assets = assets.filter(function (asset) {
if (excludes.some(function (glob) {
if ((0, _minimatch2['default'])(asset, glob)) {
return true;
}
})) {
return false;
}
return true;
});
}
this.externals = this.validatePaths(externals);
if (caches === 'all') {
this.assets = this.validatePaths(assets).concat(this.externals);
this.caches = {
main: this.assets.concat()
};
} else {
(function () {
var restSection = undefined;
var externalsSection = undefined;
var handledCaches = ['main', 'additional', 'optional'].reduce(function (result, key) {
var cache = Array.isArray(caches[key]) ? caches[key] : [];
if (!cache.length) {
result[key] = cache;
return result;
}
var cacheResult = [];
cache.some(function (cacheKey) {
if (cacheKey === _this3.REST_KEY) {
if (restSection) {
throw new Error('The ' + _this3.REST_KEY + ' keyword can be used only once');
}
restSection = key;
return;
}
if (cacheKey === _this3.EXTERNALS_KEY) {
if (externalsSection) {
throw new Error('The ' + _this3.EXTERNALS_KEY + ' keyword can be used only once');
}
externalsSection = key;
return;
}
var magic = undefined;
if (typeof cacheKey === 'string') {
magic = !(0, _miscUtils.isAbsoluteURL)(cacheKey) && cacheKey[0] !== '/' && cacheKey.indexOf('./') !== 0 && (0, _miscUtils.hasMagic)(cacheKey);
} else if (cacheKey instanceof RegExp) {
magic = (0, _miscUtils.hasMagic)(cacheKey);
} else {
// Ignore non-string and non-RegExp keys
return;
}
if (magic) {
var matched = undefined;
for (var i = 0, len = assets.length; i < len; i++) {
if (!magic.match(assets[i])) continue;
matched = true;
cacheResult.push(assets[i]);
assets.splice(i, 1);
i--, len--;
}
if (!matched) {
compilation.warnings.push(new Error('OfflinePlugin: Cache pattern [' + cacheKey + '] did not match any assets'));
}
return;
}
var index = assets.indexOf(cacheKey);
__EXTERNALS_CHECK: if (index === -1) {
var externalsIndex = externals.indexOf(cacheKey);
if (externalsIndex !== -1) {
externals.splice(externalsIndex, 1);
break __EXTERNALS_CHECK;
}
compilation.warnings.push(new Error('OfflinePlugin: Cache asset [' + cacheKey + '] is not found in the output assets, ' + 'if the asset is not processed by webpack, move it to the |externals| option to remove this warning.'));
} else {
assets.splice(index, 1);
}
cacheResult.push(cacheKey);
});
result[key] = _this3.validatePaths(cacheResult);
return result;
}, {});
if (restSection && assets.length) {
handledCaches[restSection] = handledCaches[restSection].concat(_this3.validatePaths(assets));
}
if (externalsSection && externals.length) {
handledCaches[externalsSection] = handledCaches[externalsSection].concat(_this3.validatePaths(externals));
}
_this3.caches = handledCaches;
_this3.assets = [].concat(_this3.caches.main, _this3.caches.additional, _this3.caches.optional);
})();
}
}
}, {
key: 'setHashesMap',
value: function setHashesMap(compilation) {
var _this4 = this;
this.hashesMap = {};
Object.keys(compilation.assets).forEach(function (key) {
var validatedPath = _this4.validatePaths([key])[0];
if (typeof validatedPath !== 'string' || _this4.assets.indexOf(validatedPath) === -1) return;
var hash = _loaderUtils2['default'].getHashDigest(compilation.assets[key].source(), 'sha1');
_this4.hashesMap[hash] = validatedPath;
});
}
}, {
key: 'setNetworkOptions',
value: function setNetworkOptions() {
var alwaysRevalidate = this.options.alwaysRevalidate;
var preferOnline = this.options.preferOnline;
var ignoreSearch = this.options.ignoreSearch;
var assets = this.assets;
// Disable temporarily
if (Array.isArray(alwaysRevalidate) && alwaysRevalidate.length) {
alwaysRevalidate = assets.filter(function (asset) {
if (alwaysRevalidate.some(function (glob) {
if ((0, _minimatch2['default'])(asset, glob)) {
return true;
}
})) {
return true;
}
return false;
});
if (alwaysRevalidate.length) {
this.alwaysRevalidate = alwaysRevalidate;
}
}
if (Array.isArray(ignoreSearch) && ignoreSearch.length) {
ignoreSearch = assets.filter(function (asset) {
if (ignoreSearch.some(function (glob) {
if ((0, _minimatch2['default'])(asset, glob)) {
return true;
}
})) {
return true;
}
return false;
});
if (ignoreSearch.length) {
this.ignoreSearch = ignoreSearch;
}
}
if (Array.isArray(preferOnline) && preferOnline.length) {
preferOnline = assets.filter(function (asset) {
if (preferOnline.some(function (glob) {
if ((0, _minimatch2['default'])(asset, glob)) {
return true;
}
})) {
return true;
}
return false;
});
if (preferOnline.length) {
this.preferOnline = preferOnline;
}
}
}
}, {
key: 'stringifyCacheMaps',
value: function stringifyCacheMaps(cacheMaps) {
if (!cacheMaps) {
return [];
}
return cacheMaps.map(function (map) {
if (map.to != null && typeof map.to !== 'string' && typeof map.to !== 'function') {
throw new Error('cacheMaps `to` property must either string, function, undefined or null');
}
if (map.requestTypes != null) {
if (Array.isArray(map.requestTypes)) {
var types = map.requestTypes.filter(function (item) {
if (item === 'navigate' || item === 'same-origin' || item === 'cross-origin') {
return false;
}
return true;
});
if (types.length) {
throw new Error("cacheMaps `requestTypes` array values could be only: 'navigate', 'same-origin' or 'cross-origin'");
}
} else {
throw new Error('cacheMaps `requestTypes` property must either array, undefined or null');
}
}
var to = undefined;
var match = undefined;
if (typeof map.to === 'function') {
to = (0, _miscUtils.functionToString)(map.to);
} else {
to = map.to ? JSON.stringify(map.to) : null;
}
if (typeof map.match === 'function') {
match = (0, _miscUtils.functionToString)(map.match);
} else {
match = map.match + '';
}
return {
match: match,
to: to,
requestTypes: map.requestTypes || null
};
});
}
}, {
key: 'resolveToolPaths',
value: function resolveToolPaths(tool, key, compiler) {
// Tool much implement:
//
// tool.output
// tool.publicPath
// tool.basePath
// tool.location
// tool.pathRewrite
if (!this.relativePaths && !this.publicPath) {
throw new Error('OfflinePlugin: Cannot generate base path for ' + key);
}
var isDirectory = tool.output[tool.output.length - 1] === '/';
if (this.relativePaths) {
var compilerOutput = (compiler.options.output || { path: process.cwd() }).path;
var absoluteOutput = _path3['default'].resolve(compilerOutput, tool.output);
var relativeBase = undefined;
if (isDirectory) {
relativeBase = _path3['default'].relative(absoluteOutput, compilerOutput);
} else {
relativeBase = _path3['default'].relative(_path3['default'].dirname(absoluteOutput), compilerOutput);
}
relativeBase = (0, _slash2['default'])(relativeBase);
relativeBase = relativeBase.replace(/\/$/, '');
if (relativeBase) {
relativeBase = relativeBase + '/';
}
tool.basePath = relativeBase[0] === '.' ? relativeBase : './' + relativeBase;
} else if (this.publicPath) {
tool.basePath = this.publicPath.replace(/\/$/, '') + '/';
}
if (this.relativePaths) {
tool.location = tool.output;
} else if (this.publicPath && tool.publicPath) {
tool.location = tool.publicPath;
} else if (this.publicPath) {
var publicUrl = _url2['default'].parse(this.publicPath);
var publicPath = publicUrl.pathname;
publicUrl.pathname = _path3['default'].join(publicPath, tool.output);
var outerPathname = _path3['default'].join('/outer/', publicPath, tool.output);
if (outerPathname.indexOf('/outer/') !== 0) {
new Error('OfflinePlugin: Wrong ' + key + '.output value. Final ' + key + '.location URL path bounds are outside of publicPath');
}
tool.location = _url2['default'].format(publicUrl);
}
if (this.relativePaths) {
tool.pathRewrite = function (_path) {
if ((0, _miscUtils.isAbsoluteURL)(_path) || _path[0] === '/') {
return _path;
}
return tool.basePath + _path;
};
} else {
tool.pathRewrite = function (path) {
return path;
};
}
}
}, {
key: 'validatePaths',
value: function validatePaths(assets) {
var _this5 = this;
return assets.map(this.rewrite).filter(function (asset) {
return !!asset;
}).map(function (key) {
// If absolute url, use it as is
if ((0, _miscUtils.isAbsoluteURL)(key)) {
return key;
}
if (_this5.relativePaths) {
return key.replace(/^\.\//, '');
}
// Absolute path, use it as is
if (key[0] === '/') {
return key;
}
return _this5.publicPath + key.replace(/^\.?\//, '');
});
}
}, {
key: 'useTools',
value: function useTools(fn) {
var _this6 = this;
var tools = Object.keys(this.tools).map(function (tool) {
return fn(_this6.tools[tool], tool);
});
return Promise.all(tools);
}
}, {
key: 'addTool',
value: function addTool(Tool, name) {
var options = this.options[name];
if (options === null || options === false) {
// tool is not needed
return;
}
this.tools[name] = new Tool(options);
}
}, {
key: 'version',
get: function get() {
var version = this.options.version;
var hash = this.hash;
if (version == null) {
return new Date().toLocaleString();
}
if (typeof version === 'function') {
return version(this);
}
return (0, _miscUtils.interpolateString)(version, { hash: hash });
}
}]);
return OfflinePlugin;
})();
exports['default'] = OfflinePlugin;
OfflinePlugin.defaultOptions = _defaultOptions2['default'];
module.exports = exports['default'];