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

adding swSrc as webpack asset capability #1763

Merged
merged 8 commits into from
Jan 18, 2019
18 changes: 17 additions & 1 deletion packages/workbox-webpack-plugin/src/inject-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,16 @@ class InjectManifest {
importScriptsArray.push(...workboxSWImports);
}

const originalSWString = await readFileWrapper(readFile, this.config.swSrc);
let originalSWString;
/**
* Check if the mentioned file name is in the webpack assets itself
* or fallback to filesystem.
*/
if (compilation.assets['sw.js']) {
originalSWString = compilation.assets['sw.js'].source();
} else {
originalSWString = await readFileWrapper(readFile, this.config.swSrc);
}

// compilation.fileDependencies needs absolute paths.
const absoluteSwSrc = path.resolve(this.config.swSrc);
Expand Down Expand Up @@ -140,6 +149,13 @@ ${originalSWString}
`;

const relSwDest = relativeToOutputPath(compilation, this.config.swDest);
/**
* Delete if this file already exist,
* this is the case where source is also a webpack asset.
*/
if (compilation.assets[relSwDest]) {
delete compilation.assets[relSwDest];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 159 assigns a value to compilation.assets[relSwDest], which should overwrite anything that's already there, so I'm not clear on what this delete accomplishes. Can you clarify?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw that simply overriding was somehow not overriding the content, so I added an additional delete statement which did the trick

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That feels kind of magic. @developit, have you ever seen anything like that?

(Also, I'm not sure if there's a reason to wrap this in an if(), since the delete will be a no-op if the property doesn't exist.)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird, I've never had to do this. @prateekbh any chance it was some other issue? I can't think of why this would happen.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll re-check

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well yeah it works now.. dont know what was the problem earlier..

}
compilation.assets[relSwDest] = convertStringToAsset(postInjectionSWString);
}

Expand Down