Skip to content

Commit 2712837

Browse files
Speed up builds by not bundling typescript (#398)
This marks internal/typescript.js as internal in the worker Rollup config, and switches the worker to use modules so it can load the typescript module build. playground-typescript-worker.js's size goes from 3034427 bytes to 39289 bytes, and the build time on my M1 Pro goes from 26.2s to 4.4s when the typescript bundle is already built. Module worker support is very good now, at > 96% of tracked users: https://caniuse.com/mdn-api_worker_worker_ecmascript_modules
1 parent aba2f14 commit 2712837

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1818
### Changed
1919

2020
- Upgraded to TypeScript 5.2 and Lit 3.0
21+
- **BREAKING** Use modules in workers. See [caniuse.com's support table](https://caniuse.com/mdn-api_worker_worker_ecmascript_modules) for browser support information.
2122

2223
<!-- ### Added -->
2324
<!-- ### Fixed -->

rollup.config.typescript.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ import commonjs from '@rollup/plugin-commonjs';
2323
* because we don't need to run the commonjs transform every time we build the
2424
* worker.
2525
*
26-
* - We may want to use module imports in the worker at some point, for a faster
27-
* development mode that doesn't bundle. Having only module sources will make
28-
* this easier too.
26+
* - We use module imports in the worker, for a faster development mode that
27+
* doesn't bundle. Having only module sources makes this easier too.
2928
*/
3029
export default [
3130
{

rollup.config.web-worker.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,21 @@
66

77
import resolve from '@rollup/plugin-node-resolve';
88
import {maybeTerser} from './rollup.config.common.js';
9+
import * as path from 'path';
10+
11+
const internalTypescriptPath = path.resolve(process.cwd(), 'internal/typescript.js');
912

1013
export default {
1114
input: 'typescript-worker/playground-typescript-worker.js',
15+
external(id, parentId, isResolved) {
16+
if (!isResolved && parentId !== undefined) {
17+
id = path.resolve(path.dirname(parentId), id);
18+
}
19+
return id === internalTypescriptPath;
20+
},
1221
output: {
1322
file: 'playground-typescript-worker.js',
14-
format: 'iife',
23+
format: 'esm',
1524
exports: 'none',
1625
},
1726
plugins: [resolve(), ...maybeTerser],

src/playground-project.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ export class PlaygroundProject extends LitElement {
451451
let worker: Worker;
452452
if (typescriptWorkerScriptUrl.origin === window.location.origin) {
453453
// Easy case.
454-
worker = new Worker(typescriptWorkerScriptUrl);
454+
worker = new Worker(typescriptWorkerScriptUrl, {type: 'module'});
455455
} else {
456456
// If the worker script is different-origin, we need to fetch it ourselves
457457
// and create a blob URL.

0 commit comments

Comments
 (0)