Bundle classic Vite web workers during development.
Vite will bundle classic web workers at build time but not during development. This can cause some development-only bugs (ex: vitejs/vite#8470) since imports won't be supported during development but will be in production. This is a simple Vite plugin that will force your web workers to be bundled during development too.
npm install vite-plugin-prebundle-workers
// vite.config.js
import { defineConfig } from "vite";
import prebundleWorkers from "vite-plugin-prebundle-workers";
export default defineConfig({
plugins: [
prebundleWorkers({
include: "src/myGreatWorker.ts"
})
],
});
Options are:
include
andexclude
: Passed tocreateFilter
(docs). One of them must be truthy or an error is thrown.configureEsBuild
: A function that's given theid
of the file (usually a file path) and the configuration that would be passed toesbuild.build
by default. Should return the configuration to pass toesbuild.build
.
const worker = new Worker(new URL("./myGreatWorker.ts", import.meta.url));
Forked off from simple-worker-vite. This fork ended up being pretty much entirely different though so I created a new plugin instead of trying to submit patches upstream.