Skip to content

Commit 136367c

Browse files
authored
Merge pull request #1 from mattdarveniza/worker-loader
Worker loader eslint support and documentation
2 parents 9cc7667 + d9b98ff commit 136367c

File tree

8 files changed

+154
-37
lines changed

8 files changed

+154
-37
lines changed

docusaurus/docs/adding-a-sass-stylesheet.md

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ This will allow you to do imports like
3737
To use imports relative to a path you specify, and from `node_modules` without adding the `~` prefix, you can add a [`.env` file](https://github.com/facebook/create-react-app/blob/master/docusaurus/docs/adding-custom-environment-variables.md#adding-development-environment-variables-in-env) at the project root with the variable `SASS_PATH=node_modules:src`. To specify more directories you can add them to `SASS_PATH` separated by a `:` like `path1:path2:path3`.
3838

3939
If you set `SASS_PATH=node_modules:src`, this will allow you to do imports like
40+
4041
```scss
4142
@import 'styles/colors'; // assuming a styles directory under src/, where _colors.scss partial file exists.
4243
@import 'nprogress/nprogress'; // importing a css file from the nprogress node module

docusaurus/docs/advanced-configuration.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ id: advanced-configuration
33
title: Advanced Configuration
44
---
55

6-
You can adjust various development and production settings by setting environment variables in your shell or with [.env](adding-custom-environment-variables.md#adding-development-environment-variables-in-env).
6+
You can adjust various development and production settings by setting environment variables in your shell or with [.env](adding-custom-environment-variables.md#adding-development-environment-variables-in-env).
77

88
> Note: You do not need to declare `REACT_APP_` before the below variables as you would with custom environment variables.
99
@@ -13,7 +13,7 @@ You can adjust various development and production settings by setting environmen
1313
| HOST | ✅ Used | 🚫 Ignored | By default, the development web server binds to `localhost`. You may use this variable to specify a different host. |
1414
| PORT | ✅ Used | 🚫 Ignored | By default, the development web server will attempt to listen on port 3000 or prompt you to attempt the next available port. You may use this variable to specify a different port. |
1515
| HTTPS | ✅ Used | 🚫 Ignored | When set to `true`, Create React App will run the development server in `https` mode. |
16-
| PUBLIC_URL | 🚫 Ignored | ✅ Used | Create React App assumes your application is hosted at the serving web server's root or a subpath as specified in [`package.json` (`homepage`)](deployment#building-for-relative-paths). Normally, Create React App ignores the hostname. You may use this variable to force assets to be referenced verbatim to the url you provide (hostname included). This may be particularly useful when using a CDN to host your application. |
16+
| PUBLIC_URL | 🚫 Ignored | ✅ Used | Create React App assumes your application is hosted at the serving web server's root or a subpath as specified in [`package.json` (`homepage`)](deployment#building-for-relative-paths). Normally, Create React App ignores the hostname. You may use this variable to force assets to be referenced verbatim to the url you provide (hostname included). This may be particularly useful when using a CDN to host your application. |
1717
| CI | ✅ Used | ✅ Used | When set to `true`, Create React App treats warnings as failures in the build. It also makes the test runner non-watching. Most CIs set this flag by default. |
1818
| REACT_EDITOR | ✅ Used | 🚫 Ignored | When an app crashes in development, you will see an error overlay with clickable stack trace. When you click on it, Create React App will try to determine the editor you are using based on currently running processes, and open the relevant source file. You can [send a pull request to detect your editor of choice](https://github.com/facebook/create-react-app/issues/2636). Setting this environment variable overrides the automatic detection. If you do it, make sure your systems [PATH](<https://en.wikipedia.org/wiki/PATH_(variable)>) environment variable points to your editor’s bin folder. You can also set it to `none` to disable it completely. |
1919
| CHOKIDAR_USEPOLLING | ✅ Used | 🚫 Ignored | When set to `true`, the watcher runs in polling mode, as necessary inside a VM. Use this option if `npm start` isn't detecting changes. |

docusaurus/docs/loading-graphql-files.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ You can also use the `gql` template tag the same way you would use the non-macro
5959

6060
```js
6161
import { gql } from 'graphql.macro';
62-
62+
6363
const query = gql`
6464
query User {
6565
user(id: 5) {

docusaurus/docs/using-web-workers.md

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
id: using-web-workers
3+
titile: Using Web Workers
4+
---
5+
6+
[Web Workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers)
7+
can be used by including files with the `.worker.js` in your application. Files
8+
with this extension make use of [`worker-loader`](https://github.com/webpack-contrib/worker-loader)
9+
to bundle worker files which can be used by your main application.
10+
11+
A sample WebWorker may look like:
12+
13+
```js
14+
// hello.worker.js
15+
16+
let helloInterval;
17+
18+
const sayHello = () => {
19+
self.postMessage({ message: 'Hello' });
20+
};
21+
22+
self.addEventListener('message', event => {
23+
if (event.data.run === true) {
24+
self.postMessage({ status: 'Worker started' });
25+
helloInterval = setInterval(sayHello, 1000);
26+
}
27+
28+
if (event.data.run === false) {
29+
self.postMessage({ status: 'Worker stopped' });
30+
clearInterval(helloInterval);
31+
}
32+
});
33+
```
34+
35+
This can subsequently be imported and used in your application as:
36+
37+
```js
38+
// index.js
39+
40+
import HelloWorker from './hello.worker.js';
41+
42+
const helloWorker = new HelloWorker();
43+
let messageCount = 0;
44+
45+
helloWorker.postMessage({ run: true });
46+
47+
helloWorker.onmessage = event => {
48+
if (event.data.status) {
49+
console.log('STATUS', event.data.status);
50+
}
51+
52+
if (event.data.message) {
53+
messageCount += 1;
54+
console.log('MESSAGE', event.data.message);
55+
56+
if (messageCount >= 5) {
57+
helloWorker.postMessage({ run: false });
58+
}
59+
}
60+
};
61+
```
62+
63+
## Importing modules into your workers
64+
65+
Worker files can import modules just the same as the rest of your
66+
application. These will be compiled following the same rules and features as
67+
regular `.js` or `.ts` files.
68+
69+
## Usage with TypeScript
70+
71+
Workers can be written in TypeScript, however you are required to declare the
72+
file as a worker in order for the compiler to understand that it is a worker.
73+
This can be done by including:
74+
75+
```ts
76+
/// <reference lib="webworker" />
77+
```
78+
79+
at the beginning of all of your `.worker.ts` files.
80+
81+
Because the interface imported is different from what is in your worker files,
82+
you'll also need to tell TypeScript what you're expecting this interface to look
83+
like. To achieve this, you will need to have a module declaration in each of
84+
your worker files like so:
85+
86+
```ts
87+
// my.worker.ts
88+
// <worker code>
89+
90+
// Necessary to tell typescript that this worker file is a module even though
91+
// it may not have any explicit imports or exports
92+
export {};
93+
94+
// Override the module declaration to tell Typescript that when imported, this
95+
// is what the imported types will look like.
96+
declare module './my.worker' {
97+
export default class TestWorker extends Worker {
98+
constructor();
99+
}
100+
}
101+
```

docusaurus/website/sidebars.json

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"adding-a-router",
3737
"adding-custom-environment-variables",
3838
"making-a-progressive-web-app",
39+
"using-web-workers",
3940
"production-build"
4041
],
4142
"Testing": ["running-tests", "debugging-tests"],

packages/eslint-config-react-app/index.js

+42-31
Original file line numberDiff line numberDiff line change
@@ -52,40 +52,51 @@ module.exports = {
5252
},
5353
},
5454

55-
overrides: {
56-
files: ['**/*.ts', '**/*.tsx'],
57-
parser: '@typescript-eslint/parser',
58-
parserOptions: {
59-
ecmaVersion: 2018,
60-
sourceType: 'module',
61-
ecmaFeatures: {
62-
jsx: true,
63-
},
55+
overrides: [
56+
{
57+
files: ['**/*.ts', '**/*.tsx'],
58+
parser: '@typescript-eslint/parser',
59+
parserOptions: {
60+
ecmaVersion: 2018,
61+
sourceType: 'module',
62+
ecmaFeatures: {
63+
jsx: true,
64+
},
6465

65-
// typescript-eslint specific options
66-
warnOnUnsupportedTypeScriptVersion: true,
66+
// typescript-eslint specific options
67+
warnOnUnsupportedTypeScriptVersion: true,
68+
},
69+
plugins: ['@typescript-eslint'],
70+
rules: {
71+
// These ESLint rules are known to cause issues with typescript-eslint
72+
// See https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/src/configs/recommended.json
73+
camelcase: 'off',
74+
indent: 'off',
75+
'no-array-constructor': 'off',
76+
'no-unused-vars': 'off',
77+
'@typescript-eslint/no-angle-bracket-type-assertion': 'warn',
78+
'@typescript-eslint/no-array-constructor': 'warn',
79+
'@typescript-eslint/no-namespace': 'error',
80+
'@typescript-eslint/no-unused-vars': [
81+
'warn',
82+
{
83+
args: 'none',
84+
ignoreRestSiblings: true,
85+
},
86+
],
87+
},
6788
},
68-
plugins: ['@typescript-eslint'],
69-
rules: {
70-
// These ESLint rules are known to cause issues with typescript-eslint
71-
// See https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/src/configs/recommended.json
72-
camelcase: 'off',
73-
indent: 'off',
74-
'no-array-constructor': 'off',
75-
'no-unused-vars': 'off',
76-
77-
'@typescript-eslint/no-angle-bracket-type-assertion': 'warn',
78-
'@typescript-eslint/no-array-constructor': 'warn',
79-
'@typescript-eslint/no-namespace': 'error',
80-
'@typescript-eslint/no-unused-vars': [
81-
'warn',
82-
{
83-
args: 'none',
84-
ignoreRestSiblings: true,
85-
},
86-
],
89+
{
90+
files: ['**/*.worker.js', '**/*.worker.mjs', '**/*.worker.ts'],
91+
rules: {
92+
'no-restricted-globals': ['error'].concat(
93+
restrictedGlobals.filter(g => g !== 'self')
94+
),
95+
// Necessary to allow stubbed class declartions in typescript workers
96+
'no-useless-constructor': 'off',
97+
},
8798
},
88-
},
99+
],
89100

90101
// NOTE: When adding rules here, you need to make sure they are compatible with
91102
// `typescript-eslint`, as some rules such as `no-array-constructor` aren't compatible.

packages/react-scripts/fixtures/kitchensink/src/App.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class App extends Component {
6868
// This works around an issue of a duplicate hash in the href
6969
// Ex: http://localhost:3001/#array-destructuring#array-destructuring
7070
// This seems like a jsdom bug as the URL in initDom.js appears to be correct
71-
const feature = url.slice(url.lastIndexOf("#") + 1);
71+
const feature = url.slice(url.lastIndexOf('#') + 1);
7272

7373
switch (feature) {
7474
case 'array-destructuring':

packages/react-scripts/scripts/utils/verifyTypeScriptSetup.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ const immer = require('react-dev-utils/immer').produce;
1818
const globby = require('react-dev-utils/globby').sync;
1919

2020
function writeJson(fileName, object) {
21-
fs.writeFileSync(fileName, JSON.stringify(object, null, 2).replace(/\n/g, os.EOL) + os.EOL);
21+
fs.writeFileSync(
22+
fileName,
23+
JSON.stringify(object, null, 2).replace(/\n/g, os.EOL) + os.EOL
24+
);
2225
}
2326

2427
function verifyNoTypeScript() {
@@ -184,7 +187,7 @@ function verifyTypeScriptSetup() {
184187
)
185188
);
186189
}
187-
190+
188191
console.log(e && e.message ? `${e.message}` : '');
189192
process.exit(1);
190193
}

0 commit comments

Comments
 (0)