Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 26.9k
WIP: Add support for WebWorker with worker-loader#5886
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
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
62f8b7e24ca90069147566e28d7a45707ada671c978ba668e19138700d7c9ba8ff30b05ae1a349cc7667521e492a5058d6ea7fbccd9b98ff136367cFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| --- | ||
| id: using-web-workers | ||
| titile: Using Web Workers | ||
| --- | ||
| [Web Workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers) | ||
| can be used by including files with the `.worker.js` in your application. Files | ||
| with this extension make use of [`worker-loader`](https://github.com/webpack-contrib/worker-loader) | ||
| to bundle worker files which can be used by your main application. | ||
| A sample WebWorker may look like: | ||
| ```js | ||
| // hello.worker.js | ||
| let helloInterval; | ||
| const sayHello = () => { | ||
| self.postMessage({ message: 'Hello' }); | ||
| }; | ||
| self.addEventListener('message', event => { | ||
| if (event.data.run === true) { | ||
| self.postMessage({ status: 'Worker started' }); | ||
| helloInterval = setInterval(sayHello, 1000); | ||
| } | ||
| if (event.data.run === false) { | ||
| self.postMessage({ status: 'Worker stopped' }); | ||
| clearInterval(helloInterval); | ||
| } | ||
| }); | ||
| ``` | ||
| This can subsequently be imported and used in your application as: | ||
| ```js | ||
| // index.js | ||
| import HelloWorker from './hello.worker.js'; | ||
| const helloWorker = new HelloWorker(); | ||
| let messageCount = 0; | ||
| helloWorker.postMessage({ run: true }); | ||
| helloWorker.onmessage = event => { | ||
| if (event.data.status) { | ||
| console.log('STATUS', event.data.status); | ||
| } | ||
| if (event.data.message) { | ||
| messageCount += 1; | ||
| console.log('MESSAGE', event.data.message); | ||
| if (messageCount >= 5) { | ||
| helloWorker.postMessage({ run: false }); | ||
| } | ||
| } | ||
| }; | ||
| ``` | ||
| ## Importing modules into your workers | ||
| Worker files can import modules just the same as the rest of your | ||
| application. These will be compiled following the same rules and features as | ||
| regular `.js` or `.ts` files. | ||
| ## Usage with TypeScript | ||
| Workers can be written in TypeScript, however you are required to declare the | ||
| file as a worker in order for the compiler to understand that it is a worker. | ||
| This can be done by including: | ||
| ```ts | ||
| /// <reference lib="webworker" /> | ||
| ``` | ||
| at the beginning of all of your `.worker.ts` files. | ||
| Because the interface imported is different from what is in your worker files, | ||
| you'll also need to tell TypeScript what you're expecting this interface to look | ||
| like. To achieve this, you will need to have a module declaration in each of | ||
| your worker files like so: | ||
| ```ts | ||
| // my.worker.ts | ||
| // <worker code> | ||
| // Necessary to tell typescript that this worker file is a module even though | ||
| // it may not have any explicit imports or exports | ||
| export {}; | ||
| // Override the module declaration to tell Typescript that when imported, this | ||
| // is what the imported types will look like. | ||
| declare module './my.worker' { | ||
| export default class TestWorker extends Worker { | ||
| constructor(); | ||
| } | ||
| } | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -178,6 +178,7 @@ module.exports = function(webpackEnv) { | ||
| // We inferred the "public path" (such as / or /my-project) from homepage. | ||
| // We use "/" in development. | ||
| publicPath: publicPath, | ||
| globalObject: 'this', | ||
| // Point sourcemap entries to original disk location (format as URL on Windows) | ||
| devtoolModuleFilenameTemplate: isEnvProduction | ||
| ? info => | ||
| @@ -350,6 +351,63 @@ module.exports = function(webpackEnv) { | ||
| name: 'static/media/[name].[hash:8].[ext]', | ||
| }, | ||
| }, | ||
| // Process WebWorker JS with Babel. | ||
| // The preset includes JSX, Flow, TypeScript, and some ESnext features. | ||
| { | ||
| test: /\.worker\.(js|mjs|ts)$/, | ||
| include: paths.appSrc, | ||
| use: [ | ||
| require.resolve('worker-loader'), | ||
| { | ||
| loader: require.resolve('babel-loader'), | ||
| options: { | ||
| customize: require.resolve( | ||
| 'babel-preset-react-app/webpack-overrides' | ||
| ), | ||
| // @remove-on-eject-begin | ||
| babelrc: false, | ||
| configFile: false, | ||
| presets: [require.resolve('babel-preset-react-app')], | ||
| // Make sure we have a unique cache identifier, erring on the | ||
| // side of caution. | ||
| // We remove this when the user ejects because the default | ||
| // is sane and uses Babel options. Instead of options, we use | ||
| // the react-scripts and babel-preset-react-app versions. | ||
| cacheIdentifier: getCacheIdentifier( | ||
| isEnvProduction | ||
| ? 'production' | ||
| : isEnvDevelopment && 'development', | ||
| [ | ||
| 'babel-plugin-named-asset-import', | ||
| 'babel-preset-react-app', | ||
| 'react-dev-utils', | ||
| 'react-scripts', | ||
| ] | ||
| ), | ||
| // @remove-on-eject-end | ||
| plugins: [ | ||
| [ | ||
| require.resolve('babel-plugin-named-asset-import'), | ||
| { | ||
| loaderMap: { | ||
| svg: { | ||
| ReactComponent: | ||
| '@svgr/webpack?-prettier,-svgo![path]', | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| ], | ||
| // This is a feature of `babel-loader` for webpack (not Babel itself). | ||
| // It enables caching results in ./node_modules/.cache/babel-loader/ | ||
| // directory for faster rebuilds. | ||
| cacheDirectory: true, | ||
| ||
| cacheCompression: isEnvProduction, | ||
| compact: isEnvProduction, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| // Process application JS with Babel. | ||
| // The preset includes JSX, Flow, TypeScript, and some ESnext features. | ||
| { | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is required due to what seems to be a bug/regression in webpack 4. More details can be found here: webpack/webpack#6642
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @iansu , can i ask a question . Why globalObject value is this , not self ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ri7nz -- as far as I can tell, the eslint config inside
create-react-appbugs out onselfkeyword saying it's invalid, and hence the empty production build when using worker-loader yourself.