Skip to content

new Worker() support - #11439

Merged
sokra merged 2 commits into
masterfrom
feature/web-worker
Sep 8, 2020
Merged

sokra merged 2 commits into
masterfrom
feature/web-worker

Conversation

@sokra

@sokra sokra commented Sep 8, 2020

Copy link
Copy Markdown
Member
  • add support for async anonymous entrypoints from inside the codebase
  • add worker example
  • move entry options into Entrypoint and receive them from there

This adds support to create entrypoints from AsyncDependenciesBlocks. Might also be useful for other scenarios (@timneutkens interactive)

What kind of change does this PR introduce?
feature

Did you add tests for your changes?
yes

Does this PR introduce a breaking change?
yes

What needs to be documented once your changes are merged?

  • new option ouput.workerChunkLoading controls the chunk loading of workers (default chosen based on output.chunkLoading)
  • some syntax triggers webpack to compile "..." as new entrypoint with ouput.workerChunkLoading as chunk loading mechnism
    • new Worker(new URL("...", import.meta.url), ...) with optional import { Worker } from "worker_threads"
    • navigator.serviceWorker.register(new URL("...", import.meta.url), ...)

@webpack-bot

webpack-bot commented Sep 8, 2020 •

Copy link
Copy Markdown
Contributor

For maintainers only:

  • This needs to be documented (issue in webpack/webpack.js.org will be filed when merged)
  • This needs to be backported to webpack 4 (issue will be created when merged)

add support for async anonymous entrypoints from inside the codebase
add worker example
move entry options into Entrypoint and receive them from there
@webpack-bot

Copy link
Copy Markdown
Contributor

Thank you for your pull request! The most important CI builds succeeded, we’ll review the pull request soon.

@giuseppeg

Copy link
Copy Markdown

@sokra may I suggest to document the following too? How can you create those?

add support for async anonymous entrypoints from inside the codebase

@sokra

sokra commented Sep 8, 2020

Copy link
Copy Markdown
Member Author

add support for async anonymous entrypoints from inside the codebase

new Worker(new URL("...", import.meta.url), ...) is an async anonymous entrypoint.
It's async because it's not loaded initially on page load, but on request instead.
It's anonymous because it doesn't have a name. One could give it a name with /* webpackChunkName: "name" */ or { name: "name" } as options, then it's no longer anonymous.
And it's an entrypoint, because the worker execution starts here, means webpack uses a runtime in the resulting script.

@giuseppeg

giuseppeg commented Sep 8, 2020 •

Copy link
Copy Markdown

@sokra is Worker a Web Worker or a webpack thing? I guess I am interested in the following

This adds support to create entrypoints from AsyncDependenciesBlocks. Might also be useful for other scenarios (@timneutkens interactive)

How do I create an async entrypoint from code? Next.js' interactive won't offload scripts to a worker

@sokra

sokra commented Sep 8, 2020 •

Copy link
Copy Markdown
Member Author

@sokra is Worker a Web Worker or a webpack thing? I guess I am interested in the following

This adds support to create entrypoints from AsyncDependenciesBlocks. Might also be useful for other scenarios (@timneutkens interactive)

How do I create an async entrypoint from code? Next.js' interactive won't offload scripts to a worker

This is a WebWorker thing. That's not directly related to interactive. But some lower level features are interesting for that.

For new Worker we basically create a new entrypoint from the parser, without being listed in the entry option. This entrypoint is referenced from the generated code.

interactive can do the same. When parsed it can create a new entrypoint that handles the hydration of that interactive piece. In this case you don't have to reference the entrypoint from the generated code, but instead the server would handle that. When it has a name, it can be found in namedChunkGroups in the stats.

@sokra
sokra merged commit ec8e1bb into master Sep 8, 2020
@sokra
sokra deleted the feature/web-worker branch September 8, 2020 10:20
@webpack-bot

Copy link
Copy Markdown
Contributor

I've created an issue to document this in webpack/webpack.js.org.

@developit

Copy link
Copy Markdown

@sokra I might suggest avoiding using raw import { Worker } from "worker_threads" in Node since the API differs nontrivially from Web Workers (events don't implement Event, postMessage isn't in global scope, etc). Guy Bedford and I put together a small "polyfill" that smoothes out these differences here, might make for a more consistent default.

@alexander-akait

Copy link
Copy Markdown
Member

@developit import { Worker } from "worker_threads only for Node.js environment

@sokra

sokra commented Sep 22, 2020

Copy link
Copy Markdown
Member Author

webpack doesn't care a lot about the exact API. It only uses importScripts from the WebWorker API. So everything that supports that works. So should your web-worker polyfill. But we should make the detection configurable, so the developer can change which modules should be treated as WebWorkers for parsing.

@developit

Copy link
Copy Markdown

Right - currently it would not work because of the source check when Worker is an imported identifier.

@evilebottnawi I know, my point was that this makes it impossible to write code that supports both web and node.

@sokra

sokra commented Sep 22, 2020

Copy link
Copy Markdown
Member Author

I'll add this:

module.rules: [
  {
    test: /\.[cm]?js$/,
    parser: {
      worker: ["...", "default from web-worker"]
    }
  }
]

"..." allows to reference the default, which is

 [
	"Worker",
	"SharedWorker",
	"navigator.serviceWorker.register()",
	"Worker from worker_threads"
]

@yamafaktory

Copy link
Copy Markdown

Hi @sokra,

First of all thanks for this awesome feature, it's really nice to have it available out of the box!

One question though, is it possible to use something else than import.meta.url? The reason is that if you use a loader like esbuild e.g. and do not target es2020 explicitly (see evanw/esbuild#349), the import.meta will get swallowed and not passed to Webpack, resulting in a non generated chunk and a broken URL anyway...which is not ideal if one wants to target es2017 for instance.

@alexander-akait

Copy link
Copy Markdown
Member

@yamafaktory What do you want to use? This syntax works on node and browsers, without import.meta.url it will not work

@yamafaktory

Copy link
Copy Markdown

@evilebottnawi that's exactly what I want to highlight here. Depending on the loader you use and the target, import.meta.url can eventually be undefined and thus this feature can't be used. I fully understand but was simply asking if it would be possible to use something else.

@alexander-akait

Copy link
Copy Markdown
Member

You can use any import abilities, webpack known how handle them, I think esbuild should have more abilities to setup output

@yamafaktory

Copy link
Copy Markdown

Ok, thanks for the feedback!

@sokra

sokra commented Oct 12, 2020

Copy link
Copy Markdown
Member Author

I think it's not possible to use this feature without import.meta.

As workaround use something different and chain the esbuild-loader with the string-replace-loader and replace that with import.meta

@yamafaktory

Copy link
Copy Markdown

@sokra thanks for the feedback, this sounds like a viable approach indeed! How do you inject back import.meta? Is it somehow exposed by Webpack?

@willmorgan

Copy link
Copy Markdown

This new way of handling workers is simple, saves a worker loading plugin, and is far more intuitive.

I'm working on a project that ships a library to consumers which means that more often than not, the worker sits on a CDN on some other origin.

I've skimmed the source code and it doesn't look immediately possible to add new worker import strategies.

But I wonder: is there a plugin available for adding fetch support? If no, how would I go about writing one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants