Skip to content

Commit

Permalink
fix(worker): asset in iife worker and relative base (#12697)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Apr 7, 2023
1 parent 063d93b commit ddefc06
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/vite/src/node/build.ts
Expand Up @@ -1100,6 +1100,12 @@ const relativeUrlMechanisms: Record<
}
/* end of copy */

const customRelativeUrlMechanisms = {
...relativeUrlMechanisms,
'worker-iife': (relativePath) =>
getResolveUrl(`'${relativePath}', self.location.href`),
} as const satisfies Record<string, (relativePath: string) => string>

export type RenderBuiltAssetUrl = (
filename: string,
type: {
Expand Down Expand Up @@ -1149,8 +1155,10 @@ export function toOutputFilePathInJS(

export function createToImportMetaURLBasedRelativeRuntime(
format: InternalModuleFormat,
isWorker: boolean,
): (filename: string, importer: string) => { runtime: string } {
const toRelativePath = relativeUrlMechanisms[format]
const formatLong = isWorker && format === 'iife' ? 'worker-iife' : format
const toRelativePath = customRelativeUrlMechanisms[formatLong]
return (filename, importer) => ({
runtime: toRelativePath(
path.posix.relative(path.dirname(importer), filename),
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/plugins/asset.ts
Expand Up @@ -69,6 +69,7 @@ export function renderAssetUrlInJS(
): MagicString | undefined {
const toRelativeRuntime = createToImportMetaURLBasedRelativeRuntime(
opts.format,
config.isWorker,
)

let match: RegExpExecArray | null
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/plugins/worker.ts
Expand Up @@ -369,6 +369,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
if (code.match(workerAssetUrlRE)) {
const toRelativeRuntime = createToImportMetaURLBasedRelativeRuntime(
outputOptions.format,
config.isWorker,
)

let match: RegExpExecArray | null
Expand Down
@@ -0,0 +1,10 @@
import { test } from 'vitest'
import { isBuild, page, untilUpdated } from '~utils'

test('asset url', async () => {
await untilUpdated(
() => page.textContent('.asset-url'),
isBuild ? '/worker-assets/worker_asset-vite' : '/vite.svg',
true,
)
})
@@ -0,0 +1 @@
export { default } from '../../vite.config-relative-base-iife'
3 changes: 3 additions & 0 deletions playground/worker/package.json
Expand Up @@ -21,6 +21,9 @@
"dev:relative-base": "WORKER_MODE=inline vite --config ./vite.config-relative-base.js dev",
"build:relative-base": "WORKER_MODE=inline vite --config ./vite.config-relative-base.js build",
"preview:relative-base": "WORKER_MODE=inline vite --config ./vite.config-relative-base.js preview",
"dev:relative-base-iife": "WORKER_MODE=inline vite --config ./vite.config-relative-base-iife.js dev",
"build:relative-base-iife": "WORKER_MODE=inline vite --config ./vite.config-relative-base-iife.js build",
"preview:relative-base-iife": "WORKER_MODE=inline vite --config ./vite.config-relative-base-iife.js preview",
"debug": "node --inspect-brk ../../packages/vite/bin/vite"
},
"dependencies": {
Expand Down
37 changes: 37 additions & 0 deletions playground/worker/vite.config-relative-base-iife.js
@@ -0,0 +1,37 @@
import vite from 'vite'
import workerPluginTestPlugin from './worker-plugin-test-plugin'

export default vite.defineConfig({
base: './',
resolve: {
alias: {
'@': __dirname,
},
},
worker: {
format: 'iife',
plugins: [workerPluginTestPlugin()],
rollupOptions: {
output: {
assetFileNames: 'worker-assets/worker_asset-[name]-[hash].[ext]',
chunkFileNames: 'worker-chunks/worker_chunk-[name]-[hash].js',
entryFileNames: 'worker-entries/worker_entry-[name]-[hash].js',
},
},
},
build: {
outDir: 'dist/relative-base-iife',
rollupOptions: {
output: {
assetFileNames: 'other-assets/[name]-[hash].[ext]',
chunkFileNames: 'chunks/[name]-[hash].js',
entryFileNames: 'entries/[name]-[hash].js',
},
},
},
testConfig: {
baseRoute: '/relative-base-iife/',
},
plugins: [workerPluginTestPlugin()],
cacheDir: 'node_modules/.vite-relative-base-iife',
})

0 comments on commit ddefc06

Please sign in to comment.