UNPKG

6.09 kBJavaScriptView Raw
1'use strict';
2
3var _getIterator2 = require('babel-runtime/core-js/get-iterator');
4
5var _getIterator3 = _interopRequireDefault(_getIterator2);
6
7function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
9/*
10 Copyright 2017 Google Inc.
11
12 Licensed under the Apache License, Version 2.0 (the "License");
13 you may not use this file except in compliance with the License.
14 You may obtain a copy of the License at
15
16 https://www.apache.org/licenses/LICENSE-2.0
17
18 Unless required by applicable law or agreed to in writing, software
19 distributed under the License is distributed on an "AS IS" BASIS,
20 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 See the License for the specific language governing permissions and
22 limitations under the License.
23*/
24
25var maximumSizeTransform = require('./maximum-size-transform');
26var modifyUrlPrefixTranform = require('./modify-url-prefix-transform');
27var noRevisionForUrlsMatchingTransform = require('./no-revision-for-urls-matching-transform');
28
29/**
30 * A `ManifestTransform` function can be used to modify the modify the `url` or
31 * `revision` properties of some or all of the
32 * {@link module:workbox-build#ManifestEntry|ManifestEntries} in the manifest.
33 *
34 * Deleting the `revision` property of an entry will cause
35 * the corresponding `url` to be precached without cache-busting parameters
36 * applied, which is to say, it implies that the URL itself contains
37 * proper versioning info. If the `revision` property is present, it must be
38 * set to a string.
39 *
40 * @example <caption>A transformation that prepended the origin of a CDN for any
41 * URL starting with '/assets/' could be implemented as:</caption>
42 *
43 * const cdnTransform = (manifestEntries) => {
44 * const manifest = manifestEntries.map(entry => {
45 * const cdnOrigin = 'https://example.com';
46 * if (entry.url.startsWith('/assets/')) {
47 * entry.url = cdnOrigin + entry.url;
48 * }
49 * return entry;
50 * });
51 * return {manifest, warnings: []};
52 * };
53 *
54 * @example <caption>A transformation that removes the revision field when the
55 * URL contains an 8-character hash surrounded by '.', indicating that it
56 * already contains revision information:</caption>
57 *
58 * const removeRevisionTransform = (manifestEntries) => {
59 * const manifest = manifestEntries.map(entry => {
60 * const hashRegExp = /\.\w{8}\./;
61 * if (entry.url.match(hashRegExp)) {
62 * delete entry.revision;
63 * }
64 * return entry;
65 * });
66 * return {manifest, warnings: []};
67 * };
68 *
69 * @callback ManifestTransform
70 * @param {Array<module:workbox-build.ManifestEntry>} manifestEntries The full
71 * array of entries, prior to the current transformation.
72 * @return {module:workbox-build.ManifestTransformResult}
73 * The array of entries with the transformation applied, and optionally, any
74 * warnings that should be reported back to the build tool.
75 *
76 * @memberof module:workbox-build
77 */
78
79module.exports = function (_ref) {
80 var dontCacheBustUrlsMatching = _ref.dontCacheBustUrlsMatching,
81 fileDetails = _ref.fileDetails,
82 manifestTransforms = _ref.manifestTransforms,
83 maximumFileSizeToCacheInBytes = _ref.maximumFileSizeToCacheInBytes,
84 modifyUrlPrefix = _ref.modifyUrlPrefix;
85
86 var allWarnings = [];
87
88 // Take the array of fileDetail objects and convert it into an array of
89 // {url, revision, size} objects, with \ replaced with /.
90 var normalizedManifest = fileDetails.map(function (fileDetails) {
91 return {
92 url: fileDetails.file.replace(/\\/g, '/'),
93 revision: fileDetails.hash,
94 size: fileDetails.size
95 };
96 });
97
98 var transformsToApply = [];
99
100 if (maximumFileSizeToCacheInBytes) {
101 transformsToApply.push(maximumSizeTransform(maximumFileSizeToCacheInBytes));
102 }
103
104 if (modifyUrlPrefix) {
105 transformsToApply.push(modifyUrlPrefixTranform(modifyUrlPrefix));
106 }
107
108 if (dontCacheBustUrlsMatching) {
109 transformsToApply.push(noRevisionForUrlsMatchingTransform(dontCacheBustUrlsMatching));
110 }
111
112 // Any additional manifestTransforms that were passed will be applied last.
113 transformsToApply = transformsToApply.concat(manifestTransforms || []);
114
115 var transformedManifest = normalizedManifest;
116 var _iteratorNormalCompletion = true;
117 var _didIteratorError = false;
118 var _iteratorError = undefined;
119
120 try {
121 for (var _iterator = (0, _getIterator3.default)(transformsToApply), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
122 var transform = _step.value;
123
124 var _transform = transform(transformedManifest),
125 manifest = _transform.manifest,
126 warnings = _transform.warnings;
127
128 transformedManifest = manifest;
129 allWarnings = allWarnings.concat(warnings || []);
130 }
131
132 // Generate some metadata about the manifest before we clear out the size
133 // properties from each entry.
134 } catch (err) {
135 _didIteratorError = true;
136 _iteratorError = err;
137 } finally {
138 try {
139 if (!_iteratorNormalCompletion && _iterator.return) {
140 _iterator.return();
141 }
142 } finally {
143 if (_didIteratorError) {
144 throw _iteratorError;
145 }
146 }
147 }
148
149 var count = transformedManifest.length;
150 var size = 0;
151 var _iteratorNormalCompletion2 = true;
152 var _didIteratorError2 = false;
153 var _iteratorError2 = undefined;
154
155 try {
156 for (var _iterator2 = (0, _getIterator3.default)(transformedManifest), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
157 var manifestEntry = _step2.value;
158
159 size += manifestEntry.size;
160 delete manifestEntry.size;
161 }
162 } catch (err) {
163 _didIteratorError2 = true;
164 _iteratorError2 = err;
165 } finally {
166 try {
167 if (!_iteratorNormalCompletion2 && _iterator2.return) {
168 _iterator2.return();
169 }
170 } finally {
171 if (_didIteratorError2) {
172 throw _iteratorError2;
173 }
174 }
175 }
176
177 return {
178 count,
179 size,
180 manifestEntries: transformedManifest,
181 warnings: allWarnings
182 };
183};
\No newline at end of file