Skip to content

Commit cba5dd0

Browse files
committed
fix(worker): asset in iife worker and relative base
1 parent 1638ebd commit cba5dd0

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

packages/vite/src/node/build.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,12 @@ const relativeUrlMechanisms: Record<
10761076
}
10771077
/* end of copy */
10781078

1079+
const customRelativeUrlMechanisms = {
1080+
...relativeUrlMechanisms,
1081+
'worker-iife': (relativePath) =>
1082+
getResolveUrl(`'${relativePath}', self.location.href`),
1083+
} as const satisfies Record<string, (relativePath: string) => string>
1084+
10791085
export type RenderBuiltAssetUrl = (
10801086
filename: string,
10811087
type: {
@@ -1125,8 +1131,10 @@ export function toOutputFilePathInJS(
11251131

11261132
export function createToImportMetaURLBasedRelativeRuntime(
11271133
format: InternalModuleFormat,
1134+
isWorker: boolean,
11281135
): (filename: string, importer: string) => { runtime: string } {
1129-
const toRelativePath = relativeUrlMechanisms[format]
1136+
const formatLong = isWorker && format === 'iife' ? 'worker-iife' : format
1137+
const toRelativePath = customRelativeUrlMechanisms[formatLong]
11301138
return (filename, importer) => ({
11311139
runtime: toRelativePath(
11321140
path.posix.relative(path.dirname(importer), filename),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export function renderAssetUrlInJS(
6969
): MagicString | undefined {
7070
const toRelativeRuntime = createToImportMetaURLBasedRelativeRuntime(
7171
opts.format,
72+
config.isWorker,
7273
)
7374

7475
let match: RegExpExecArray | null

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
355355
if (code.match(workerAssetUrlRE) || code.includes('import.meta.url')) {
356356
const toRelativeRuntime = createToImportMetaURLBasedRelativeRuntime(
357357
outputOptions.format,
358+
config.isWorker,
358359
)
359360

360361
let match: RegExpExecArray | null

0 commit comments

Comments
 (0)