Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(worker): bundle worker emit asset file #6599

Merged
4 changes: 4 additions & 0 deletions packages/playground/wasm/__tests__/wasm.spec.ts
Expand Up @@ -9,3 +9,7 @@ test('should work when output', async () => {
await page.click('.output-wasm .run')
await untilUpdated(() => page.textContent('.output-wasm .result'), '24')
})

test('should work when wasm in worker', async () => {
await untilUpdated(() => page.textContent('.worker-wasm .result'), '3')
})
Binary file added packages/playground/wasm/add.wasm
Binary file not shown.
15 changes: 15 additions & 0 deletions packages/playground/wasm/index.html
Expand Up @@ -12,9 +12,20 @@ <h3>When wasm is output, result should be 24</h3>
<span class="result"></span>
</div>

<div class="worker-wasm">
<h3>worker wasm</h3>
<span class="result"></span>
</div>

<script type="module">
import light from './light.wasm'
import heavy from './heavy.wasm'
import myWorker from './worker?worker'

const w = new myWorker()
w.addEventListener('message', (ev) => {
text('.worker-wasm .result', ev.data.result)
})

async function testWasm(init, resultElement) {
const { exported_func } = await init({
Expand All @@ -25,6 +36,10 @@ <h3>When wasm is output, result should be 24</h3>
exported_func()
}

function text(el, text) {
document.querySelector(el).textContent = text
}

document
.querySelector('.inline-wasm .run')
.addEventListener('click', async () =>
Expand Down
8 changes: 8 additions & 0 deletions packages/playground/wasm/vite.config.ts
@@ -0,0 +1,8 @@
import { defineConfig } from 'vite'
export default defineConfig({
build: {
// make can no emit light.wasm
// and emit add.wasm
assetsInlineLimit: 80
}
})
5 changes: 5 additions & 0 deletions packages/playground/wasm/worker.js
@@ -0,0 +1,5 @@
import init from './add.wasm'
init().then((exports) => {
// eslint-disable-next-line no-undef
self.postMessage({ result: exports.add(1, 2) })
})
17 changes: 14 additions & 3 deletions packages/vite/src/node/plugins/worker.ts
Expand Up @@ -6,10 +6,12 @@ import type Rollup from 'rollup'
import { ENV_PUBLIC_PATH } from '../constants'
import path from 'path'
import { onRollupWarning } from '../build'
import { registerAssetToChunk } from './asset'

const WorkerFileId = 'worker_file'

export async function bundleWorkerEntry(
ctx: Rollup.TransformPluginContext,
config: ResolvedConfig,
id: string
): Promise<Buffer> {
Expand All @@ -27,11 +29,20 @@ export async function bundleWorkerEntry(
})
let code: string
try {
const { output } = await bundle.generate({
const {
output: [outputCode, ...outputChunks]
} = await bundle.generate({
format,
sourcemap: config.build.sourcemap
})
code = output[0].code
code = outputCode.code
outputChunks.forEach((outputChunk) => {
if (outputChunk.type === 'chunk') {
registerAssetToChunk(outputChunk, outputChunk.name)
} else if (outputChunk.type === 'asset') {
ctx.emitFile(outputChunk)
}
})
} finally {
await bundle.close()
}
Expand Down Expand Up @@ -72,7 +83,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {

let url: string
if (isBuild) {
const code = await bundleWorkerEntry(config, id)
const code = await bundleWorkerEntry(this, config, id)
if (query.inline != null) {
const { format } = config.worker
const workerOptions = format === 'es' ? '{type: "module"}' : '{}'
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/workerImportMetaUrl.ts
Expand Up @@ -65,7 +65,7 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
const file = path.resolve(path.dirname(id), rawUrl.slice(1, -1))
let url: string
if (isBuild) {
const content = await bundleWorkerEntry(config, file)
const content = await bundleWorkerEntry(this, config, file)
const basename = path.parse(cleanUrl(file)).name
const contentHash = getAssetHash(content)
const fileName = path.posix.join(
Expand Down