diff options
Diffstat (limited to 'node_modules/workbox-build/src/entry-points')
13 files changed, 576 insertions, 0 deletions
diff --git a/node_modules/workbox-build/src/entry-points/generate-sw-string.js b/node_modules/workbox-build/src/entry-points/generate-sw-string.js new file mode 100644 index 00000000..925be51f --- /dev/null +++ b/node_modules/workbox-build/src/entry-points/generate-sw-string.js @@ -0,0 +1,47 @@ +/* + Copyright 2017 Google Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +const generateSWStringSchema = require('./options/generate-sw-string-schema'); +const getFileManifestEntries = require('../lib/get-file-manifest-entries'); +const populateSWTemplate = require('../lib/populate-sw-template'); +const validate = require('./options/validate'); + +/** + * This method generates a service worker based on the configuration options + * provided. + * + * @param {Object} config Please refer to the + * [configuration guide](https://developers.google.com/web/tools/workbox/modules/workbox-build#generateswstring_mode). + * @return {Promise<{swString: string, warnings: Array<string>}>} A promise that + * resolves once the service worker template is populated. The `swString` + * property contains a string representation of the full service worker code. + * Any non-fatal warning messages will be returned via `warnings`. + * + * @memberof module:workbox-build + */ +async function generateSWString(config) { + const options = validate(config, generateSWStringSchema); + + const {manifestEntries, warnings} = await getFileManifestEntries(options); + + const swString = await populateSWTemplate(Object.assign({ + manifestEntries, + }, options)); + + return {swString, warnings}; +} + +module.exports = generateSWString; diff --git a/node_modules/workbox-build/src/entry-points/generate-sw.js b/node_modules/workbox-build/src/entry-points/generate-sw.js new file mode 100644 index 00000000..7d3b7078 --- /dev/null +++ b/node_modules/workbox-build/src/entry-points/generate-sw.js @@ -0,0 +1,84 @@ +/* + Copyright 2017 Google Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +const path = require('path'); + +const cdnUtils = require('../lib/cdn-utils'); +const copyWorkboxLibraries = require('../lib/copy-workbox-libraries'); +const generateSWSchema = require('./options/generate-sw-schema'); +const getFileManifestEntries = require('../lib/get-file-manifest-entries'); +const validate = require('./options/validate'); +const writeServiceWorkerUsingDefaultTemplate = + require('../lib/write-sw-using-default-template'); + +/** + * This method creates a list of URLs to precache, referred to as a "precache + * manifest", based on the options you provide. + * + * It also takes in additional options that configures the service worker's + * behavior, like any `runtimeCaching` rules it should use. + * + * Based on the precache manifest and the additional configuration, it writes + * a ready-to-use service worker file to disk at `swDest`. + * + * @param {Object} config Please refer to the + * [configuration guide](https://developers.google.com/web/tools/workbox/modules/workbox-build#full_generatesw_config). + * @return {Promise<{count: number, size: number, warnings: Array<string>}>} + * A promise that resolves once the service worker file has been written to + * `swDest`. The `size` property contains the aggregate size of all the + * precached entries, in bytes, and the `count` property contains the total + * number of precached entries. Any non-fatal warning messages will be returned + * via `warnings`. + * + * @memberof module:workbox-build + */ +async function generateSW(config) { + const options = validate(config, generateSWSchema); + + const destDirectory = path.dirname(options.swDest); + + // Do nothing if importWorkboxFrom is set to 'disabled'. Otherwise, check: + if (options.importWorkboxFrom === 'cdn') { + const cdnUrl = cdnUtils.getModuleUrl('workbox-sw'); + options.workboxSWImport = cdnUrl; + } else if (options.importWorkboxFrom === 'local') { + // Copy over the dev + prod version of all of the core libraries. + const workboxDirectoryName = await copyWorkboxLibraries(destDirectory); + + // The Workbox library files should not be precached, since they're cached + // automatically by virtue of being used with importScripts(). + options.globIgnores = [ + `**/${workboxDirectoryName}/*.js*`, + ].concat(options.globIgnores || []); + + const workboxSWPkg = require(`workbox-sw/package.json`); + const workboxSWFilename = path.basename(workboxSWPkg.main); + + options.workboxSWImport = `${workboxDirectoryName}/${workboxSWFilename}`; + options.modulePathPrefix = workboxDirectoryName; + } + + const {count, size, manifestEntries, warnings} = + await getFileManifestEntries(options); + + await writeServiceWorkerUsingDefaultTemplate(Object.assign({ + manifestEntries, + }, options)); + + return {count, size, warnings}; +} + +module.exports = generateSW; diff --git a/node_modules/workbox-build/src/entry-points/get-manifest.js b/node_modules/workbox-build/src/entry-points/get-manifest.js new file mode 100644 index 00000000..6eafaee5 --- /dev/null +++ b/node_modules/workbox-build/src/entry-points/get-manifest.js @@ -0,0 +1,46 @@ +/* + Copyright 2017 Google Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +const getFileManifestEntries = require('../lib/get-file-manifest-entries'); +const getManifestSchema = require('./options/get-manifest-schema'); +const validate = require('./options/validate'); + +/** + * This method returns a list of URLs to precache, referred to as a "precache + * manifest", along with details about the number of entries and their size, + * based on the options you provide. + * + * @param {Object} config Please refer to the + * [configuration guide](https://developers.google.com/web/tools/workbox/modules/workbox-build#getmanifest_mode). + * @return {Promise<{manifestEntries: Array<ManifestEntry>, + * count: number, size: number, warnings: Array<string>}>} A promise that + * resolves once the precache manifest is determined. The `size` property + * contains the aggregate size of all the precached entries, in bytes, the + * `count` property contains the total number of precached entries, and the + * `manifestEntries` property contains all the `ManifestEntry` items. Any + * non-fatal warning messages will be returned via `warnings`. + * + * @memberof module:workbox-build + */ +async function getManifest(config) { + const options = validate(config, getManifestSchema); + + const {manifestEntries, count, size, warnings} = + await getFileManifestEntries(options); + return {manifestEntries, count, size, warnings}; +} + +module.exports = getManifest; diff --git a/node_modules/workbox-build/src/entry-points/inject-manifest.js b/node_modules/workbox-build/src/entry-points/inject-manifest.js new file mode 100644 index 00000000..eac3e820 --- /dev/null +++ b/node_modules/workbox-build/src/entry-points/inject-manifest.js @@ -0,0 +1,93 @@ +/* + Copyright 2017 Google Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +const assert = require('assert'); +const fse = require('fs-extra'); +const path = require('path'); + +const defaults = require('./options/defaults'); +const errors = require('../lib/errors'); +const getFileManifestEntries = require('../lib/get-file-manifest-entries'); +const injectManifestSchema = require('./options/inject-manifest-schema'); +const validate = require('./options/validate'); + +/** + * This method creates a list of URLs to precache, referred to as a "precache + * manifest", based on the options you provide. + * + * The manifest is injected into the `swSrc` file, and the regular expression + * `injectionPointRegexp` determines where in the file the manifest should go. + * + * The final service worker file, with the manifest injected, is written to + * disk at `swDest`. + * + * @param {Object} config Please refer to the + * [configuration guide](https://developers.google.com/web/tools/workbox/modules/workbox-build#full_injectmanifest_config). + * @return {Promise<{count: number, size: number, warnings: Array<string>}>} + * A promise that resolves once the service worker file has been written to + * `swDest`. The `size` property contains the aggregate size of all the + * precached entries, in bytes, and the `count` property contains the total + * number of precached entries. Any non-fatal warning messages will be returned + * via `warnings`. + * + * @memberof module:workbox-build + */ +async function injectManifest(config) { + const options = validate(config, injectManifestSchema); + + if (path.normalize(config.swSrc) === path.normalize(config.swDest)) { + throw new Error(errors['same-src-and-dest']); + } + + const globalRegexp = new RegExp(options.injectionPointRegexp, 'g'); + + const {count, size, manifestEntries, warnings} = + await getFileManifestEntries(options); + let swFileContents; + try { + swFileContents = await fse.readFile(config.swSrc, 'utf8'); + } catch (error) { + throw new Error(`${errors['invalid-sw-src']} ${error.message}`); + } + + const injectionResults = swFileContents.match(globalRegexp); + assert(injectionResults, errors['injection-point-not-found'] + + // Customize the error message when this happens: + // - If the default RegExp is used, then include the expected string that + // matches as a hint to the developer. + // - If a custom RegExp is used, then just include the raw RegExp. + (options.injectionPointRegexp === defaults.injectionPointRegexp ? + 'workbox.precaching.precacheAndRoute([])' : + options.injectionPointRegexp)); + assert(injectionResults.length === 1, errors['multiple-injection-points'] + + ` ${options.injectionPointRegexp}`); + + const entriesString = JSON.stringify(manifestEntries, null, 2); + swFileContents = swFileContents.replace(globalRegexp, `$1${entriesString}$2`); + + try { + await fse.mkdirp(path.dirname(options.swDest)); + } catch (error) { + throw new Error(errors['unable-to-make-injection-directory'] + + ` '${error.message}'`); + } + + await fse.writeFile(config.swDest, swFileContents); + + return {count, size, warnings}; +} + +module.exports = injectManifest; diff --git a/node_modules/workbox-build/src/entry-points/options/base-schema.js b/node_modules/workbox-build/src/entry-points/options/base-schema.js new file mode 100644 index 00000000..12292bfa --- /dev/null +++ b/node_modules/workbox-build/src/entry-points/options/base-schema.js @@ -0,0 +1,37 @@ +/* + Copyright 2017 Google Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +const joi = require('joi'); + +const defaults = require('./defaults'); +const regExpObject = require('./reg-exp-object'); + +// Define some common constrains used by all methods. +module.exports = joi.object().keys({ + dontCacheBustUrlsMatching: regExpObject, + globFollow: joi.boolean().default(defaults.globFollow), + globIgnores: joi.array().items(joi.string()).default(defaults.globIgnores), + globPatterns: joi.array().items(joi.string()).default(defaults.globPatterns), + globStrict: joi.boolean().default(defaults.globStrict), + manifestTransforms: joi.array().items(joi.func().arity(1)), + maximumFileSizeToCacheInBytes: joi.number().min(1) + .default(defaults.maximumFileSizeToCacheInBytes), + modifyUrlPrefix: joi.object(), + // templatedUrls is an object where any property name is valid, and the values + // can be either a string or an array of strings. + templatedUrls: joi.object().pattern(/./, + [joi.string(), joi.array().items(joi.string())]), +}); diff --git a/node_modules/workbox-build/src/entry-points/options/common-generate-schema.js b/node_modules/workbox-build/src/entry-points/options/common-generate-schema.js new file mode 100644 index 00000000..69e92843 --- /dev/null +++ b/node_modules/workbox-build/src/entry-points/options/common-generate-schema.js @@ -0,0 +1,77 @@ +/* + Copyright 2017 Google Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +const joi = require('joi'); + +const baseSchema = require('./base-schema'); +const defaults = require('./defaults'); +const regExpObject = require('./reg-exp-object'); + +// Add some constraints that apply to both generateSW and generateSWString. +module.exports = baseSchema.keys({ + cacheId: joi.string(), + clientsClaim: joi.boolean().default(defaults.clientsClaim), + directoryIndex: joi.string(), + ignoreUrlParametersMatching: joi.array().items(regExpObject), + navigateFallback: joi.string().default(defaults.navigateFallback), + navigateFallbackBlacklist: joi.array().items(regExpObject), + navigateFallbackWhitelist: joi.array().items(regExpObject), + offlineGoogleAnalytics: joi.alternatives().try(joi.boolean(), joi.object()) + .default(defaults.offlineGoogleAnalytics), + runtimeCaching: joi.array().items(joi.object().keys({ + method: joi.string().valid( + 'DELETE', + 'GET', + 'HEAD', + 'PATCH', + 'POST', + 'PUT' + ), + urlPattern: [regExpObject, joi.string()], + handler: [joi.func(), joi.string().valid( + 'cacheFirst', + 'cacheOnly', + 'networkFirst', + 'networkOnly', + 'staleWhileRevalidate' + )], + options: joi.object().keys({ + backgroundSync: joi.object().keys({ + name: joi.string().required(), + options: joi.object(), + }), + broadcastUpdate: joi.object().keys({ + channelName: joi.string().required(), + options: joi.object(), + }), + cacheableResponse: joi.object().keys({ + statuses: joi.array().items(joi.number().min(0).max(599)), + headers: joi.object(), + }).or('statuses', 'headers'), + cacheName: joi.string(), + expiration: joi.object().keys({ + maxEntries: joi.number().min(1), + maxAgeSeconds: joi.number().min(1), + purgeOnQuotaError: joi.boolean().default(defaults.purgeOnQuotaError), + }).or('maxEntries', 'maxAgeSeconds'), + networkTimeoutSeconds: joi.number().min(1), + plugins: joi.array().items(joi.object()), + fetchOptions: joi.object(), + matchOptions: joi.object(), + }).with('expiration', 'cacheName'), + }).requiredKeys('urlPattern', 'handler')), + skipWaiting: joi.boolean().default(defaults.skipWaiting), +}); diff --git a/node_modules/workbox-build/src/entry-points/options/defaults.js b/node_modules/workbox-build/src/entry-points/options/defaults.js new file mode 100644 index 00000000..7a8bbdb0 --- /dev/null +++ b/node_modules/workbox-build/src/entry-points/options/defaults.js @@ -0,0 +1,30 @@ +/* + Copyright 2017 Google Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +module.exports = { + clientsClaim: false, + globFollow: true, + globIgnores: ['**/node_modules/**/*'], + globPatterns: ['**/*.{js,css,html}'], + globStrict: true, + importWorkboxFrom: 'cdn', + injectionPointRegexp: /(\.precacheAndRoute\()\s*\[\s*\]\s*(\)|,)/, + maximumFileSizeToCacheInBytes: 2 * 1024 * 1024, + navigateFallback: undefined, + offlineGoogleAnalytics: false, + purgeOnQuotaError: false, + skipWaiting: false, +}; diff --git a/node_modules/workbox-build/src/entry-points/options/generate-sw-schema.js b/node_modules/workbox-build/src/entry-points/options/generate-sw-schema.js new file mode 100644 index 00000000..64dbaf29 --- /dev/null +++ b/node_modules/workbox-build/src/entry-points/options/generate-sw-schema.js @@ -0,0 +1,32 @@ +/* + Copyright 2017 Google Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +const joi = require('joi'); + +const commonGenerateSchema = require('./common-generate-schema'); +const defaults = require('./defaults'); + +// Define some additional constraints. +module.exports = commonGenerateSchema.keys({ + globDirectory: joi.string().required(), + importScripts: joi.array().items(joi.string()), + importWorkboxFrom: joi.string().default(defaults.importWorkboxFrom).valid( + 'cdn', + 'local', + 'disabled' + ), + swDest: joi.string().required(), +}); diff --git a/node_modules/workbox-build/src/entry-points/options/generate-sw-string-schema.js b/node_modules/workbox-build/src/entry-points/options/generate-sw-string-schema.js new file mode 100644 index 00000000..9c1f496c --- /dev/null +++ b/node_modules/workbox-build/src/entry-points/options/generate-sw-string-schema.js @@ -0,0 +1,27 @@ +/* + Copyright 2017 Google Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +const joi = require('joi'); + +const commonGenerateSchema = require('./common-generate-schema'); + +// Define some additional constraints. +module.exports = commonGenerateSchema.keys({ + globDirectory: joi.string(), + importScripts: joi.array().items(joi.string()).required(), + modulePathPrefix: joi.string(), + workboxSWImport: joi.string(), +}); diff --git a/node_modules/workbox-build/src/entry-points/options/get-manifest-schema.js b/node_modules/workbox-build/src/entry-points/options/get-manifest-schema.js new file mode 100644 index 00000000..31704c8e --- /dev/null +++ b/node_modules/workbox-build/src/entry-points/options/get-manifest-schema.js @@ -0,0 +1,24 @@ +/* + Copyright 2017 Google Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +const joi = require('joi'); + +const baseSchema = require('./base-schema'); + +// Define some additional constraints. +module.exports = baseSchema.keys({ + globDirectory: joi.string(), +}); diff --git a/node_modules/workbox-build/src/entry-points/options/inject-manifest-schema.js b/node_modules/workbox-build/src/entry-points/options/inject-manifest-schema.js new file mode 100644 index 00000000..6f901824 --- /dev/null +++ b/node_modules/workbox-build/src/entry-points/options/inject-manifest-schema.js @@ -0,0 +1,28 @@ +/* + Copyright 2017 Google Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +const joi = require('joi'); + +const baseSchema = require('./base-schema'); +const defaults = require('./defaults'); +const regExpObject = require('./reg-exp-object'); + +module.exports = baseSchema.keys({ + globDirectory: joi.string().required(), + injectionPointRegexp: regExpObject.default(defaults.injectionPointRegexp), + swSrc: joi.string().required(), + swDest: joi.string().required(), +}); diff --git a/node_modules/workbox-build/src/entry-points/options/reg-exp-object.js b/node_modules/workbox-build/src/entry-points/options/reg-exp-object.js new file mode 100644 index 00000000..d91fd1a1 --- /dev/null +++ b/node_modules/workbox-build/src/entry-points/options/reg-exp-object.js @@ -0,0 +1,20 @@ +/* + Copyright 2017 Google Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +const joi = require('joi'); + +module.exports = joi.object().type(RegExp) + .error(() => 'the value must be a RegExp'); diff --git a/node_modules/workbox-build/src/entry-points/options/validate.js b/node_modules/workbox-build/src/entry-points/options/validate.js new file mode 100644 index 00000000..487e5446 --- /dev/null +++ b/node_modules/workbox-build/src/entry-points/options/validate.js @@ -0,0 +1,31 @@ +/* + Copyright 2017 Google Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +module.exports = (options, schema) => { + const {value, error} = schema.validate(options, { + language: { + object: { + allowUnknown: 'is not a supported parameter.', + }, + }, + }); + + if (error) { + throw error; + } + + return value; +}; |
