Skip to content

Commit ddefc06

Browse files
authoredApr 7, 2023
fix(worker): asset in iife worker and relative base (#12697)
1 parent 063d93b commit ddefc06

File tree

7 files changed

+62
-1
lines changed

7 files changed

+62
-1
lines changed
 

‎packages/vite/src/node/build.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,12 @@ const relativeUrlMechanisms: Record<
11001100
}
11011101
/* end of copy */
11021102

1103+
const customRelativeUrlMechanisms = {
1104+
...relativeUrlMechanisms,
1105+
'worker-iife': (relativePath) =>
1106+
getResolveUrl(`'${relativePath}', self.location.href`),
1107+
} as const satisfies Record<string, (relativePath: string) => string>
1108+
11031109
export type RenderBuiltAssetUrl = (
11041110
filename: string,
11051111
type: {
@@ -1149,8 +1155,10 @@ export function toOutputFilePathInJS(
11491155

11501156
export function createToImportMetaURLBasedRelativeRuntime(
11511157
format: InternalModuleFormat,
1158+
isWorker: boolean,
11521159
): (filename: string, importer: string) => { runtime: string } {
1153-
const toRelativePath = relativeUrlMechanisms[format]
1160+
const formatLong = isWorker && format === 'iife' ? 'worker-iife' : format
1161+
const toRelativePath = customRelativeUrlMechanisms[formatLong]
11541162
return (filename, importer) => ({
11551163
runtime: toRelativePath(
11561164
path.posix.relative(path.dirname(importer), filename),

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

+1
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

+1
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
369369
if (code.match(workerAssetUrlRE)) {
370370
const toRelativeRuntime = createToImportMetaURLBasedRelativeRuntime(
371371
outputOptions.format,
372+
config.isWorker,
372373
)
373374

374375
let match: RegExpExecArray | null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { test } from 'vitest'
2+
import { isBuild, page, untilUpdated } from '~utils'
3+
4+
test('asset url', async () => {
5+
await untilUpdated(
6+
() => page.textContent('.asset-url'),
7+
isBuild ? '/worker-assets/worker_asset-vite' : '/vite.svg',
8+
true,
9+
)
10+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from '../../vite.config-relative-base-iife'

‎playground/worker/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
"dev:relative-base": "WORKER_MODE=inline vite --config ./vite.config-relative-base.js dev",
2222
"build:relative-base": "WORKER_MODE=inline vite --config ./vite.config-relative-base.js build",
2323
"preview:relative-base": "WORKER_MODE=inline vite --config ./vite.config-relative-base.js preview",
24+
"dev:relative-base-iife": "WORKER_MODE=inline vite --config ./vite.config-relative-base-iife.js dev",
25+
"build:relative-base-iife": "WORKER_MODE=inline vite --config ./vite.config-relative-base-iife.js build",
26+
"preview:relative-base-iife": "WORKER_MODE=inline vite --config ./vite.config-relative-base-iife.js preview",
2427
"debug": "node --inspect-brk ../../packages/vite/bin/vite"
2528
},
2629
"dependencies": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import vite from 'vite'
2+
import workerPluginTestPlugin from './worker-plugin-test-plugin'
3+
4+
export default vite.defineConfig({
5+
base: './',
6+
resolve: {
7+
alias: {
8+
'@': __dirname,
9+
},
10+
},
11+
worker: {
12+
format: 'iife',
13+
plugins: [workerPluginTestPlugin()],
14+
rollupOptions: {
15+
output: {
16+
assetFileNames: 'worker-assets/worker_asset-[name]-[hash].[ext]',
17+
chunkFileNames: 'worker-chunks/worker_chunk-[name]-[hash].js',
18+
entryFileNames: 'worker-entries/worker_entry-[name]-[hash].js',
19+
},
20+
},
21+
},
22+
build: {
23+
outDir: 'dist/relative-base-iife',
24+
rollupOptions: {
25+
output: {
26+
assetFileNames: 'other-assets/[name]-[hash].[ext]',
27+
chunkFileNames: 'chunks/[name]-[hash].js',
28+
entryFileNames: 'entries/[name]-[hash].js',
29+
},
30+
},
31+
},
32+
testConfig: {
33+
baseRoute: '/relative-base-iife/',
34+
},
35+
plugins: [workerPluginTestPlugin()],
36+
cacheDir: 'node_modules/.vite-relative-base-iife',
37+
})

0 commit comments

Comments
 (0)
Please sign in to comment.