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
Merged

Conversation

prateekbh
Copy link
Collaborator

  • Adds a capability to specify swSrc as a webpack asset filename
Copy link
Contributor

@jeffposnick jeffposnick left a comment

Choose a reason for hiding this comment

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

Hardcoding 'sw.js' is not a good idea, as plenty of folks use alternative names for their service worker file. You'd need to take this in as a config parameter.

...and since you need a config parameter, maybe it makes sense to reuse swSrc for this purpose? You can have a check like:

let originalSWString;
if (this.config.swSrc in compilation.assets) {
  originalSWString = compilation.assets[this.config.swSrc].source();
} else {
  originalSWString = await readFileWrapper(readFile, this.config.swSrc);
}

Of course, that's basically magic overloading of the existing option, and maybe it doesn't feel very webpacky. In which case, I think we'd need a new config option.

@prateekbh
Copy link
Collaborator Author

woops sry, I copied it from the project which caused this feature request, but I feel this.swSrc should be fine as it can be a filepath or the filename. Do you think the overload will be too confusing?

@prateekbh
Copy link
Collaborator Author

I am closing this as swSrc should never be a webpack asset. The compilation generating the assets will mostly have webpack target as "window" but the any service worker file being produced needs to have target as worker.

Since the service worker and rest of the assets cannot share the same compilation, the plugin can only have access to either the list of assets or the service worker file.

@prateekbh prateekbh closed this Nov 25, 2018
@prateekbh prateekbh reopened this Dec 19, 2018
@prateekbh
Copy link
Collaborator Author

Sorry, I did discover a valid use case for this now: "childCompiler"

Since a child compiler can generate swSrc in the same compilation with a different target, hence this change will be required

@jeffposnick
Copy link
Contributor

I'd love to get @developit's take on how this fits in with the other things he's looking into.

@prateekbh
Copy link
Collaborator Author

@developit bumping this thread for your review, as the release of this feature is blocking integration in preact-cli. Which btw also needs to be reviewed by you 😂

@philipwalton
Copy link
Member

@developit, can you take a look at this PR and let us know if it conflicts with any of your future plans for the webpack plugin.

This is blocking the preact cli integration, but I wouldn't want to merge this only to change the behavior in v5.

Copy link

@developit developit left a comment

Choose a reason for hiding this comment

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

IMO this is a good step towards the approach I've been pondering, and that I believe Prateek has been playing with as well.

@developit
Copy link

Honestly what @prateekbh has set up in his Preact CLI PR is super close to what was in my head. The only difference would be that I'd like to bundle via Webpack and import workbox as modules, which I think we can still add independently in v5?

@prateekbh do you think it would be prohibitive to require that a custom Service Worker defined via swSrc import workbox rather than relying on it being global?

@philipwalton
Copy link
Member

@prateekbh do you think it would be prohibitive to require that a custom Service Worker defined via swSrc import workbox rather than relying on it being global?

Or perhaps (whether in this version or a future version), there could be an option to use either import or importScripts()?

@developit
Copy link

@philipwalton yup. writing up an RFC right now.

@prateekbh
Copy link
Collaborator Author

@developit is there a good reason to make it compulsory to not use a global workbox via importScript?

@developit
Copy link

Nope, I meant leave it as an exercise for the developer. They can importScripts().

@prateekbh
Copy link
Collaborator Author

I guess it'll be somewhat restrictive to say that in order to use swSrc you can only import Workbox module and not use importScripts

@developit
Copy link

developit commented Jan 16, 2019

It'd also be a little counterintuitive, since Service Worker doesn't natively support Modules, but does support importScripts(). I am a fan of adding "futurey" Modules support, but it seems worth keeping the option to use current stuff there too.

that is to say, support/allow both of these:

import workbox from 'workbox';
workbox.precaching.precacheAndRoute(self.__precacheManifest);

or:

importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.6.1/workbox-sw.js');
workbox.precaching.precacheAndRoute(self.__precacheManifest);
Copy link
Contributor

@jeffposnick jeffposnick left a comment

Choose a reason for hiding this comment

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

* 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..

@prateekbh
Copy link
Collaborator Author

@jeffposnick added the tests

@workbox-pr-bot
Copy link
Collaborator

PR-Bot Size Plugin

Changed File Sizes

