Skip to content

Commit

Permalink
Refactor the way workbox-build references defaults, and update the in…
Browse files Browse the repository at this point in the history
…jection-point-not-found message. (#1075)
  • Loading branch information
jeffposnick authored Nov 29, 2017
1 parent fb4401c commit 7a74459
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 14 deletions.
9 changes: 8 additions & 1 deletion packages/workbox-build/src/entry-points/inject-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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');
Expand Down Expand Up @@ -60,7 +61,13 @@ async function injectManifest(input) {

const injectionResults = swFileContents.match(globalRegexp);
assert(injectionResults, errors['injection-point-not-found'] +
` ${options.injectionPointRegexp}`);
// 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}`);

Expand Down
13 changes: 6 additions & 7 deletions packages/workbox-build/src/entry-points/options/base-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@

const joi = require('joi');

const defaults = require('./defaults');

// Define some common constrains used by all methods.
module.exports = joi.object().keys({
dontCacheBustUrlsMatching: joi.object().type(RegExp),
globIgnores: joi.array().items(joi.string()).default([
'node_modules/**/*',
]),
globPatterns: joi.array().items(joi.string()).default([
'**/*.{js,css,html}',
]),
globIgnores: joi.array().items(joi.string()).default(defaults.globIgnores),
globPatterns: joi.array().items(joi.string()).default(defaults.globPatterns),
manifestTransforms: joi.array().items(joi.func().arity(1)),
maximumFileSizeToCacheInBytes: joi.number().min(1).default(2 * 1024 * 1024),
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
const joi = require('joi');

const baseSchema = require('./base-schema');
const defaults = require('./defaults');

// Add some constraints that apply to both generateSW and generateSWString.
module.exports = baseSchema.keys({
cacheId: joi.string(),
clientsClaim: joi.boolean().default(false),
clientsClaim: joi.boolean().default(defaults.clientsClaim),
directoryIndex: joi.string(),
ignoreUrlParametersMatching: joi.array().items(joi.object().type(RegExp)),
navigateFallback: joi.string().default(false),
navigateFallback: joi.string().default(defaults.navigateFallback),
navigateFallbackBlacklist: joi.array().items(joi.object().type(RegExp)),
navigateFallbackWhitelist: joi.array().items(joi.object().type(RegExp)),
runtimeCaching: joi.array().items(joi.object().keys({
Expand All @@ -49,5 +50,5 @@ module.exports = baseSchema.keys({
}).or('statuses', 'headers'),
}),
}).requiredKeys('urlPattern', 'handler')),
skipWaiting: joi.boolean().default(false),
skipWaiting: joi.boolean().default(defaults.skipWaiting),
});
26 changes: 26 additions & 0 deletions packages/workbox-build/src/entry-points/options/defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
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 = {
globIgnores: ['node_modules/**/*'],
globPatterns: ['**/*.{js,css,html}'],
maximumFileSizeToCacheInBytes: 2 * 1024 * 1024,
clientsClaim: false,
navigateFallback: undefined,
skipWaiting: false,
importWorkboxFromCDN: true,
injectionPointRegexp: /(\.precacheAndRoute\()\s*\[\s*\]\s*(\))/,
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
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()),
importWorkboxFromCDN: joi.boolean().default(true),
importWorkboxFromCDN: joi.boolean().default(defaults.importWorkboxFromCDN),
swDest: joi.string().required(),
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
const joi = require('joi');

const baseSchema = require('./base-schema');
const defaults = require('./defaults');

module.exports = baseSchema.keys({
globDirectory: joi.string().required(),
injectionPointRegexp: joi.object().type(RegExp)
.default(/(\.precacheAndRoute\()\s*\[\s*\]\s*(\))/),
.default(defaults.injectionPointRegexp),
swSrc: joi.string().required(),
swDest: joi.string().required(),
});
2 changes: 1 addition & 1 deletion packages/workbox-build/src/lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ module.exports = {
'invalid-inject-manifest-arg': ol`The input to 'injectManifest()' must be an
object.`,
'injection-point-not-found': ol`Unable to find a place to inject the manifest.
Please ensure that your 'swSrc' file contains a match for the RegExp:`,
Please ensure that your service worker file contains the following: `,
'multiple-injection-points': ol`Please ensure that your 'swSrc' file contains
only one match for the RegExp:`,
'populating-sw-tmpl-failed': ol`Unable to generate service worker from
Expand Down

0 comments on commit 7a74459

Please sign in to comment.