Skip to content

Commit 1b61029

Browse files
marcbouchenoireTimDaubdevelopit
authored
Rebase: Add worker-loader (#867)
* Add worker-loader Targets enhancement: #170 Enable usage of `microbundle --worker-loader` to apply rollup-plugin-web-worker-loader. Allow bundling web workers automatically - Add flag `--worker-loader` - For format `es` and `modern`, a Worker `type: module` is automatically bundled * Update README.md Co-authored-by: Jason Miller <developit@users.noreply.github.com> * Update src/index.js Co-authored-by: Jason Miller <developit@users.noreply.github.com> * Update src/prog.js Co-authored-by: Jason Miller <developit@users.noreply.github.com> * Update test/__snapshots__/index.test.js.snap Co-authored-by: Jason Miller <developit@users.noreply.github.com> * Update test/fixtures/worker-loader/package.json Co-authored-by: Jason Miller <developit@users.noreply.github.com> * Update README.md Co-authored-by: Jason Miller <developit@users.noreply.github.com> * Remove node_modules from snapshot * Upgrade rollup-plugin-off-main-thread * Add changeset Co-authored-by: Tim Daubenschütz <tim@daubenschuetz.de> Co-authored-by: Jason Miller <developit@users.noreply.github.com>
1 parent b1a6374 commit 1b61029

12 files changed

Lines changed: 338 additions & 9 deletions

File tree

.changeset/curly-eels-watch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'microbundle': minor
3+
---
4+
5+
- Add support for Module Workers with a new `--workers` flag

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,26 @@ This can be customized by passing the command line argument `--css-modules "[nam
253253
| true | import './my-file.css'; | :white_check_mark: |
254254
| true | import './my-file.module.css'; | :white_check_mark: |
255255

256+
### Building Module Workers
257+
258+
Microbundle is able to detect and bundle Module Workers when generating bundles in the
259+
`es`, `umd` and `modern` formats. To use this feature, instantiate your Web Worker as follows:
260+
261+
```js
262+
worker = new Worker(new URL('./worker.js', import.meta.url), { type: 'module' });
263+
// or simply:
264+
worker = new Worker('./worker.js', { type: 'module' });
265+
```
266+
267+
... then add the `--workers` flag to your build command:
268+
269+
```bash
270+
microbundle --workers
271+
```
272+
273+
For more information see
274+
[@surma/rollup-plugin-off-main-thread](https://github.com/surma/rollup-plugin-off-main-thread#config).
275+
256276
### Mangling Properties
257277
258278
To achieve the smallest possible bundle size, libraries often wish to rename internal object properties or class members to smaller names - transforming `this._internalIdValue` to `this._i`. Microbundle doesn't do this by default, however it can be enabled by creating a `mangle.json` file (or a `"mangle"` property in your package.json). Within that file, you can specify a regular expression pattern to control which properties should be mangled. For example: to mangle all property names beginning an underscore:
@@ -316,6 +336,7 @@ Options
316336
--generateTypes Whether or not to generate types, if `types` or `typings` is set in `package.json` then it will default to be `true`
317337
--css Where to output CSS: "inline" or "external" (default: "external")
318338
--css-modules Configures .css to be treated as modules (default: null)
339+
--workers Bundle module workers - see https://git.io/J3oSF (default false)
319340
-h, --help Displays this message
320341

321342
Examples

package-lock.json

Lines changed: 105 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
"@rollup/plugin-commonjs": "^17.0.0",
8989
"@rollup/plugin-json": "^4.1.0",
9090
"@rollup/plugin-node-resolve": "^11.0.1",
91+
"@surma/rollup-plugin-off-main-thread": "^2.2.2",
9192
"asyncro": "^3.0.0",
9293
"autoprefixer": "^10.1.0",
9394
"babel-plugin-macros": "^3.0.1",

src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import alias from '@rollup/plugin-alias';
1818
import postcss from 'rollup-plugin-postcss';
1919
import typescript from 'rollup-plugin-typescript2';
2020
import json from '@rollup/plugin-json';
21+
import OMT from '@surma/rollup-plugin-off-main-thread';
2122
import logError from './log-error';
2223
import { isDir, isFile, stdout, isTruthy, removeScope } from './utils';
2324
import { getSizeInfo } from './lib/compressed-size';
@@ -389,6 +390,8 @@ function createConfig(options, entry, format, writeMeta) {
389390
options.generateTypes == null
390391
? !!(pkg.types || pkg.typings)
391392
: options.generateTypes;
393+
const useWorkerLoader = options.workers !== false;
394+
392395
const escapeStringExternals = ext =>
393396
ext instanceof RegExp ? ext.source : escapeStringRegexp(ext);
394397
const externalPredicate = new RegExp(
@@ -427,7 +430,7 @@ function createConfig(options, entry, format, writeMeta) {
427430
let config = {
428431
/** @type {import('rollup').InputOptions} */
429432
inputOptions: {
430-
// disable Rollup's cache for the modern build to prevent re-use of legacy transpiled modules:
433+
// disable Rollup's cache for modern builds to prevent re-use of legacy transpiled modules:
431434
cache,
432435
input: entry,
433436
external: id => {
@@ -616,6 +619,9 @@ function createConfig(options, entry, format, writeMeta) {
616619
},
617620
},
618621
],
622+
// NOTE: OMT only works with amd and esm
623+
// Source: https://github.com/surma/rollup-plugin-off-main-thread#config
624+
useWorkerLoader && (format === 'es' || modern) && OMT(),
619625
/** @type {import('rollup').Plugin} */
620626
({
621627
name: 'postprocessing',

src/prog.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ export default handler => {
5757
.option('--cwd', 'Use an alternative working directory', '.')
5858
.option('--sourcemap', 'Generate source map')
5959
.option('--css', 'Where to output CSS: "inline" or "external"', 'external')
60+
.option(
61+
'--workers',
62+
'Bundle module workers - see https://git.io/J3oSF',
63+
false,
64+
)
6065
.option(
6166
'--css-modules',
6267
'Turns on css-modules for all .css imports. Passing a string will override the scopeName. eg --css-modules="_[hash]"',

test/__snapshots__/index.test.js.snap

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,3 +3016,63 @@ exports[`fixtures build ts-module with microbundle 7`] = `
30163016
//# sourceMappingURL=ts-module.umd.js.map
30173017
"
30183018
`;
3019+
3020+
exports[`fixtures build worker-loader with microbundle 1`] = `
3021+
"Used script: microbundle -f modern,es --workers
3022+
3023+
Directory tree:
3024+
3025+
worker-loader
3026+
dist
3027+
worker-35b22e56.js
3028+
worker-35b22e56.js.map
3029+
worker-7e1b9921.js
3030+
worker-7e1b9921.js.map
3031+
worker-loader.esm.js
3032+
worker-loader.esm.js.map
3033+
worker-loader.modern.js
3034+
worker-loader.modern.js.map
3035+
package-lock.json
3036+
package.json
3037+
src
3038+
bar.js
3039+
index.js
3040+
worker.js
3041+
3042+
3043+
Build \\"workerLoader\\" to dist:
3044+
140 B: worker-loader.modern.js.gz
3045+
112 B: worker-loader.modern.js.br
3046+
63 B: worker-7e1b9921.js.gz
3047+
54 B: worker-7e1b9921.js.br
3048+
150 B: worker-loader.esm.js.gz
3049+
123 B: worker-loader.esm.js.br
3050+
81 B: worker-35b22e56.js.gz
3051+
66 B: worker-35b22e56.js.br"
3052+
`;
3053+
3054+
exports[`fixtures build worker-loader with microbundle 2`] = `8`;
3055+
3056+
exports[`fixtures build worker-loader with microbundle 3`] = `
3057+
"self.onmessage=function(e){return self.postMessage(e.data+\\"bar\\")};
3058+
//# sourceMappingURL=worker-35b22e56.js.map
3059+
"
3060+
`;
3061+
3062+
exports[`fixtures build worker-loader with microbundle 4`] = `
3063+
"self.onmessage=s=>self.postMessage(s.data+\\"bar\\");
3064+
//# sourceMappingURL=worker-7e1b9921.js.map
3065+
"
3066+
`;
3067+
3068+
exports[`fixtures build worker-loader with microbundle 5`] = `
3069+
"var e=new Worker(new URL(\\"worker-35b22e56.js\\",import.meta.url),{type:\\"module\\"});e.onmessage=function(e){return\\"foobar\\"===e.data},e.postMessage(\\"foo\\");
3070+
//# sourceMappingURL=worker-loader.esm.js.map
3071+
"
3072+
`;
3073+
3074+
exports[`fixtures build worker-loader with microbundle 6`] = `
3075+
"const e=new Worker(new URL(\\"worker-7e1b9921.js\\",import.meta.url),{type:\\"module\\"});e.onmessage=e=>\\"foobar\\"===e.data,e.postMessage(\\"foo\\");
3076+
//# sourceMappingURL=worker-loader.modern.js.map
3077+
"
3078+
`;

0 commit comments

Comments
 (0)