Skip to content

Commit

Permalink
feat: support worker-loader with config workerLoader (#6253)
Browse files Browse the repository at this point in the history
* feat: support worker-loader with config workerLoader

* fix: ci
  • Loading branch information
sorrycc authored Mar 11, 2021
1 parent 906363c commit 7a33a61
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 11 deletions.
7 changes: 7 additions & 0 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1249,3 +1249,10 @@ export default {
包含以下子配置项:

- lazyCompilation,是否启用基于路由的按需编译

## workerLoader <Badge>3.4.1+</Badge>

- Type: `object`
- Default: `false`

开启 worker-loader 功能。
7 changes: 7 additions & 0 deletions docs/config/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -1248,3 +1248,10 @@ export default {
包含以下子配置项:

- lazyCompilation,是否启用基于路由的按需编译

## workerLoader <Badge>3.4.1+</Badge>

- Type: `object`
- Default: `false`

开启 worker-loader 功能。
2 changes: 1 addition & 1 deletion packages/bundler-webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"dependencies": {
"@umijs/bundler-utils": "3.4.0",
"@umijs/deps": "0.2.34",
"@umijs/deps": "0.3.3",
"@umijs/types": "3.4.0",
"@umijs/utils": "3.4.0",
"jest-worker": "26.6.2",
Expand Down
3 changes: 3 additions & 0 deletions packages/bundler-webpack/src/fixtures/worker-loader/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
workerLoader: {},
}
7 changes: 7 additions & 0 deletions packages/bundler-webpack/src/fixtures/worker-loader/expect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IExpectOpts } from '../types';

export default ({ indexJS, files }: IExpectOpts) => {
expect(files).toContain('index.js');
expect(files).toContain('worker.worker.js');
expect(indexJS).toContain(`return new Worker(__webpack_require__.p + "worker.worker.js");`);
}
4 changes: 4 additions & 0 deletions packages/bundler-webpack/src/fixtures/worker-loader/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Worker from './worker';

const worker = new Worker();
worker.postMessage('aaaa');
4 changes: 4 additions & 0 deletions packages/bundler-webpack/src/fixtures/worker-loader/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

self.addEventListener('message', (e) => {
console.log('message received');
});
10 changes: 10 additions & 0 deletions packages/bundler-webpack/src/getConfig/getConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,16 @@ export default async function getConfig(
.use('raw-loader')
.loader(require.resolve('@umijs/deps/compiled/raw-loader'));

if (config.workerLoader) {
// prettier-ignore
webpackConfig.module
.rule('worker')
.test(/.*worker.(ts|js)/)
.use('worker-loader')
.loader(require.resolve('@umijs/deps/compiled/worker-loader'))
.options(config.workerLoader);
}

// css
css({
type,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"dependencies": {
"@umijs/ast": "3.4.0",
"@umijs/babel-preset-umi": "3.4.0",
"@umijs/deps": "0.2.34",
"@umijs/deps": "0.3.3",
"@umijs/utils": "3.4.0"
}
}
2 changes: 1 addition & 1 deletion packages/preset-built-in/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@types/react-router-config": "5.0.1",
"@umijs/babel-preset-umi": "3.4.0",
"@umijs/bundler-webpack": "3.4.0",
"@umijs/deps": "0.2.34",
"@umijs/deps": "0.3.3",
"@umijs/renderer-mpa": "3.4.0",
"@umijs/renderer-react": "3.4.0",
"@umijs/runtime": "3.4.0",
Expand Down
1 change: 1 addition & 0 deletions packages/preset-built-in/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export default function () {
require.resolve('./plugins/features/umiInfo'),
require.resolve('./plugins/features/runtimeHistory'),
require.resolve('./plugins/features/webpack5'),
require.resolve('./plugins/features/workerLoader'),

// html
require.resolve('./plugins/features/html/favicon'),
Expand Down
12 changes: 12 additions & 0 deletions packages/preset-built-in/src/plugins/features/workerLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { IApi } from '@umijs/types';

export default (api: IApi) => {
api.describe({
key: 'workerLoader',
config: {
schema(joi) {
return joi.object();
},
},
});
};
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"access": "public"
},
"dependencies": {
"@umijs/deps": "0.2.34",
"@umijs/deps": "0.3.3",
"@umijs/utils": "3.4.0"
}
}
2 changes: 1 addition & 1 deletion packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"dependencies": {
"@umijs/babel-preset-umi": "3.4.0",
"@umijs/core": "3.4.0",
"@umijs/deps": "0.2.34",
"@umijs/deps": "0.3.3",
"@umijs/server": "3.4.0",
"@umijs/utils": "3.4.0",
"webpack-chain": "6.5.1"
Expand Down
2 changes: 1 addition & 1 deletion packages/umi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"dependencies": {
"@umijs/bundler-webpack": "3.4.0",
"@umijs/core": "3.4.0",
"@umijs/deps": "0.2.34",
"@umijs/deps": "0.3.3",
"@umijs/preset-built-in": "3.4.0",
"@umijs/runtime": "3.4.0",
"@umijs/types": "3.4.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
"access": "public"
},
"dependencies": {
"@umijs/deps": "0.2.34"
"@umijs/deps": "0.3.3"
}
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2945,10 +2945,10 @@
dependencies:
"@types/yargs-parser" "*"

"@umijs/deps@0.2.34":
version "0.2.34"
resolved "https://registry.yarnpkg.com/@umijs/deps/-/deps-0.2.34.tgz#3d748c5818da53f013d16dbb686c1b00e4409a4b"
integrity sha512-6PUTf2IeMh1V1k0aoFOT4Z7gs3vYPOxkDuKsjglT+UrQxa8Wfm613U7LecnkC6nv3wZk23oolGmE6fjMgtYfSw==
"@umijs/deps@0.3.3":
version "0.3.3"
resolved "https://registry.yarnpkg.com/@umijs/deps/-/deps-0.3.3.tgz#b712f7b66c5d2339e7833fc2547b7b50b8390ade"
integrity sha512-MO13OHd77nXObz1B5xaYOc/ZxdVyaaV6ZRIVVxsWKsB941zEJC9lImvga7ZMd7d3Fy6+M7HRi9LsCo8PHUhp5Q==
dependencies:
chokidar "3.5.1"
clipboardy "2.3.0"
Expand Down

0 comments on commit 7a33a61

Please sign in to comment.