-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
WIP: Add support for WebWorker with worker-loader #5886
Open
iansu
wants to merge
17
commits into
facebook:main
Choose a base branch
from
iansu:worker-loader
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+215
−39
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
62f8b7e
Add support for WebWorker with worker-loader
iansu 24ca900
Add support for writing WebWorkers in TypeScript
iansu 6914756
Don't allow workers with jsx or tsx extension
iansu 6e28d7a
Merge branch 'master' of github.com:mattdarveniza/create-react-app in…
45707ad
Added eslint exception for worker files using 'self'
a671c97
Added documentation for web workers
8ba668e
Added constructor rule and fixed file matching
1913870
Updated ts worker docs
0d7c9ba
Added comment explaining lint rule for workers
8ff30b0
Add support for WebWorker with worker-loader
iansu 5ae1a34
Add support for writing WebWorkers in TypeScript
iansu 9cc7667
Don't allow workers with jsx or tsx extension
iansu 521e492
Merge branch 'worker-loader' into worker-loader
mattdarveniza a5058d6
clarified comment
ea7fbcc
merge
d9b98ff
Merge branch 'worker-loader' of github.com:mattdarveniza/create-react…
136367c
Merge pull request #1 from mattdarveniza/worker-loader
iansu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
cacheCompression: isEnvProduction, | ||
compact: isEnvProduction, | ||
}, | ||
}, | ||
], | ||
}, | ||
// Process application JS with Babel. | ||
// The preset includes JSX, Flow, TypeScript, and some ESnext features. | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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-app
bugs out onself
keyword saying it's invalid, and hence the empty production build when using worker-loader yourself.