Skip to content

Commit f743aa9

Browse files
author
Matt Gaunt
authored
Workbox-build linking up config. (#1097)
* Switching back the webpack change * Just js
1 parent 0342601 commit f743aa9

File tree

8 files changed

+1290
-368
lines changed

8 files changed

+1290
-368
lines changed

package-lock.json

Lines changed: 905 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 370 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,370 @@
1+
/*
2+
Copyright 2017 Google Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import './_version.mjs';
18+
19+
/**
20+
* These are the full set of options that could potentially be used to configure
21+
* one of the build tools. Each of the build tools has a slightly different way
22+
* of providing this configuration:
23+
*
24+
* - When using the `workbox-build` module directly, pass the
25+
* configuration object to appropriate method. For example,
26+
* `workboxBuild.injectManifest(configuration)` or
27+
* `workboxBuild.generateSW(configuration)`.
28+
*
29+
* - When using the `workbox-cli` command line interface, use the
30+
* `--config-file` flag to point to a
31+
* [CommonJS module file](https://nodejs.org/docs/latest/api/modules.html) that
32+
* assigns the configuration object to `module.exports`.
33+
*
34+
* - When using `workbox-webpack-plugin` within a
35+
* [Webpack](https://webpack.js.org/) build, pass the configuration object to
36+
* the plugin's constructor, like
37+
* `new WorkboxBuildWebpackPlugin(configuration)`.
38+
*
39+
* Some specific options might not make sense with certain combinations of
40+
* interfaces. In those cases, the limitations are called out in the
41+
* documentation, and may lead to build-time warnings or errors.
42+
*
43+
* Each option documented here includes an example, which, for the sake of
44+
* illustration, assumes the following local filesystem setup. Please adjust
45+
* the example values to match your actual setup.
46+
*
47+
* ```sh
48+
* ./
49+
* ├── dev/
50+
* │ ├── app.js
51+
* │ ├── ignored.html
52+
* │ ├── image.png
53+
* │ ├── index.html
54+
* │ ├── main.css
55+
* │ ├── sw.js
56+
* │ └── templates/
57+
* │ └── app_shell.hbs
58+
* └── dist/
59+
* ├── app.js
60+
* ├── image.png
61+
* ├── index.html
62+
* ├── main.css
63+
* └── sw.js
64+
* ```
65+
*
66+
* @typedef {Object} Configuration
67+
*
68+
* @property {String} swDest The path to the final service worker
69+
* file that will be created by the build process, relative to the current
70+
* working directory.
71+
*
72+
* E.g.: `'./dist/sw.js'`
73+
*
74+
* Note: This option is only valid when used with
75+
* {@link module:workbox-build.generateSW|generateSW()} or
76+
* {@link module:workbox-build.injectManifest|injectManifest()}.
77+
*
78+
* @property {String} swSrc The path to the source service worker
79+
* containing a `precache([])` placeholder, which will be replaced with the
80+
* precache manifest generated by the build.
81+
*
82+
* E.g.: `'./dev/sw.js'`
83+
*
84+
* Note: This option is only valid when used with
85+
* {@link module:workbox-build.injectManifest|injectManifest()}.
86+
*
87+
* @property {String} swTemplate A service worker template that should be
88+
* populated based on the configuration provided. The template should be in a
89+
* format that [`lodash.template`](https://lodash.com/docs/4.17.4#template)
90+
* understands.
91+
*
92+
* Note: This option is only valid when used with
93+
* {@link module:workbox-build.generateSWNoFS|generateSWNoFS()}.
94+
*
95+
* @property {boolean} [importWorkboxFromCDN=true] If `true`, the WorkboxSW
96+
* runtime will be automatically imported into the generated service worker from
97+
* the official CDN URL. If `false`, the WorkboxSW runtime will be copied
98+
* locally into your `swDest` directory when using
99+
* {@link module:workbox-build.generateSW|generateSW()}.
100+
* If `process.env.NODE_ENV` is set to a string starting with `dev` then the
101+
* `dev` bundle of WorkboxSW, with additional assertions and debugging info,
102+
* will be used; otherwise, the `prod` bundle will be used.
103+
*
104+
* Note: This option is only valid when used with
105+
* {@link module:workbox-build.generateSW|generateSW()} or
106+
* {@link module:workbox-build.generateSWNoFS|generateSWNoFS()}.
107+
*
108+
* @property {Array<String>} [importScripts] An optional list of JavaScript
109+
* files that should be passed to
110+
* [`importScripts()`](https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/importScripts)
111+
* inside the generated service worker file.
112+
*
113+
* Note: This option is only valid when used with
114+
* {@link module:workbox-build.generateSW|generateSW()} or
115+
* {@link module:workbox-build.generateSWNoFS|generateSWNoFS()}.
116+
*
117+
* @property {Array<String>} [globPatterns=['**\/*.{js,css,html}']]
118+
* Files matching against any of these
119+
* [glob patterns](https://github.com/isaacs/node-glob) will be included in the
120+
* precache manifest.
121+
*
122+
* E.g.: `'**\/*.{js,css,html,png}'`
123+
*
124+
* @property {String} globDirectory The base directory you wish to
125+
* match `globPatterns` against, related to the current working directory.
126+
*
127+
* E.g.: `'./dev'`
128+
*
129+
* @property {String|Array<String>} [globIgnores='node_modules']
130+
* Files matching against any of these glob patterns will be excluded from the
131+
* file manifest, overriding any matches from `globPatterns`.
132+
*
133+
* E.g. `['**\/ignored.html']`
134+
*
135+
* @property {Object<String,Array|string>} [templatedUrls]
136+
* If a URL is rendered generated based on some server-side logic, its contents
137+
* may depend on multiple files or on some other unique string value.
138+
*
139+
* If used with an array of strings, they will be interpreted as
140+
* [glob patterns](https://github.com/isaacs/node-glob), and the contents of
141+
* any files matching the patterns will be used to uniquely version the URL.
142+
*
143+
* If used with a single string, it will be interpreted as unique versioning
144+
* information that you've generated out of band for a given URL.
145+
*
146+
* E.g.
147+
* ```js
148+
* {
149+
* '/app-shell': [
150+
* 'dev/templates/app-shell.hbs',
151+
* 'dev/**\/*.css',
152+
* ],
153+
* '/other-page': 'my-version-info',
154+
* }
155+
* ```
156+
*
157+
* @property {number} [maximumFileSizeToCacheInBytes=2097152]
158+
* This value can be used to determine the maximum size of files that will be
159+
* precached. This prevents you from inadvertantly precaching very large files
160+
* that might have been accidentally match your `globPatterns` values.
161+
*
162+
* @property {Array<ManifestTransform>} [manifestTransforms] An array of
163+
* manifest transformations, which will be applied sequentially against the
164+
* generated manifest. If `modifyUrlPrefix` or `dontCacheBustUrlsMatching` are
165+
* also specified, their corresponding transformations will be applied first.
166+
*
167+
* See {@link module:workbox-build.ManifestTransform|ManifestTransform}.
168+
*
169+
* @property {Object<String,String>} [modifyUrlPrefix] A mapping of
170+
* prefixes that, if present in an entry in the precache manifest, will be
171+
* replaced with the corresponding value.
172+
*
173+
* This can be used to, for example, remove or add a path prefix from a manifest
174+
* entry if your web hosting setup doesn't match your local filesystem setup.
175+
*
176+
* As an alternative with more flexibility, you can use the `manifestTransforms`
177+
* option and provide a function that modifies the entries in the manifest using
178+
* whatever logic you provide.
179+
*
180+
* E.g.
181+
* ```js
182+
* {
183+
* '/prefix-to-remove': '',
184+
* }
185+
* ```
186+
*
187+
* @property {RegExp} [dontCacheBustUrlsMatching] Assets that match this
188+
* regex will be assumed to be uniquely versioned via their URL, an exempted
189+
* from the normal HTTP cache-busting that's done when populating the precache.
190+
*
191+
* While not required, it's recommended that if your existing build process
192+
* already inserts a `[hash]` value into each filename, you provide a RegExp
193+
* that will detect those values, as it will reduce the amount of bandwidth
194+
* consumed when precaching.
195+
*
196+
* E.g. `/\.\w{8}\./`
197+
*
198+
* @property {String} [navigateFallback] This will be used to create a
199+
* {@link workbox.routing.NavigationRoute|NavigationRoute} that will
200+
* respond to navigation requests for URLs that that aren't precached.
201+
*
202+
* This is meant to be used in a
203+
* [Single Page App](https://en.wikipedia.org/wiki/Single-page_application)
204+
* scenario, in which you want all navigations to result in common App Shell
205+
* HTML being reused.
206+
*
207+
* It's *not* intended for use as a fallback that's displayed when the browser
208+
* is offline.
209+
*
210+
* Note: This option is only valid when used with
211+
* {@link module:workbox-build#generateSW|generateSW()}. When using
212+
* {@link module:workbox-build.injectManifest|injectManifest()}, you can
213+
* explicitly add in a call to
214+
* {@link module:workbox-sw.Router#registerNavigationRoute|
215+
* registerNavigationRoute()}
216+
* in your `swSrc` file.
217+
*
218+
* E.g. `'/app-shell'`
219+
*
220+
* @property {Array<RegExp>} [navigateFallbackWhitelist=/./] An optional
221+
* array of regular expressions that restrict which URLs the navigation route
222+
* applies to.
223+
*
224+
* Note: This option is only valid when used with
225+
* {@link module:workbox-build#generateSW|generateSW()}. When using
226+
* {@link module:workbox-build.injectManifest|injectManifest()}, you can
227+
* explicitly add in the whitelist when calling
228+
* {@link module:workbox-sw.Router#registerNavigationRoute|
229+
* registerNavigationRoute()}
230+
* in your `swSrc` file.
231+
*
232+
* E.g. `[/pages/, /articles/]`
233+
*
234+
* @property {String} [cacheId] An optional ID to be prepended to caches
235+
* used by `workbox-sw`. This is primarily useful for local development where
236+
* multiple sites may be served from the same `http://localhost` origin.
237+
*
238+
* Note: This option is only valid when used with
239+
* {@link module:workbox-build#generateSW|generateSW()}. When using
240+
* {@link module:workbox-build.injectManifest|injectManifest()}, you can
241+
* explicitly pass the desired value in to the
242+
* {@link module:workbox-sw.WorkboxSW|WorkboxSW() constructor} in your `swSrc`
243+
* file.
244+
*
245+
* E.g. `'my-app-name'`
246+
*
247+
* @property {Boolean} [skipWaiting=false] Whether or not the service worker
248+
* should skip over the [waiting](https://developers.google.com/web/fundamentals/instant-and-offline/service-worker/lifecycle#waiting)
249+
* lifecycle stage.
250+
*
251+
* Note: This option is only valid when used with
252+
* {@link module:workbox-build#generateSW|generateSW()}. When using
253+
* {@link module:workbox-build.injectManifest|injectManifest()}, you can
254+
* explicitly pass the desired value in to the
255+
* {@link module:workbox-sw.WorkboxSW|WorkboxSW() constructor} in your `swSrc`
256+
* file.
257+
*
258+
* @property {Boolean} [clientsClaim=false] Whether or not the service worker
259+
* should [start controlling](https://developers.google.com/web/fundamentals/instant-and-offline/service-worker/lifecycle#clientsclaim)
260+
* any existing clients as soon as it activates.
261+
*
262+
* Note: This option is only valid when used with
263+
* {@link module:workbox-build#generateSW|generateSW()}. When using
264+
* {@link module:workbox-build.injectManifest|injectManifest()}, you can
265+
* explicitly pass the desired value in to the
266+
* {@link module:workbox-sw.WorkboxSW|WorkboxSW() constructor} in your `swSrc`
267+
* file.
268+
*
269+
* @property {string} [directoryIndex='index.html'] If a request for a URL
270+
* ending in '/' fails, this value will be appended to the URL and a second
271+
* request will be made.
272+
*
273+
* This should be configured to whatever your web server is using, if anything,
274+
* for its [directory index](https://httpd.apache.org/docs/2.0/mod/mod_dir.html).
275+
*
276+
* Note: This option is only valid when used with
277+
* {@link module:workbox-build#generateSW|generateSW()}. When using
278+
* {@link module:workbox-build.injectManifest|injectManifest()}, you can
279+
* explicitly pass the desired value in to the
280+
* {@link module:workbox-sw.WorkboxSW|WorkboxSW() constructor} in your `swSrc`
281+
* file.
282+
*
283+
* @property {Array<Object>} [runtimeCaching] Passing in an array of objects
284+
* containing `urlPattern`s, `handler`s, and potentially `option`s that will add
285+
* the appropriate code to the generated service worker to handle runtime
286+
* caching.
287+
*
288+
* Requests for precached URLs that are picked up via `globPatterns` are handled
289+
* by default, and don't need to be accomodated in `runtimeCaching`.
290+
*
291+
* The `handler` values correspond the names of the
292+
* {@link module:workbox-sw.Strategies|strategies} supported by `workbox-sw`.
293+
*
294+
*
295+
* Note: This option is only valid when used with
296+
* {@link module:workbox-build#generateSW|generateSW()}. When using
297+
* {@link module:workbox-build.injectManifest|injectManifest()}, you can
298+
* explicitly add in the corresponding runtime caching behavior via
299+
* {@link module:workbox-sw.Router#registerRoute|registerRoute()} in your
300+
* `swSrc` file.
301+
*
302+
* E.g.
303+
* ```js
304+
* [{
305+
* // You can use a RegExp as the pattern:
306+
* urlPattern: /.jpg$/,
307+
* handler: 'cacheFirst',
308+
* // Any options provided will be used when
309+
* // creating the caching strategy.
310+
* options: {
311+
* cacheName: 'image-cache',
312+
* cacheExpiration: {
313+
* maxEntries: 10,
314+
* },
315+
* },
316+
* }, {
317+
* // You can also use Express-style strings:
318+
* urlPattern: 'https://example.com/path/to/:file',
319+
* handler: 'staleWhileRevalidate',
320+
* options: {
321+
* cacheableResponse: {
322+
statuses: [0],
323+
* },
324+
* },
325+
* }]
326+
* ```
327+
*
328+
* @property {Array<RegExp>} [ignoreUrlParametersMatching=[/^utm_/]] Any
329+
* search parameter names that match against one of the regex's in this array
330+
* will be removed before looking for a precache match.
331+
*
332+
* This is useful if your users might request URLs that contain, for example,
333+
* URL parameters used to track the source of the traffic. Those URL parameters
334+
* would normally cause the cache lookup to fail, since the URL strings used
335+
* as cache keys would not be expected to include them.
336+
*
337+
* You can use `[/./]` to ignore all URL parameters.
338+
*
339+
* Note: This option is only valid when used with
340+
* {@link module:workbox-build#generateSW|generateSW()}. When using
341+
* {@link module:workbox-build.injectManifest|injectManifest()}, you can
342+
* explicitly pass the desired value in to the
343+
* {@link module:workbox-sw.WorkboxSW|WorkboxSW() constructor} in your `swSrc`
344+
* file.
345+
*
346+
* E.g. `[/homescreen/]`
347+
*
348+
* @property {Boolean} [handleFetch=true] Whether or not `workbox-sw` should
349+
* create a `fetch` event handler that responds to network requests. This is
350+
* useful during development if you don't want the service worker serving stale
351+
* content.
352+
*
353+
* Note: This option is only valid when used with
354+
* {@link module:workbox-build#generateSW|generateSW()}. When using
355+
* {@link module:workbox-build.injectManifest|injectManifest()}, you can
356+
* explicitly pass the desired value in to the
357+
* {@link module:workbox-sw.WorkboxSW|WorkboxSW() constructor} in your `swSrc`
358+
* file.
359+
*
360+
* @memberof module:workbox-build
361+
*/
362+
363+
/**
364+
* @typedef {Object} ManifestEntry
365+
* @property {String} url The URL to the asset in the manifest.
366+
* @property {String} revision The revision details for the file. This is a
367+
* hash generated by node based on the file contents.
368+
*
369+
* @memberof module:workbox-build
370+
*/

packages/workbox-build/src/entry-points/generate-sw-string.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ const validate = require('./options/validate');
2323
* This method generates a service worker based on the configuration options
2424
* provided.
2525
*
26-
* @param {Object} input
26+
* @param {module:workbox-build.Configuration} config
2727
* @return {Promise<String>} A populated service worker template, based on the
2828
* other configuration options provided.
2929
*
3030
* @memberof module:workbox-build
3131
*/
32-
async function generateSWString(input) {
33-
const options = validate(input, generateSWStringSchema);
32+
async function generateSWString(config) {
33+
const options = validate(config, generateSWStringSchema);
3434

3535
const {manifestEntries} = await getFileManifestEntries(options);
3636

0 commit comments

Comments
 (0)