Skip to content

Commit

Permalink
fix: import.meta.url in worker (#7464)
Browse files Browse the repository at this point in the history
  • Loading branch information
poyoho committed Mar 27, 2022
1 parent 9ee2cf6 commit 8ac4b12
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
7 changes: 5 additions & 2 deletions packages/playground/worker/__tests__/worker.spec.ts
Expand Up @@ -51,8 +51,11 @@ test.concurrent.each([[true], [false]])('shared worker', async (doTick) => {
await waitSharedWorkerTick(page)
})

test('worker emitted', async () => {
await untilUpdated(() => page.textContent('.nested-worker'), 'pong')
test('worker emitted and import.meta.url in nested worker', async () => {
await untilUpdated(
() => page.textContent('.nested-worker'),
'pong http://localhost:3000/iife/sub-worker.js?worker_file'
)
})

if (isBuild) {
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/worker/sub-worker.js
@@ -1,5 +1,5 @@
self.onmessage = (event) => {
if (event.data === 'ping') {
self.postMessage('pong')
self.postMessage(`pong ${import.meta.url}`)
}
}
2 changes: 1 addition & 1 deletion packages/playground/worker/url-worker.js
@@ -1 +1 @@
self.postMessage('A string' + import.meta.env.BASE_URL)
self.postMessage('A string' + import.meta.env.BASE_URL + import.meta.url)
8 changes: 7 additions & 1 deletion packages/vite/src/node/plugins/define.ts
Expand Up @@ -10,6 +10,7 @@ const isNonJsRequest = (request: string): boolean => nonJsRe.test(request)

export function definePlugin(config: ResolvedConfig): Plugin {
const isBuild = config.command === 'build'
const isWorker = config.isWorker

const processNodeEnv: Record<string, string> = {
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || config.mode),
Expand Down Expand Up @@ -40,7 +41,12 @@ export function definePlugin(config: ResolvedConfig): Plugin {
Object.assign(importMetaKeys, {
'import.meta.env.': `({}).`,
'import.meta.env': JSON.stringify(config.env),
'import.meta.hot': `false`
'import.meta.hot': `false`,
...(isWorker
? {
'import.meta.url': 'self.location.href'
}
: {})
})
}

Expand Down

0 comments on commit 8ac4b12

Please sign in to comment.