Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Url to URL renaming for build interfaces #1841

Merged
merged 5 commits into from
Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/workbox-build/src/entry-points/generate-sw-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
https://opensource.org/licenses/MIT.
*/

const checkForDeprecatedOptions =
require('../lib/check-for-deprecated-options');
const generateSWStringSchema = require('./options/generate-sw-string-schema');
const getFileManifestEntries = require('../lib/get-file-manifest-entries');
const populateSWTemplate = require('../lib/populate-sw-template');
Expand All @@ -25,6 +27,10 @@ const validate = require('./options/validate');
* @memberof module:workbox-build
*/
async function generateSWString(config) {
// This check needs to be done before validation, since the deprecated options
// will be renamed.
const deprecationWarnings = checkForDeprecatedOptions(config);

const options = validate(config, generateSWStringSchema);

const {manifestEntries, warnings} = await getFileManifestEntries(options);
Expand All @@ -33,6 +39,9 @@ async function generateSWString(config) {
manifestEntries,
}, options));

// Add in any deprecation warnings.
warnings.push(...deprecationWarnings);

return {swString, warnings};
}

Expand Down
13 changes: 11 additions & 2 deletions packages/workbox-build/src/entry-points/generate-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
const path = require('path');

const cdnUtils = require('../lib/cdn-utils');
const checkForDeprecatedOptions =
require('../lib/check-for-deprecated-options');
const copyWorkboxLibraries = require('../lib/copy-workbox-libraries');
const generateSWSchema = require('./options/generate-sw-schema');
const getFileManifestEntries = require('../lib/get-file-manifest-entries');
Expand Down Expand Up @@ -38,14 +40,18 @@ const writeServiceWorkerUsingDefaultTemplate =
* @memberof module:workbox-build
*/
async function generateSW(config) {
// This check needs to be done before validation, since the deprecated options
// will be renamed.
const deprecationWarnings = checkForDeprecatedOptions(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;
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);
Expand All @@ -70,6 +76,9 @@ async function generateSW(config) {
manifestEntries,
}, options));

// Add in any deprecation warnings.
warnings.push(...deprecationWarnings);

return {count, size, warnings};
}

Expand Down
10 changes: 10 additions & 0 deletions packages/workbox-build/src/entry-points/get-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
https://opensource.org/licenses/MIT.
*/

const checkForDeprecatedOptions =
require('../lib/check-for-deprecated-options');
const getFileManifestEntries = require('../lib/get-file-manifest-entries');
const getManifestSchema = require('./options/get-manifest-schema');
const validate = require('./options/validate');
Expand All @@ -28,10 +30,18 @@ const validate = require('./options/validate');
* @memberof module:workbox-build
*/
async function getManifest(config) {
// This check needs to be done before validation, since the deprecated options
// will be renamed.
const deprecationWarnings = checkForDeprecatedOptions(config);

const options = validate(config, getManifestSchema);

const {manifestEntries, count, size, warnings} =
await getFileManifestEntries(options);

// Add in any deprecation warnings.
warnings.push(...deprecationWarnings);

return {manifestEntries, count, size, warnings};
}

Expand Down
9 changes: 9 additions & 0 deletions packages/workbox-build/src/entry-points/inject-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const assert = require('assert');
const fse = require('fs-extra');
const path = require('path');