File Before After Change GZipped
packages/workbox-background-sync/build/workbox-background-sync.prod.js 3.51 KB 3.59 KB +2% 1.56 KB
packages/workbox-broadcast-cache-update/build/workbox-broadcast-cache-update.prod.js 1.12 KB 1.88 KB +68% 940 B ☠️
packages/workbox-build/build/index.js 4.02 KB 3.64 KB -9% 1.36 KB
packages/workbox-cache-expiration/build/workbox-cache-expiration.prod.js 3.88 KB 3.14 KB -19% 1.28 KB 🎉
packages/workbox-cli/build/app.js 6.76 KB 5.58 KB -17% 1.98 KB 🎉
packages/workbox-cli/build/bin.js 2.32 KB 1.16 KB -50% 580 B 🎉
packages/workbox-core/build/workbox-core.prod.js 7.47 KB 5.51 KB -26% 2.39 KB 🎉
packages/workbox-precaching/build/workbox-precaching.prod.js 5.80 KB 4.20 KB -28% 1.68 KB 🎉
packages/workbox-range-requests/build/workbox-range-requests.prod.js 1.63 KB 1.52 KB -7% 763 B
packages/workbox-routing/build/workbox-routing.prod.js 2.87 KB 3.21 KB +12% 1.41 KB ☠️
packages/workbox-strategies/build/workbox-strategies.prod.js 5.09 KB 4.87 KB -4% 1.19 KB
packages/workbox-streams/build/workbox-streams.prod.js 1.57 KB 1.38 KB -12% 684 B 🎉
packages/workbox-sw/build/workbox-sw.js 1.50 KB 1.36 KB -10% 750 B
packages/workbox-webpack-plugin/build/generate-sw.js 8.04 KB 5.29 KB -34% 1.84 KB 🎉
packages/workbox-webpack-plugin/build/index.js 742 B 349 B -53% 255 B 🎉
packages/workbox-webpack-plugin/build/inject-manifest.js 10.30 KB 7.22 KB -30% 2.48 KB 🎉

New Files

File Size GZipped
packages/workbox-google-analytics/build/workbox-offline-ga.prod.js 1.73 KB 876 B

All File Sizes

View Table
File Before After Change GZipped
packages/workbox-background-sync/build/workbox-background-sync.prod.js 3.51 KB 3.59 KB +2% 1.56 KB
packages/workbox-broadcast-cache-update/build/workbox-broadcast-cache-update.prod.js 1.12 KB 1.88 KB +68% 940 B ☠️
packages/workbox-build/build/_types.js 41 B 41 B 0% 61 B
packages/workbox-build/build/index.js 4.02 KB 3.64 KB -9% 1.36 KB
packages/workbox-cache-expiration/build/workbox-cache-expiration.prod.js 3.88 KB 3.14 KB -19% 1.28 KB 🎉
packages/workbox-cacheable-response/build/workbox-cacheable-response.prod.js 587 B 587 B 0% 350 B
packages/workbox-cli/build/app.js 6.76 KB 5.58 KB -17% 1.98 KB 🎉
packages/workbox-cli/build/bin.js 2.32 KB 1.16 KB -50% 580 B 🎉
packages/workbox-core/build/workbox-core.prod.js 7.47 KB 5.51 KB -26% 2.39 KB 🎉
packages/workbox-google-analytics/build/workbox-offline-ga.prod.js 1.73 KB 876 B
packages/workbox-navigation-preload/build/workbox-navigation-preload.prod.js 660 B 660 B 0% 323 B
packages/workbox-precaching/build/workbox-precaching.prod.js 5.80 KB 4.20 KB -28% 1.68 KB 🎉
packages/workbox-range-requests/build/workbox-range-requests.prod.js 1.63 KB 1.52 KB -7% 763 B
packages/workbox-routing/build/workbox-routing.prod.js 2.87 KB 3.21 KB +12% 1.41 KB ☠️
packages/workbox-strategies/build/workbox-strategies.prod.js 5.09 KB 4.87 KB -4% 1.19 KB
packages/workbox-streams/build/workbox-streams.prod.js 1.57 KB 1.38 KB -12% 684 B 🎉
packages/workbox-sw/build/workbox-sw.js 1.50 KB 1.36 KB -10% 750 B
packages/workbox-webpack-plugin/build/generate-sw.js 8.04 KB 5.29 KB -34% 1.84 KB 🎉
packages/workbox-webpack-plugin/build/index.js 742 B 349 B -53% 255 B 🎉
packages/workbox-webpack-plugin/build/inject-manifest.js 10.30 KB 7.22 KB -30% 2.48 KB 🎉

Workbox Aggregate Size Plugin

8.84KB gzip'ed (59% of limit)
22.33KB uncompressed

@jeffposnick jeffposnick merged commit c8b6566 into next Jan 18, 2019
@jeffposnick jeffposnick deleted the src-as-webpack-asset branch January 18, 2019 19:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
5 participants