1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true
|
5 | });
|
6 |
|
7 | 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; }; })();
|
8 |
|
9 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
10 |
|
11 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
12 |
|
13 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
|
14 |
|
15 | var _miscUtils = require('./misc/utils');
|
16 |
|
17 | var _fs = require('fs');
|
18 |
|
19 | var _fs2 = _interopRequireDefault(_fs);
|
20 |
|
21 | var _path = require('path');
|
22 |
|
23 | var _path2 = _interopRequireDefault(_path);
|
24 |
|
25 | var _deepExtend2 = require('deep-extend');
|
26 |
|
27 | var _deepExtend3 = _interopRequireDefault(_deepExtend2);
|
28 |
|
29 | var AppCache = (function () {
|
30 | function AppCache(options) {
|
31 | _classCallCheck(this, AppCache);
|
32 |
|
33 | var output = options.output || options.directory;
|
34 |
|
35 | if ((0, _miscUtils.isAbsolutePath)(output)) {
|
36 | throw new Error('OfflinePlugin: ServiceWorker.output option must be a relative path, ' + 'but an absolute path was passed');
|
37 | }
|
38 |
|
39 | this.output = output.replace(/^\//, '').replace(/\/$/, '') + '/';
|
40 | this.publicPath = options.publicPath;
|
41 |
|
42 | this.basePath = null;
|
43 | this.location = null;
|
44 | this.pathRewrite = null;
|
45 |
|
46 |
|
47 | this.NETWORK = options.NETWORK;
|
48 | this.FALLBACK = options.FALLBACK;
|
49 | this.name = 'manifest';
|
50 | this.caches = options.caches;
|
51 | this.events = !!options.events;
|
52 | this.disableInstall = options.disableInstall;
|
53 | this.includeCrossOrigin = options.includeCrossOrigin;
|
54 | }
|
55 |
|
56 | _createClass(AppCache, [{
|
57 | key: 'addEntry',
|
58 | value: function addEntry(plugin, compilation, compiler) {
|
59 |
|
60 | return Promise.resolve();
|
61 | }
|
62 | }, {
|
63 | key: 'apply',
|
64 | value: function apply(plugin, compilation, compiler) {
|
65 | if (!Array.isArray(this.caches)) {
|
66 | throw new Error('AppCache caches must be an array');
|
67 | }
|
68 |
|
69 | var pathRewrite = this.pathRewrite;
|
70 | var cache = (this.caches.reduce(function (result, name) {
|
71 | var cache = plugin.caches[name];
|
72 | if (!cache || !cache.length) return result;
|
73 |
|
74 | if (result) {
|
75 | result = result.concat(cache);
|
76 | } else {
|
77 | result = cache;
|
78 | }
|
79 |
|
80 | return result;
|
81 | }, null) || []).map(pathRewrite);
|
82 |
|
83 | var path = this.output + this.name;
|
84 | var manifest = this.getManifestTemplate(cache, plugin);
|
85 | var content = this.getPageContent();
|
86 | var page = this.getPageTemplate(this.name, content);
|
87 |
|
88 | compilation.assets[path + '.appcache'] = (0, _miscUtils.getSource)(manifest);
|
89 | compilation.assets[path + '.html'] = (0, _miscUtils.getSource)(page);
|
90 | }
|
91 | }, {
|
92 | key: 'getManifestTemplate',
|
93 | value: function getManifestTemplate(cache, plugin) {
|
94 | var _this = this;
|
95 |
|
96 | var tag = '#ver:' + plugin.version;
|
97 |
|
98 | if (plugin.pluginVersion && !plugin.__tests.noVersionDump) {
|
99 | tag += '\n' + '#plugin:' + plugin.pluginVersion;
|
100 | }
|
101 |
|
102 | var FALLBACK = '';
|
103 | var NETWORK = '';
|
104 |
|
105 | if (this.NETWORK) {
|
106 | NETWORK = 'NETWORK:\n' + (Array.isArray(this.NETWORK) ? this.NETWORK.join('\n') : this.NETWORK + '');
|
107 | }
|
108 |
|
109 | if (plugin.appShell) {
|
110 | var scope = undefined;
|
111 |
|
112 | if (plugin.relativePaths) {
|
113 | scope = '';
|
114 | } else {
|
115 | scope = plugin.publicPath;
|
116 | }
|
117 |
|
118 | FALLBACK = (0, _deepExtend3['default'])(_defineProperty({}, this.pathRewrite(scope), plugin.appShell), this.FALLBACK || {});
|
119 | } else if (this.FALLBACK) {
|
120 | FALLBACK = this.FALLBACK;
|
121 | }
|
122 |
|
123 | if (FALLBACK) {
|
124 | FALLBACK = 'FALLBACK:\n' + Object.keys(FALLBACK).map(function (path) {
|
125 | return path + ' ' + FALLBACK[path];
|
126 | }).join('\n');
|
127 | }
|
128 |
|
129 | if (!this.includeCrossOrigin) {
|
130 | cache = cache.filter(function (asset) {
|
131 | if ((0, _miscUtils.isAbsoluteURL)(asset) && (_this.basePath === '/' || asset.indexOf(_this.basePath) !== 0)) {
|
132 | return false;
|
133 | }
|
134 |
|
135 | return true;
|
136 | });
|
137 | }
|
138 |
|
139 | return ('\n CACHE MANIFEST\n ' + tag + '\n\n CACHE:\n ' + cache.join('\n') + '\n\n ' + NETWORK + '\n\n ' + FALLBACK + '\n ').trim().replace(/^ */gm, '');
|
140 | }
|
141 | }, {
|
142 | key: 'getPageTemplate',
|
143 | value: function getPageTemplate(name, content) {
|
144 | return ('\n <!doctype html>\n <html manifest="' + name + '.appcache"><meta charset="utf-8">' + (content || '') + '</html>\n ').trim().replace(/^ */gm, '');
|
145 | }
|
146 | }, {
|
147 | key: 'getPageContent',
|
148 | value: function getPageContent() {
|
149 | if (this.events) {
|
150 | return _fs2['default'].readFileSync(_path2['default'].join(__dirname, '../tpls/appcache-frame.tpl'), 'utf-8');
|
151 | } else {
|
152 | return '';
|
153 | }
|
154 | }
|
155 | }, {
|
156 | key: 'getConfig',
|
157 | value: function getConfig(plugin) {
|
158 | return {
|
159 | location: this.location,
|
160 | name: this.name,
|
161 | events: this.events,
|
162 | disableInstall: this.disableInstall
|
163 | };
|
164 | }
|
165 | }]);
|
166 |
|
167 | return AppCache;
|
168 | })();
|
169 |
|
170 | exports['default'] = AppCache;
|
171 | module.exports = exports['default']; |
\ | No newline at end of file |