const checkForDeprecatedOptions =
require('../lib/check-for-deprecated-options');
const defaults = require('./options/defaults');
const errors = require('../lib/errors');
const getFileManifestEntries = require('../lib/get-file-manifest-entries');
Expand Down Expand Up @@ -38,6 +40,10 @@ const validate = require('./options/validate');
* @memberof module:workbox-build
*/
async function injectManifest(config) {
// This check needs to be done before validation, since the deprecated options
// will be renamed.
const deprecationWarnings = checkForDeprecatedOptions(config);

const options = validate(config, injectManifestSchema);

if (path.normalize(config.swSrc) === path.normalize(config.swDest)) {
Expand Down Expand Up @@ -79,6 +85,9 @@ async function injectManifest(config) {

await fse.writeFile(config.swDest, swFileContents);

// Add in any deprecation warnings.
warnings.push(...deprecationWarnings);

return {count, size, warnings};
}

Expand Down
17 changes: 13 additions & 4 deletions packages/workbox-build/src/entry-points/options/base-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,26 @@ const regExpObject = require('./reg-exp-object');

// Define some common constrains used by all methods.
module.exports = joi.object().keys({
dontCacheBustUrlsMatching: regExpObject,
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
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(/./,
templatedURLs: joi.object().pattern(/./,
[joi.string(), joi.array().items(joi.string())]),
}).rename('dontCacheBustUrlsMatching', 'dontCacheBustURLsMatching', {
ignoreUndefined: true,
override: true,
}).rename('modifyUrlPrefix', 'modifyURLPrefix', {
ignoreUndefined: true,
override: true,
}).rename('templatedUrls', 'templatedURLs', {
ignoreUndefined: true,
override: true,
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = baseSchema.keys({
cacheId: joi.string(),
clientsClaim: joi.boolean().default(defaults.clientsClaim),
directoryIndex: joi.string(),
ignoreUrlParametersMatching: joi.array().items(regExpObject),
ignoreURLParametersMatching: joi.array().items(regExpObject),
navigateFallback: joi.string().default(defaults.navigateFallback),
navigateFallbackBlacklist: joi.array().items(regExpObject),
navigateFallbackWhitelist: joi.array().items(regExpObject),
Expand Down Expand Up @@ -66,4 +66,7 @@ module.exports = baseSchema.keys({
}).with('expiration', 'cacheName'),
}).requiredKeys('urlPattern', 'handler')),
skipWaiting: joi.boolean().default(defaults.skipWaiting),
}).rename('ignoreUrlParametersMatching', 'ignoreURLParametersMatching', {
ignoreUndefined: true,
override: true,
});
6 changes: 3 additions & 3 deletions packages/workbox-build/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const generateSW = require('./entry-points/generate-sw');
const generateSWString = require('./entry-points/generate-sw-string');
const getManifest = require('./entry-points/get-manifest');
const injectManifest = require('./entry-points/inject-manifest');
const {getModuleUrl} = require('./lib/cdn-utils');
const {getModuleURL} = require('./lib/cdn-utils');

/**
* This Node module can be used to generate a list of assets that should be
Expand All @@ -36,7 +36,7 @@ const {getModuleUrl} = require('./lib/cdn-utils');
* service worker file and are okay with including the manifest yourself.
* See [getManifest()]{@link module:workbox-build.getManifest}.
*
* @property {Array<RegExp>} [ignoreUrlParametersMatching=[/^utm_/]] Any
* @property {Array<RegExp>} [ignoreURLParametersMatching=[/^utm_/]] Any
* search parameter names that match against one of the regex's in this array
* will be removed before looking for a precache match.
*
Expand Down Expand Up @@ -75,6 +75,6 @@ module.exports = {
generateSW,
generateSWString,
getManifest,
getModuleUrl,
getModuleURL,
injectManifest,
};
10 changes: 5 additions & 5 deletions packages/workbox-build/src/lib/cdn-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ const getCDNOrigin = () => {
return `${cdn.origin}/${cdn.bucketName}/${cdn.releasesDir}`;
};

const getVersionedCDNUrl = () => {
const getVersionedCDNURL = () => {
return `${getCDNOrigin()}/${cdn.latestVersion}`;
};

const getModuleUrl = (moduleName, buildType) => {
const getModuleURL = (moduleName, buildType) => {
assert(moduleName, errors['no-module-name']);

if (buildType) {
Expand All @@ -29,12 +29,12 @@ const getModuleUrl = (moduleName, buildType) => {
// without creating an entry in errors.js.
throw Error(`The 'dev' build of ${moduleName} is not available.`);
}
return `${getVersionedCDNUrl()}/${moduleName}.${buildType.slice(0, 4)}.js`;
return `${getVersionedCDNURL()}/${moduleName}.${buildType.slice(0, 4)}.js`;
}
return `${getVersionedCDNUrl()}/${moduleName}.js`;
return `${getVersionedCDNURL()}/${moduleName}.js`;
};

module.exports = {
getCDNOrigin,
getModuleUrl,
getModuleURL,
};
26 changes: 26 additions & 0 deletions packages/workbox-build/src/lib/check-for-deprecated-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2019 Google LLC

Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

const ol = require('common-tags').oneLine;

const oldToNewOptionNames = {
dontCacheBustUrlsMatching: 'dontCacheBustURLsMatching',
ignoreUrlParametersMatching: 'ignoreURLParametersMatching',
modifyUrlPrefix: 'modifyURLPrefix',
templatedUrls: 'templatedURLs',
};

module.exports = (options) => {
return Object.entries(oldToNewOptionNames).map(([oldOption, newOption]) => {
if (oldOption in options) {
return ol`The '${oldOption}' option is deprecated and will be removed in a
future release of Workbox. Please update your config to use '${newOption}'
instead.`;
}
}).filter((item) => item);
};
16 changes: 8 additions & 8 deletions packages/workbox-build/src/lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = {
'invalid-generate-sw-input': ol`The input to generateSW() must be an object.`,
'invalid-glob-directory': ol`The supplied globDirectory must be a path as a
string.`,
'invalid-dont-cache-bust': ol`The supplied 'dontCacheBustUrlsMatching'
'invalid-dont-cache-bust': ol`The supplied 'dontCacheBustURLsMatching'
parameter must be a RegExp.`,
'invalid-exclude-files': 'The excluded files should be an array of strings.',
'invalid-get-manifest-entries-input': ol`The input to
Expand All @@ -55,15 +55,15 @@ module.exports = {
generateFileManifest() must be either 'iife' (the default) or 'es'.`,
'invalid-static-file-globs': ol`The 'globPatterns' value must be an array
of strings.`,
'invalid-templated-urls': ol`The 'templatedUrls' value should be an object
'invalid-templated-urls': ol`The 'templatedURLs' value should be an object
that maps URLs to either a string, or to an array of glob patterns.`,
'templated-url-matches-glob': ol`One of the 'templatedUrls' URLs is already
'templated-url-matches-glob': ol`One of the 'templatedURLs' URLs is already
being tracked via 'globPatterns': `,
'invalid-glob-ignores': ol`The 'globIgnores' parameter must be an array of
glob pattern strings.`,
'manifest-entry-bad-url': ol`The generated manifest contains an entry without
a URL string. This is likely an error with workbox-build.`,
'modify-url-prefix-bad-prefixes': ol`The 'modifyUrlPrefix' parameter must be
'modify-url-prefix-bad-prefixes': ol`The 'modifyURLPrefix' parameter must be
an object with string key value pairs.`,
'invalid-inject-manifest-arg': ol`The input to 'injectManifest()' must be an
object.`,
Expand All @@ -76,13 +76,13 @@ module.exports = {
'useless-glob-pattern': ol`One of the glob patterns doesn't match any files.
Please remove or fix the following: `,
'bad-template-urls-asset': ol`There was an issue using one of the provided
'templatedUrls'.`,
'templatedURLs'.`,
'invalid-runtime-caching': ol`The 'runtimeCaching' parameter must an an
array of objects with at least a 'urlPattern' and 'handler'.`,
'static-file-globs-deprecated': ol`'staticFileGlobs' is deprecated.
Please use 'globPatterns' instead.`,
'dynamic-url-deprecated': ol`'dynamicUrlToDependencies' is deprecated.
Please use 'templatedUrls' instead.`,
'dynamic-url-deprecated': ol`'dynamicURLToDependencies' is deprecated.
Please use 'templatedURLs' instead.`,
'urlPattern-is-required': ol`The 'urlPattern' option is required when using
'runtimeCaching'.`,
'handler-is-required': ol`The 'handler' option is required when using
Expand All @@ -100,5 +100,5 @@ module.exports = {
'invalid-network-timeout-seconds': ol`When using networkTimeoutSeconds, you
must set the handler to 'networkFirst'.`,
'no-module-name': ol`You must provide a moduleName parameter when calling
getModuleUrl().`,
getModuleURL().`,
};
16 changes: 8 additions & 8 deletions packages/workbox-build/src/lib/filter-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
*/

const maximumSizeTransform = require('./maximum-size-transform');
const modifyUrlPrefixTranform = require('./modify-url-prefix-transform');
const noRevisionForUrlsMatchingTransform =
const modifyURLPrefixTranform = require('./modify-url-prefix-transform');
const noRevisionForURLsMatchingTransform =
require('./no-revision-for-urls-matching-transform');

/**
Expand Down Expand Up @@ -62,11 +62,11 @@ const noRevisionForUrlsMatchingTransform =
*/

module.exports = ({
dontCacheBustUrlsMatching,
dontCacheBustURLsMatching,
fileDetails,
manifestTransforms,
maximumFileSizeToCacheInBytes,
modifyUrlPrefix,
modifyURLPrefix,
}) => {
let allWarnings = [];

Expand All @@ -86,13 +86,13 @@ module.exports = ({
transformsToApply.push(maximumSizeTransform(maximumFileSizeToCacheInBytes));
}

if (modifyUrlPrefix) {
transformsToApply.push(modifyUrlPrefixTranform(modifyUrlPrefix));
if (modifyURLPrefix) {
transformsToApply.push(modifyURLPrefixTranform(modifyURLPrefix));
}

if (dontCacheBustUrlsMatching) {
if (dontCacheBustURLsMatching) {
transformsToApply.push(
noRevisionForUrlsMatchingTransform(dontCacheBustUrlsMatching));
noRevisionForURLsMatchingTransform(dontCacheBustURLsMatching));
}

// Any additional manifestTransforms that were passed will be applied last.
Expand Down
4 changes: 2 additions & 2 deletions packages/workbox-build/src/lib/get-composite-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

const crypto = require('crypto');

module.exports = (compositeUrl, dependencyDetails) => {
module.exports = (compositeURL, dependencyDetails) => {
let totalSize = 0;
let compositeHash = '';

Expand All @@ -22,7 +22,7 @@ module.exports = (compositeUrl, dependencyDetails) => {
const hashOfHashes = md5.digest('hex');

return {
file: compositeUrl,
file: compositeURL,
hash: hashOfHashes,
size: totalSize,
};
Expand Down
Loading