Skip to content

Commit a428f8d

Browse files
authored
fix(web): correctly resolve assets in new URL (#3950)
1 parent 867dbf6 commit a428f8d

File tree

7 files changed

+49
-18
lines changed

7 files changed

+49
-18
lines changed

packages/vitest/src/node/plugins/ssrReplacer.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { cleanUrl } from 'vite-node/utils'
77
// import.meta.env.VITE_NAME = 'app' -> process.env.VITE_NAME = 'app'
88
export function SsrReplacerPlugin(): Plugin {
99
return {
10-
name: 'vitest:env-replacer',
10+
name: 'vitest:ssr-replacer',
1111
enforce: 'pre',
1212
transform(code, id) {
1313
if (!/\bimport\.meta\.env\b/.test(code) && !/\bimport\.meta\.url\b/.test(code))
@@ -26,17 +26,6 @@ export function SsrReplacerPlugin(): Plugin {
2626
s.overwrite(startIndex, endIndex, 'process.env')
2727
}
2828

29-
const urls = cleanCode.matchAll(/\bimport\.meta\.url\b/g)
30-
31-
for (const env of urls) {
32-
s ||= new MagicString(code)
33-
34-
const startIndex = env.index!
35-
const endIndex = startIndex + env[0].length
36-
37-
s.overwrite(startIndex, endIndex, '__vite_ssr_import_meta__.url')
38-
}
39-
4029
if (s) {
4130
return {
4231
code: s.toString(),

packages/web-worker/src/shared-worker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { MessageChannel, type MessagePort as NodeMessagePort } from 'node:worker_threads'
22
import type { InlineWorkerContext, Procedure } from './types'
33
import { InlineWorkerRunner } from './runner'
4-
import { debug, getRunnerOptions } from './utils'
4+
import { debug, getFileIdFromUrl, getRunnerOptions } from './utils'
55

66
interface SharedInlineWorkerContext extends Omit<InlineWorkerContext, 'onmessage' | 'postMessage' | 'self' | 'global'> {
77
onconnect: Procedure | null
@@ -101,7 +101,7 @@ export function createSharedWorkerConstructor(): typeof SharedWorker {
101101

102102
const runner = new InlineWorkerRunner(runnerOptions, context)
103103

104-
const id = (url instanceof URL ? url.toString() : url).replace(/^file:\/+/, '/')
104+
const id = getFileIdFromUrl(url)
105105

106106
this._vw_name = id
107107

packages/web-worker/src/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,11 @@ export function getRunnerOptions(): any {
8080
state,
8181
}
8282
}
83+
84+
export function getFileIdFromUrl(url: URL | string) {
85+
if (!(url instanceof URL))
86+
url = new URL(url, self.location.origin)
87+
if (url.protocol === 'http:' || url.protocol === 'https:')
88+
return url.pathname
89+
return url.toString().replace(/^file:\/+/, '/')
90+
}

packages/web-worker/src/worker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { CloneOption, DefineWorkerOptions, InlineWorkerContext, Procedure } from './types'
22
import { InlineWorkerRunner } from './runner'
3-
import { createMessageEvent, debug, getRunnerOptions } from './utils'
3+
import { createMessageEvent, debug, getFileIdFromUrl, getRunnerOptions } from './utils'
44

55
export function createWorkerConstructor(options?: DefineWorkerOptions): typeof Worker {
66
const runnerOptions = getRunnerOptions()
@@ -66,7 +66,7 @@ export function createWorkerConstructor(options?: DefineWorkerOptions): typeof W
6666

6767
const runner = new InlineWorkerRunner(runnerOptions, context)
6868

69-
const id = (url instanceof URL ? url.toString() : url).replace(/^file:\/+/, '/')
69+
const id = getFileIdFromUrl(url)
7070

7171
this._vw_name = id
7272

test/core/test/url-ssr.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// @vitest-environment node
2+
3+
import { fileURLToPath, pathToFileURL } from 'node:url'
4+
import { dirname, resolve } from 'pathe'
5+
import { expect, it } from 'vitest'
6+
7+
it('correctly resolves new assets URL paths', () => {
8+
const urlCss = new URL('../src/file-css.css', import.meta.url)
9+
expect(urlCss.toString()).toBe(
10+
pathToFileURL(resolve(dirname(fileURLToPath(import.meta.url)), '../src/file-css.css')).toString(),
11+
)
12+
})
13+
14+
it('doesn\'t resolve aliases for new URL in SSR', () => {
15+
const urlAlias = new URL('#/file-css.css', import.meta.url)
16+
expect(urlAlias.toString()).toBe(
17+
pathToFileURL(`${fileURLToPath(import.meta.url)}#/file-css.css`).toString().replace('%23', '#'),
18+
)
19+
})

test/core/test/url-web.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @vitest-environment jsdom
2+
3+
import { expect, it } from 'vitest'
4+
5+
it('correctly resolves new assets URL paths', () => {
6+
const urlCss = new URL('../src/file-css.css', import.meta.url)
7+
expect(urlCss.toString()).toBe('http://localhost:3000/src/file-css.css')
8+
})
9+
10+
it('correctly resolves aliased URL paths', () => {
11+
const urlAlias = new URL('#/file-css.css', import.meta.url)
12+
expect(urlAlias.toString()).toBe('http://localhost:3000/src/file-css.css')
13+
})

test/web-worker/test/init.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,13 @@ it('self injected into worker and its deps should be equal', async () => {
7575
expect.assertions(4)
7676
expect(await testSelfWorker(new MySelfWorker())).toBeTruthy()
7777
// wait for clear worker mod cache
78-
await sleep(500)
78+
await sleep(0)
7979
expect(await testSelfWorker(new MySelfWorker())).toBeTruthy()
8080

81+
await sleep(0)
82+
8183
expect(await testSelfWorker(new Worker(new URL('../src/selfWorker.ts', import.meta.url)))).toBeTruthy()
8284
// wait for clear worker mod cache
83-
await sleep(500)
85+
await sleep(0)
8486
expect(await testSelfWorker(new Worker(new URL('../src/selfWorker.ts', import.meta.url)))).toBeTruthy()
8587
})

0 commit comments

Comments
 (0)