A Vite plugin that statically inlines Emscripten worker options at build time.
This could serve as a temporary solution to the Emscripten issue (emscripten-core/emscripten#22394)
Copy the plugin.js code to a file in your project.
Add the plugin to your vite.config.js
file within the Worker plugins configuration:
import { defineConfig } from 'vite'
import emscriptenStaticWorkerOptions from './your-file-with-the-plugin-code.js'
export default defineConfig({
worker: {
format: "es",
plugins: [
emscriptenStaticWorkerOptions()
]
}
})
This plugin transforms code that uses dynamic worker options into static values at build time. For example:
var workerOptions = { type: "module",name: "em-pthread" };
//...
worker=new Worker(new URL("wasm-module.js",import.meta.url),workerOptions);
// Output: Transformed code
worker=new Worker(new URL("wasm-module.js",import.meta.url),{ type: "module",name: "em-pthread" });
The plugin optionally accepts the following options:
include
: Array of patterns (RegExp or glob strings) to include in transformation- Default:
[/\.[jt]s$/]
- Default:
exclude
: Array of patterns (RegExp or glob strings) to exclude from transformation- Default:
[]
- Default:
The plugin supports both RegExp and glob string patterns:
// Using RegExp
include: [/\.worker\.js$/]
// Using glob strings
include: ['src/*.worker.js']
- It only processes emscripten presenting files
- By default plugin is run on all .js and .ts files. This can be further customised with the include and exclude options.
- The original
workerOptions
declaration is removed from the code