UNPKG

7.96 kBJavaScriptView Raw
1'use strict';
2
3var _regenerator = require('babel-runtime/regenerator');
4
5var _regenerator2 = _interopRequireDefault(_regenerator);
6
7var _getIterator2 = require('babel-runtime/core-js/get-iterator');
8
9var _getIterator3 = _interopRequireDefault(_getIterator2);
10
11var _promise = require('babel-runtime/core-js/promise');
12
13var _promise2 = _interopRequireDefault(_promise);
14
15var _keys = require('babel-runtime/core-js/object/keys');
16
17var _keys2 = _interopRequireDefault(_keys);
18
19var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
20
21var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
22
23function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
24
25/*
26 Copyright 2017 Google Inc.
27
28 Licensed under the Apache License, Version 2.0 (the "License");
29 you may not use this file except in compliance with the License.
30 You may obtain a copy of the License at
31
32 https://www.apache.org/licenses/LICENSE-2.0
33
34 Unless required by applicable law or agreed to in writing, software
35 distributed under the License is distributed on an "AS IS" BASIS,
36 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37 See the License for the specific language governing permissions and
38 limitations under the License.
39*/
40
41var fse = require('fs-extra');
42var path = require('path');
43
44var errors = require('./errors');
45var useBuildType = require('./use-build-type');
46
47// Used to filter the libraries to copy based on our package.json dependencies.
48var WORKBOX_PREFIX = 'workbox-';
49var BUILD_TYPES = ['dev', 'prod'];
50
51/**
52 * This copies over a set of runtime libraries used by Workbox into a
53 * local directory, which should be deployed alongside your service worker file.
54 *
55 * As an alternative to deploying these local copies, you could instead use
56 * Workbox from its official CDN URL.
57 *
58 * This method is exposed for the benefit of developers using
59 * [injectManifest()]{@link module:workbox-build.injectManifest} who would
60 * prefer not to use the CDN copies of Workbox. Developers using
61 * [generateSW()]{@link module:workbox-build.generateSW} don't need to
62 * explicitly call this method, as it's called automatically when
63 * `importWorkboxFrom` is set to `local`.
64 *
65 * @param {string} destDirectory The path to the parent directory under which
66 * the new directory of libraries will be created.
67 * @return {Promise<string>} The name of the newly created directory.
68 *
69 * @alias module:workbox-build.copyWorkboxLibraries
70 */
71module.exports = function () {
72 var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(destDirectory) {
73 var thisPkg, workboxDirectoryName, workboxDirectoryPath, copyPromises, librariesToCopy, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, library, pkg, defaultPathToLibrary, _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step2, buildType, srcPath, destPath;
74
75 return _regenerator2.default.wrap(function _callee$(_context) {
76 while (1) {
77 switch (_context.prev = _context.next) {
78 case 0:
79 thisPkg = require('../../package.json');
80 // Use the version string from workbox-build in the name of the parent
81 // directory. This should be safe, because lerna will bump workbox-build's
82 // pkg.version whenever one of the dependent libraries gets bumped, and we
83 // care about versioning the dependent libraries.
84
85 workboxDirectoryName = `workbox-v${thisPkg.version}`;
86 workboxDirectoryPath = path.join(destDirectory, workboxDirectoryName);
87 _context.next = 5;
88 return fse.ensureDir(workboxDirectoryPath);
89
90 case 5:
91 copyPromises = [];
92 librariesToCopy = (0, _keys2.default)(thisPkg.dependencies).filter(function (dependency) {
93 return dependency.startsWith(WORKBOX_PREFIX);
94 });
95 _iteratorNormalCompletion = true;
96 _didIteratorError = false;
97 _iteratorError = undefined;
98 _context.prev = 10;
99 _iterator = (0, _getIterator3.default)(librariesToCopy);
100
101 case 12:
102 if (_iteratorNormalCompletion = (_step = _iterator.next()).done) {
103 _context.next = 49;
104 break;
105 }
106
107 library = _step.value;
108 pkg = require(`${library}/package.json`);
109 defaultPathToLibrary = require.resolve(`${library}/${pkg.main}`);
110 _iteratorNormalCompletion2 = true;
111 _didIteratorError2 = false;
112 _iteratorError2 = undefined;
113 _context.prev = 19;
114 _iterator2 = (0, _getIterator3.default)(BUILD_TYPES);
115
116 case 21:
117 if (_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done) {
118 _context.next = 32;
119 break;
120 }
121
122 buildType = _step2.value;
123
124 if (!(library === 'workbox-sw' && buildType === BUILD_TYPES[0])) {
125 _context.next = 25;
126 break;
127 }
128
129 return _context.abrupt('continue', 29);
130
131 case 25:
132 srcPath = useBuildType(defaultPathToLibrary, buildType);
133 destPath = path.join(workboxDirectoryPath, path.basename(srcPath));
134
135 copyPromises.push(fse.copy(srcPath, destPath));
136 copyPromises.push(fse.copy(`${srcPath}.map`, `${destPath}.map`));
137
138 case 29:
139 _iteratorNormalCompletion2 = true;
140 _context.next = 21;
141 break;
142
143 case 32:
144 _context.next = 38;
145 break;
146
147 case 34:
148 _context.prev = 34;
149 _context.t0 = _context['catch'](19);
150 _didIteratorError2 = true;
151 _iteratorError2 = _context.t0;
152
153 case 38:
154 _context.prev = 38;
155 _context.prev = 39;
156
157 if (!_iteratorNormalCompletion2 && _iterator2.return) {
158 _iterator2.return();
159 }
160
161 case 41:
162 _context.prev = 41;
163
164 if (!_didIteratorError2) {
165 _context.next = 44;
166 break;
167 }
168
169 throw _iteratorError2;
170
171 case 44:
172 return _context.finish(41);
173
174 case 45:
175 return _context.finish(38);
176
177 case 46:
178 _iteratorNormalCompletion = true;
179 _context.next = 12;
180 break;
181
182 case 49:
183 _context.next = 55;
184 break;
185
186 case 51:
187 _context.prev = 51;
188 _context.t1 = _context['catch'](10);
189 _didIteratorError = true;
190 _iteratorError = _context.t1;
191
192 case 55:
193 _context.prev = 55;
194 _context.prev = 56;
195
196 if (!_iteratorNormalCompletion && _iterator.return) {
197 _iterator.return();
198 }
199
200 case 58:
201 _context.prev = 58;
202
203 if (!_didIteratorError) {
204 _context.next = 61;
205 break;
206 }
207
208 throw _iteratorError;
209
210 case 61:
211 return _context.finish(58);
212
213 case 62:
214 return _context.finish(55);
215
216 case 63:
217 _context.prev = 63;
218 _context.next = 66;
219 return _promise2.default.all(copyPromises);
220
221 case 66:
222 return _context.abrupt('return', workboxDirectoryName);
223
224 case 69:
225 _context.prev = 69;
226 _context.t2 = _context['catch'](63);
227 throw Error(`${errors['unable-to-copy-workbox-libraries']} ${_context.t2}`);
228
229 case 72:
230 case 'end':
231 return _context.stop();
232 }
233 }
234 }, _callee, undefined, [[10, 51, 55, 63], [19, 34, 38, 46], [39,, 41, 45], [56,, 58, 62], [63, 69]]);
235 }));
236
237 return function (_x) {
238 return _ref.apply(this, arguments);
239 };
240}();
\No newline at end of file