Skip to content

Commit

Permalink
fix: emit missing web worker chunks
Browse files Browse the repository at this point in the history
closes #6706
  • Loading branch information
pd4d10 committed Feb 3, 2022
1 parent ff37c03 commit beed060
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
21 changes: 17 additions & 4 deletions packages/vite/src/node/plugins/worker.ts
Expand Up @@ -11,7 +11,8 @@ const WorkerFileId = 'worker_file'

export async function bundleWorkerEntry(
config: ResolvedConfig,
id: string
id: string,
ctx: Rollup.TransformPluginContext
): Promise<Buffer> {
// bundle the file as entry to support imports
const rollup = require('rollup') as typeof Rollup
Expand All @@ -25,13 +26,25 @@ export async function bundleWorkerEntry(
},
preserveEntrySignatures: false
})
let code: string
let code = ''
try {
const { output } = await bundle.generate({
format,
sourcemap: config.build.sourcemap
})
code = output[0].code

output.forEach((item) => {
if (item.type === 'chunk' && item.isEntry) {
code = item.code
return
}

ctx.emitFile({
type: 'asset',
fileName: path.posix.join(config.build.assetsDir, item.fileName),
source: item.type === 'chunk' ? item.code : item.source
})
})
} finally {
await bundle.close()
}
Expand Down Expand Up @@ -72,7 +85,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {

let url: string
if (isBuild) {
const code = await bundleWorkerEntry(config, id)
const code = await bundleWorkerEntry(config, id, this)
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(config, file, this)
const basename = path.parse(cleanUrl(file)).name
const contentHash = getAssetHash(content)
const fileName = path.posix.join(
Expand Down

0 comments on commit beed060

Please sign in to comment.