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

Add a new workboxSWImport option to the template. #1327

Merged
merged 2 commits into from
Feb 22, 2018
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
11 changes: 2 additions & 9 deletions packages/workbox-build/src/entry-points/generate-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ async function generateSW(config) {
// Do nothing if importWorkboxFrom is set to 'disabled'. Otherwise, check:
if (options.importWorkboxFrom === 'cdn') {
const cdnUrl = cdnUtils.getModuleUrl('workbox-sw');
// importScripts may or may not already be an array containing other URLs.
// Either way, list cdnUrl first.
options.importScripts = [cdnUrl].concat(options.importScripts || []);
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 @@ -67,12 +65,7 @@ async function generateSW(config) {
const workboxSWPkg = require(`workbox-sw/package.json`);
const workboxSWFilename = path.basename(workboxSWPkg.main);

// importScripts may or may not already be an array containing other URLs.
// Either way, list workboxSWFilename first.
options.importScripts = [
`${workboxDirectoryName}/${workboxSWFilename}`,
].concat(options.importScripts || []);

options.workboxSWImport = `${workboxDirectoryName}/${workboxSWFilename}`;
options.modulePathPrefix = workboxDirectoryName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ module.exports = commonGenerateSchema.keys({
globDirectory: joi.string(),
importScripts: joi.array().items(joi.string()).required(),
modulePathPrefix: joi.string(),
workboxSWImport: joi.string(),
});
2 changes: 2 additions & 0 deletions packages/workbox-build/src/lib/populate-sw-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = ({
navigateFallbackWhitelist,
runtimeCaching,
skipWaiting,
workboxSWImport,
}) => {
// These are all options that can be passed to the precacheAndRoute() method.
const precacheOptions = {
Expand Down Expand Up @@ -66,6 +67,7 @@ module.exports = ({
precacheOptionsString,
skipWaiting,
runtimeCaching: runtimeCachingConverter(runtimeCaching),
workboxSWImport,
});

// Clean up multiple blank lines.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = async ({
runtimeCaching,
skipWaiting,
swDest,
workboxSWImport,
}) => {
try {
await fse.mkdirp(path.dirname(swDest));
Expand All @@ -57,6 +58,7 @@ module.exports = async ({
navigateFallbackWhitelist,
runtimeCaching,
skipWaiting,
workboxSWImport,
});

try {
Expand Down
5 changes: 4 additions & 1 deletion packages/workbox-build/src/templates/sw-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ module.exports = `/**
* See https://goo.gl/2aRDsh
*/

<% if (workboxSWImport) { %>
importScripts(<%= JSON.stringify(workboxSWImport) %>);
<% if (modulePathPrefix) { %>workbox.setConfig({modulePathPrefix: <%= JSON.stringify(modulePathPrefix) %>});<% } %>
<% } %>
<% if (importScripts) { %>
importScripts(
<%= importScripts.map(JSON.stringify).join(',\\n ') %>
);
<% } %>

<% if (modulePathPrefix) { %>workbox.setConfig({modulePathPrefix: <%= JSON.stringify(modulePathPrefix) %>});<% } %>
<% if (cacheId) { %>workbox.core.setCacheNameDetails({prefix: <%= JSON.stringify(cacheId) %>});<% } %>

<% if (skipWaiting) { %>workbox.skipWaiting();<% } %>
Expand Down
14 changes: 13 additions & 1 deletion packages/workbox-webpack-plugin/src/generate-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,27 @@ class GenerateSW {
(compilation.options.output.publicPath || '') + manifestFilename);

// workboxSWImports might be null if importWorkboxFrom is 'disabled'.
let workboxSWImport;
if (workboxSWImports) {
importScriptsArray.push(...workboxSWImports);
if (workboxSWImports.length === 1) {
// When importWorkboxFrom is 'cdn' or 'local', or a chunk name
// that only contains one JavaScript asset, then this will be a one
// element array, containing just the Workbox SW code.
workboxSWImport = workboxSWImports[0];
} else {
// If importWorkboxFrom was a chunk name that contained multiple
// JavaScript assets, then we don't know which contains the Workbox SW
// code. Just import them first as part of the "main" importScripts().
importScriptsArray.unshift(...workboxSWImports);
}
}

const sanitizedConfig = sanitizeConfig.forGenerateSWString(this.config);
// If globPatterns isn't explicitly set, then default to [], instead of
// the workbox-build.generateSWString() default.
sanitizedConfig.globPatterns = sanitizedConfig.globPatterns || [];
sanitizedConfig.importScripts = importScriptsArray;
sanitizedConfig.workboxSWImport = workboxSWImport;
const serviceWorker = await generateSWString(sanitizedConfig);
compilation.assets[this.config.swDest] =
convertStringToAsset(serviceWorker);
Expand Down
2 changes: 1 addition & 1 deletion test/workbox-build/node/entry-points/generate-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ describe(`[workbox-build] entry-points/generate-sw.js (End to End)`, function()
expect(count).to.eql(6);
expect(size).to.eql(2421);
await validateServiceWorkerRuntime({swFile: swDest, expectedMethodCalls: {
importScripts: [[WORKBOX_SW_CDN_URL, ...importScripts]],
importScripts: [[WORKBOX_SW_CDN_URL], [...importScripts]],
suppressWarnings: [[]],
precacheAndRoute: [[[{
url: 'index.html',
Expand Down
4 changes: 4 additions & 0 deletions test/workbox-build/node/lib/populate-sw-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ describe(`[workbox-build] lib/populate-sw-template.js`, function() {
precacheOptionsString,
runtimeCaching: runtimeCachingPlaceholder,
skipWaiting: undefined,
workboxSWImport: undefined,
}]);
});

Expand All @@ -70,6 +71,7 @@ describe(`[workbox-build] lib/populate-sw-template.js`, function() {
const skipWaiting = true;
const swTemplate = 'template';
const precacheOptionsString = '{\n "directoryIndex": "index.html",\n "ignoreUrlParametersMatching": [/a/, /b/]\n}';
const workboxSWImport = 'workbox-sw.js';

// There are two stages in templating: creating the active template function
// from an initial string, and passing variables to that template function
Expand Down Expand Up @@ -97,6 +99,7 @@ describe(`[workbox-build] lib/populate-sw-template.js`, function() {
navigateFallbackWhitelist,
runtimeCaching,
skipWaiting,
workboxSWImport,
});

expect(templateCreationStub.alwaysCalledWith(swTemplate)).to.be.true;
Expand All @@ -112,6 +115,7 @@ describe(`[workbox-build] lib/populate-sw-template.js`, function() {
runtimeCaching: runtimeCachingPlaceholder,
precacheOptionsString,
skipWaiting,
workboxSWImport,
}]);
});
});
120 changes: 60 additions & 60 deletions test/workbox-webpack-plugin/node/generate-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ describe(`[workbox-webpack-plugin] GenerateSW (End to End)`, function() {
try {
// First, validate that the generated service-worker.js meets some basic assumptions.
await validateServiceWorkerRuntime({swFile, expectedMethodCalls: {
importScripts: [[
FILE_MANIFEST_NAME,
WORKBOX_SW_FILE_NAME,
]],
importScripts: [
[WORKBOX_SW_FILE_NAME],
[FILE_MANIFEST_NAME],
],
suppressWarnings: [[]],
precacheAndRoute: [[[], {}]],
}});
Expand Down Expand Up @@ -188,10 +188,10 @@ describe(`[workbox-webpack-plugin] GenerateSW (End to End)`, function() {
try {
// First, validate that the generated service-worker.js meets some basic assumptions.
await validateServiceWorkerRuntime({swFile, expectedMethodCalls: {
importScripts: [[
FILE_MANIFEST_NAME,
WORKBOX_SW_FILE_NAME,
]],
importScripts: [
[WORKBOX_SW_FILE_NAME],
[FILE_MANIFEST_NAME],
],
suppressWarnings: [[]],
precacheAndRoute: [[[], {}]],
}});
Expand Down Expand Up @@ -245,10 +245,10 @@ describe(`[workbox-webpack-plugin] GenerateSW (End to End)`, function() {
try {
// First, validate that the generated service-worker.js meets some basic assumptions.
await validateServiceWorkerRuntime({swFile, expectedMethodCalls: {
importScripts: [[
FILE_MANIFEST_NAME,
workboxEntryName,
]],
importScripts: [
[workboxEntryName],
[FILE_MANIFEST_NAME],
],
suppressWarnings: [[]],
precacheAndRoute: [[[], {}]],
}});
Expand Down Expand Up @@ -316,10 +316,10 @@ describe(`[workbox-webpack-plugin] GenerateSW (End to End)`, function() {

// First, validate that the generated service-worker.js meets some basic assumptions.
await validateServiceWorkerRuntime({swFile, expectedMethodCalls: {
importScripts: [[
FILE_MANIFEST_NAME,
workboxSWImport,
]],
importScripts: [
[workboxSWImport],
[FILE_MANIFEST_NAME],
],
setConfig: [[{modulePathPrefix}]],
suppressWarnings: [[]],
precacheAndRoute: [[[], {}]],
Expand Down Expand Up @@ -391,10 +391,10 @@ describe(`[workbox-webpack-plugin] GenerateSW (End to End)`, function() {

// First, validate that the generated service-worker.js meets some basic assumptions.
await validateServiceWorkerRuntime({swFile, expectedMethodCalls: {
importScripts: [[
publicPath + FILE_MANIFEST_NAME,
publicPath + workboxSWImport,
]],
importScripts: [
[publicPath + workboxSWImport],
[publicPath + FILE_MANIFEST_NAME],
],
setConfig: [[{modulePathPrefix}]],
suppressWarnings: [[]],
precacheAndRoute: [[[], {}]],
Expand Down Expand Up @@ -450,10 +450,10 @@ describe(`[workbox-webpack-plugin] GenerateSW (End to End)`, function() {
try {
// First, validate that the generated service-worker.js meets some basic assumptions.
await validateServiceWorkerRuntime({swFile, expectedMethodCalls: {
importScripts: [[
FILE_MANIFEST_NAME,
WORKBOX_SW_FILE_NAME,
]],
importScripts: [
[WORKBOX_SW_FILE_NAME],
[FILE_MANIFEST_NAME],
],
suppressWarnings: [[]],
precacheAndRoute: [[[], {}]],
}});
Expand Down Expand Up @@ -508,10 +508,10 @@ describe(`[workbox-webpack-plugin] GenerateSW (End to End)`, function() {
try {
// First, validate that the generated service-worker.js meets some basic assumptions.
await validateServiceWorkerRuntime({swFile, expectedMethodCalls: {
importScripts: [[
FILE_MANIFEST_NAME,
WORKBOX_SW_FILE_NAME,
]],
importScripts: [
[WORKBOX_SW_FILE_NAME],
[FILE_MANIFEST_NAME],
],
suppressWarnings: [[]],
precacheAndRoute: [[[], {}]],
}});
Expand Down Expand Up @@ -567,10 +567,10 @@ describe(`[workbox-webpack-plugin] GenerateSW (End to End)`, function() {
try {
// First, validate that the generated service-worker.js meets some basic assumptions.
await validateServiceWorkerRuntime({swFile, expectedMethodCalls: {
importScripts: [[
FILE_MANIFEST_NAME,
WORKBOX_SW_FILE_NAME,
]],
importScripts: [
[WORKBOX_SW_FILE_NAME],
[FILE_MANIFEST_NAME],
],
suppressWarnings: [[]],
precacheAndRoute: [[[], {}]],
}});
Expand Down Expand Up @@ -630,10 +630,10 @@ describe(`[workbox-webpack-plugin] GenerateSW (End to End)`, function() {
try {
// First, validate that the generated service-worker.js meets some basic assumptions.
await validateServiceWorkerRuntime({swFile, expectedMethodCalls: {
importScripts: [[
FILE_MANIFEST_NAME,
WORKBOX_SW_FILE_NAME,
]],
importScripts: [
[WORKBOX_SW_FILE_NAME],
[FILE_MANIFEST_NAME],
],
clientsClaim: [[]],
skipWaiting: [[]],
suppressWarnings: [[]],
Expand Down Expand Up @@ -693,10 +693,10 @@ describe(`[workbox-webpack-plugin] GenerateSW (End to End)`, function() {
try {
// First, validate that the generated service-worker.js meets some basic assumptions.
await validateServiceWorkerRuntime({swFile, expectedMethodCalls: {
importScripts: [[
FILE_MANIFEST_NAME,
WORKBOX_SW_FILE_NAME,
]],
importScripts: [
[WORKBOX_SW_FILE_NAME],
[FILE_MANIFEST_NAME],
],
suppressWarnings: [[]],
precacheAndRoute: [[[], {}]],
}});
Expand Down Expand Up @@ -754,10 +754,10 @@ describe(`[workbox-webpack-plugin] GenerateSW (End to End)`, function() {
try {
// First, validate that the generated service-worker.js meets some basic assumptions.
await validateServiceWorkerRuntime({swFile, expectedMethodCalls: {
importScripts: [[
FILE_MANIFEST_NAME,
WORKBOX_SW_FILE_NAME,
]],
importScripts: [
[WORKBOX_SW_FILE_NAME],
[FILE_MANIFEST_NAME],
],
suppressWarnings: [[]],
precacheAndRoute: [[[], {}]],
}});
Expand Down Expand Up @@ -839,10 +839,10 @@ describe(`[workbox-webpack-plugin] GenerateSW (End to End)`, function() {
try {
// First, validate that the generated service-worker.js meets some basic assumptions.
await validateServiceWorkerRuntime({swFile, expectedMethodCalls: {
importScripts: [[
FILE_MANIFEST_NAME,
WORKBOX_SW_FILE_NAME,
]],
importScripts: [
[WORKBOX_SW_FILE_NAME],
[FILE_MANIFEST_NAME],
],
suppressWarnings: [[]],
precacheAndRoute: [[[], {}]],
}});
Expand Down Expand Up @@ -893,10 +893,10 @@ describe(`[workbox-webpack-plugin] GenerateSW (End to End)`, function() {
try {
// First, validate that the generated service-worker.js meets some basic assumptions.
await validateServiceWorkerRuntime({swFile, expectedMethodCalls: {
importScripts: [[
FILE_MANIFEST_NAME,
WORKBOX_SW_FILE_NAME,
]],
importScripts: [
[WORKBOX_SW_FILE_NAME],
[FILE_MANIFEST_NAME],
],
suppressWarnings: [[]],
precacheAndRoute: [[[], {}]],
}});
Expand Down Expand Up @@ -954,10 +954,10 @@ describe(`[workbox-webpack-plugin] GenerateSW (End to End)`, function() {
try {
// First, validate that the generated service-worker.js meets some basic assumptions.
await validateServiceWorkerRuntime({swFile, expectedMethodCalls: {
importScripts: [[
FILE_MANIFEST_NAME,
WORKBOX_SW_FILE_NAME,
]],
importScripts: [
[WORKBOX_SW_FILE_NAME],
[FILE_MANIFEST_NAME],
],
suppressWarnings: [[]],
precacheAndRoute: [[[], {}]],
}});
Expand Down Expand Up @@ -1019,10 +1019,10 @@ describe(`[workbox-webpack-plugin] GenerateSW (End to End)`, function() {
try {
// First, validate that the generated service-worker.js meets some basic assumptions.
await validateServiceWorkerRuntime({swFile, expectedMethodCalls: {
importScripts: [[
FILE_MANIFEST_NAME,
WORKBOX_SW_FILE_NAME,
]],
importScripts: [
[WORKBOX_SW_FILE_NAME],
[FILE_MANIFEST_NAME],
],
suppressWarnings: [[]],
precacheAndRoute: [[[], {}]],
}});
Expand Down