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

Allow injecting manifests into existing webpack-compiled assets #1765

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/workbox-webpack-plugin/package.json
Expand Up @@ -30,6 +30,7 @@
"dependencies": {
"babel-runtime": "^6.26.0",
"json-stable-stringify": "^1.0.1",
"webpack-sources": "^1.3.0",
"workbox-build": "^3.6.3"
},
"peerDependencies": {
Expand Down
61 changes: 40 additions & 21 deletions packages/workbox-webpack-plugin/src/inject-manifest.js
Expand Up @@ -17,6 +17,7 @@
const assert = require('assert');
const path = require('path');
const {getManifest} = require('workbox-build');
const {ConcatSource} = require('webpack-sources');

const convertStringToAsset = require('./lib/convert-string-to-asset');
const getDefaultConfig = require('./lib/get-default-config');
Expand Down Expand Up @@ -51,13 +52,16 @@ class InjectManifest {
* for all supported options and defaults.
*/
constructor(config = {}) {
assert(typeof config.swSrc === 'string', `swSrc must be set to the path ` +
`to an existing service worker file.`);
assert(typeof config.swSrc === 'string' ||
typeof config.swDest === 'string',
`Either swSrc or swDest should be set to the path ` +
`of an existing service worker file.`);

this.config = Object.assign(getDefaultConfig(), {
// Default to using the same filename as the swSrc file, since that's
// provided here. (In GenerateSW, that's not available.)
swDest: path.basename(config.swSrc),
// If swDest is not set, default to using the same filename
// as the swSrc file, since that's provided here. (In GenerateSW,
// that's not available.)
swDest: config.swDest || path.basename(config.swSrc),
}, config);
}

Expand Down Expand Up @@ -119,20 +123,6 @@ class InjectManifest {
importScriptsArray.push(...workboxSWImports);
}

const originalSWString = await readFileWrapper(readFile, this.config.swSrc);

// compilation.fileDependencies needs absolute paths.
const absoluteSwSrc = path.resolve(this.config.swSrc);
if (Array.isArray(compilation.fileDependencies)) {
// webpack v3
if (compilation.fileDependencies.indexOf(absoluteSwSrc) === -1) {
compilation.fileDependencies.push(absoluteSwSrc);
}
} else if ('add' in compilation.fileDependencies) {
// webpack v4; no need to check for membership first, since it's a Set.
compilation.fileDependencies.add(absoluteSwSrc);
}

const importScriptsString = importScriptsArray
.map(JSON.stringify)
.join(', ');
Expand All @@ -144,11 +134,40 @@ class InjectManifest {

const postInjectionSWString = `importScripts(${importScriptsString});
${setConfigString}
${originalSWString}
`;

const relSwDest = relativeToOutputPath(compilation, this.config.swDest);
compilation.assets[relSwDest] = convertStringToAsset(postInjectionSWString);

if (this.config.swSrc) {
// We're adding a new source file to the build:
const originalSWString = await readFileWrapper(
readFile, this.config.swSrc);

// compilation.fileDependencies needs absolute paths.
const absoluteSwSrc = path.resolve(this.config.swSrc);
if (Array.isArray(compilation.fileDependencies)) {
// webpack v3
if (compilation.fileDependencies.indexOf(absoluteSwSrc) === -1) {
compilation.fileDependencies.push(absoluteSwSrc);
}
} else if ('add' in compilation.fileDependencies) {
// webpack v4; no need to check for membership first, since it's a Set.
compilation.fileDependencies.add(absoluteSwSrc);
}
compilation.assets[relSwDest] = convertStringToAsset(
postInjectionSWString + originalSWString
);
} else {
assert(!!compilation.assets[relSwDest],
`If swSrc is not set, swDest must be the path ` +
`of an existing output file to update`);

// We're transforming an existing source file:
compilation.assets[relSwDest] = new ConcatSource(
postInjectionSWString,
compilation.assets[relSwDest]
);
}
}

/**
Expand Down