Skip to content

Commit f3d3149

Browse files
authored
feat(bundled-dev): support worker in initial bundle (#21415)
1 parent 9fcde3c commit f3d3149

2 files changed

Lines changed: 40 additions & 17 deletions

File tree

packages/vite/src/node/plugins/asset.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
withTrailingSlash,
4141
} from '../../shared/utils'
4242
import type { Environment } from '../environment'
43+
import type { PartialEnvironment } from '../baseEnvironment'
4344

4445
// referenceId is base64url but replaces - with $
4546
export const assetUrlRE: RegExp = /__VITE_ASSET__([\w$]+)__(?:\$_(.*?)__)?/g
@@ -473,18 +474,7 @@ async function fileToBuiltUrl(
473474
environment.config.experimental.bundledDev
474475
) {
475476
const outputFilename = pluginContext.getFileName(referenceId)
476-
const outputUrl = toOutputFilePathInJS(
477-
environment,
478-
outputFilename,
479-
'asset',
480-
'assets/dummy.js',
481-
'js',
482-
() => {
483-
throw new Error('unreachable')
484-
},
485-
)
486-
if (typeof outputUrl === 'object') throw new Error('unreachable')
487-
url = outputUrl
477+
url = toOutputFilePathInJSForBundledDev(environment, outputFilename)
488478
} else {
489479
url = `__VITE_ASSET__${referenceId}__${postfix ? `$_${postfix}__` : ``}`
490480
}
@@ -494,6 +484,27 @@ async function fileToBuiltUrl(
494484
return url
495485
}
496486

487+
export function toOutputFilePathInJSForBundledDev(
488+
environment: PartialEnvironment,
489+
filename: string,
490+
): string {
491+
const outputUrl = toOutputFilePathInJS(
492+
environment,
493+
filename,
494+
'asset',
495+
// in bundled dev, the chunks are always emitted to `assets` directory
496+
'assets/dummy.js',
497+
'js',
498+
// relative base is not supported in bundled dev
499+
() => {
500+
throw new Error('unreachable')
501+
},
502+
)
503+
// renderBuiltUrl is not supported in bundled dev
504+
if (typeof outputUrl === 'object') throw new Error('unreachable')
505+
return outputUrl
506+
}
507+
497508
export async function urlToBuiltUrl(
498509
pluginContext: PluginContext,
499510
url: string,

packages/vite/src/node/plugins/worker.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
} from '../build'
2626
import { cleanUrl } from '../../shared/utils'
2727
import type { Logger } from '../logger'
28-
import { fileToUrl } from './asset'
28+
import { fileToUrl, toOutputFilePathInJSForBundledDev } from './asset'
2929

3030
type WorkerBundle = {
3131
entryFilename: string
@@ -401,7 +401,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
401401
const { format } = config.worker
402402
const workerConstructor =
403403
workerMatch[1] === 'sharedworker' ? 'SharedWorker' : 'Worker'
404-
const workerType = isBuild
404+
const workerType = config.isBundled
405405
? format === 'es'
406406
? 'module'
407407
: 'classic'
@@ -412,7 +412,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
412412
}`
413413

414414
let urlCode: string
415-
if (isBuild) {
415+
if (config.isBundled) {
416416
if (isWorker && config.bundleChain.at(-1) === cleanUrl(id)) {
417417
urlCode = 'self.location.href'
418418
} else if (inlineRE.test(id)) {
@@ -467,7 +467,19 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
467467
}
468468
} else {
469469
const result = await workerFileToUrl(config, id)
470-
urlCode = JSON.stringify(result.entryUrlPlaceholder)
470+
let url: string
471+
if (
472+
this.environment.config.command === 'serve' &&
473+
this.environment.config.experimental.bundledDev
474+
) {
475+
url = toOutputFilePathInJSForBundledDev(
476+
this.environment,
477+
result.entryFilename,
478+
)
479+
} else {
480+
url = result.entryUrlPlaceholder
481+
}
482+
urlCode = JSON.stringify(url)
471483
for (const file of result.watchedFiles) {
472484
this.addWatchFile(file)
473485
}
@@ -517,7 +529,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
517529
const scriptPath = JSON.stringify(ENV_PUBLIC_PATH)
518530
injectEnv = `import ${scriptPath}\n`
519531
} else if (workerType === 'ignore') {
520-
if (isBuild) {
532+
if (config.isBundled) {
521533
injectEnv = ''
522534
} else {
523535
// dynamic worker type we can't know how import the env

0 commit comments

Comments
 (0)