From ef952fa59a31580dd58b72158636908fdb8f9b9d Mon Sep 17 00:00:00 2001 From: Justin Fagnani Date: Mon, 29 Jan 2024 16:13:02 -0800 Subject: [PATCH] Speed up builds by not bundling typescript --- CHANGELOG.md | 1 + rollup.config.typescript.js | 5 ++--- rollup.config.web-worker.js | 11 ++++++++++- src/playground-project.ts | 2 +- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d2827297..7b488afc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Changed - Upgraded to TypeScript 5.2 and Lit 3.0 +- **BREAKING** Use modules in workers. See [caniuse.com's support table](https://caniuse.com/mdn-api_worker_worker_ecmascript_modules) for browser support information. diff --git a/rollup.config.typescript.js b/rollup.config.typescript.js index 4ad01cbf..a440c401 100644 --- a/rollup.config.typescript.js +++ b/rollup.config.typescript.js @@ -23,9 +23,8 @@ import commonjs from '@rollup/plugin-commonjs'; * because we don't need to run the commonjs transform every time we build the * worker. * - * - We may want to use module imports in the worker at some point, for a faster - * development mode that doesn't bundle. Having only module sources will make - * this easier too. + * - We use module imports in the worker, for a faster development mode that + * doesn't bundle. Having only module sources makes this easier too. */ export default [ { diff --git a/rollup.config.web-worker.js b/rollup.config.web-worker.js index 638649f2..9df03eca 100644 --- a/rollup.config.web-worker.js +++ b/rollup.config.web-worker.js @@ -6,12 +6,21 @@ import resolve from '@rollup/plugin-node-resolve'; import {maybeTerser} from './rollup.config.common.js'; +import * as path from 'path'; + +const internalTypescriptPath = path.resolve(process.cwd(), 'internal/typescript.js'); export default { input: 'typescript-worker/playground-typescript-worker.js', + external(id, parentId, isResolved) { + if (!isResolved && parentId !== undefined) { + id = path.resolve(path.dirname(parentId), id); + } + return id === internalTypescriptPath; + }, output: { file: 'playground-typescript-worker.js', - format: 'iife', + format: 'esm', exports: 'none', }, plugins: [resolve(), ...maybeTerser], diff --git a/src/playground-project.ts b/src/playground-project.ts index b5e40fad..2d485ec5 100644 --- a/src/playground-project.ts +++ b/src/playground-project.ts @@ -451,7 +451,7 @@ export class PlaygroundProject extends LitElement { let worker: Worker; if (typescriptWorkerScriptUrl.origin === window.location.origin) { // Easy case. - worker = new Worker(typescriptWorkerScriptUrl); + worker = new Worker(typescriptWorkerScriptUrl, {type: 'module'}); } else { // If the worker script is different-origin, we need to fetch it ourselves // and create a blob URL.