Skip to content

Instantly share code, notes, and snippets.

@jamsinclair
Last active December 4, 2023 15:03
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jamsinclair/1585d4fc7acca314a69f1d1dc117a0cf to your computer and use it in GitHub Desktop.
Experimental Vite plugin to not bundle module workers and chunk them instead (vitejs/vite issue #7015)
const MODULE_WORKER_REGEX =
/new\s+Worker\s*\(\s*new\s+URL\s*\(\s*["'](.*?)["']\s*,\s*import\.meta\.url\s*\)\s*,\s*\{\s*type\s*:\s*["']module["']\s*\}\s*\)/g;
const TEMPORARY_DYNAMIC_IMPORT_REGEX =
/\/\*__TEMP_WORKER__\*\/__vitePreload\(\(\) => import\((['"])(.+?)\1\),.*?\)/g;
const hasNoPathPrefix = (path) => !/^[\./]|^file:|^https?:/.test(path);
export default function moduleWorkerChunkPlugin() {
return {
name: "module-worker-chunk-plugin",
apply: "build",
transform(code, _id) {
if (!code.includes("new Worker")) {
return null;
}
const transformedCode = code.replaceAll(
MODULE_WORKER_REGEX,
(_, workerPath) => {
const resolvedPath = hasNoPathPrefix(workerPath)
? `./${workerPath}`
: workerPath;
return `/*__TEMP_WORKER__*/import('${resolvedPath}')`;
}
);
return {
code: transformedCode,
map: null,
};
},
renderChunk(code) {
if (!code.includes("/*__TEMP_WORKER__*/")) {
return null;
}
const transformedCode = code.replaceAll(
TEMPORARY_DYNAMIC_IMPORT_REGEX,
(_, _quote, importPath) => {
return `new Worker(new URL('${importPath}', import.meta.url), { type: 'module' })`;
}
);
return {
code: transformedCode,
map: null,
};
},
};
}
import { defineConfig } from 'vite'
import moduleWorkerChunkPlugin from './plugin.js';
export default defineConfig({
plugins: [
moduleWorkerChunkPlugin()
],
build: {
modulePreload: false,
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment