[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

fix(web): correctly resolve assets in new URL (#3950)

authored by

Vladimir and committed by
GitHub
(Aug 14, 2023, 4:29 PM +0200) a428f8d4 867dbf60

+49 -18
+2 -2
packages/web-worker/src/shared-worker.ts
··· 1 1 import { MessageChannel, type MessagePort as NodeMessagePort } from 'node:worker_threads' 2 2 import type { InlineWorkerContext, Procedure } from './types' 3 3 import { InlineWorkerRunner } from './runner' 4 - import { debug, getRunnerOptions } from './utils' 4 + import { debug, getFileIdFromUrl, getRunnerOptions } from './utils' 5 5 6 6 interface SharedInlineWorkerContext extends Omit<InlineWorkerContext, 'onmessage' | 'postMessage' | 'self' | 'global'> { 7 7 onconnect: Procedure | null ··· 101 101 102 102 const runner = new InlineWorkerRunner(runnerOptions, context) 103 103 104 - const id = (url instanceof URL ? url.toString() : url).replace(/^file:\/+/, '/') 104 + const id = getFileIdFromUrl(url) 105 105 106 106 this._vw_name = id 107 107
+8
packages/web-worker/src/utils.ts
··· 80 80 state, 81 81 } 82 82 } 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 + }
+2 -2
packages/web-worker/src/worker.ts
··· 1 1 import type { CloneOption, DefineWorkerOptions, InlineWorkerContext, Procedure } from './types' 2 2 import { InlineWorkerRunner } from './runner' 3 - import { createMessageEvent, debug, getRunnerOptions } from './utils' 3 + import { createMessageEvent, debug, getFileIdFromUrl, getRunnerOptions } from './utils' 4 4 5 5 export function createWorkerConstructor(options?: DefineWorkerOptions): typeof Worker { 6 6 const runnerOptions = getRunnerOptions() ··· 66 66 67 67 const runner = new InlineWorkerRunner(runnerOptions, context) 68 68 69 - const id = (url instanceof URL ? url.toString() : url).replace(/^file:\/+/, '/') 69 + const id = getFileIdFromUrl(url) 70 70 71 71 this._vw_name = id 72 72
+19
test/core/test/url-ssr.test.ts
··· 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 + })
+13
test/core/test/url-web.test.ts
··· 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 + })
+4 -2
test/web-worker/test/init.test.ts
··· 75 75 expect.assertions(4) 76 76 expect(await testSelfWorker(new MySelfWorker())).toBeTruthy() 77 77 // wait for clear worker mod cache 78 - await sleep(500) 78 + await sleep(0) 79 79 expect(await testSelfWorker(new MySelfWorker())).toBeTruthy() 80 + 81 + await sleep(0) 80 82 81 83 expect(await testSelfWorker(new Worker(new URL('../src/selfWorker.ts', import.meta.url)))).toBeTruthy() 82 84 // wait for clear worker mod cache 83 - await sleep(500) 85 + await sleep(0) 84 86 expect(await testSelfWorker(new Worker(new URL('../src/selfWorker.ts', import.meta.url)))).toBeTruthy() 85 87 })
+1 -12
packages/vitest/src/node/plugins/ssrReplacer.ts
··· 7 7 // import.meta.env.VITE_NAME = 'app' -> process.env.VITE_NAME = 'app' 8 8 export function SsrReplacerPlugin(): Plugin { 9 9 return { 10 - name: 'vitest:env-replacer', 10 + name: 'vitest:ssr-replacer', 11 11 enforce: 'pre', 12 12 transform(code, id) { 13 13 if (!/\bimport\.meta\.env\b/.test(code) && !/\bimport\.meta\.url\b/.test(code)) ··· 24 24 const endIndex = startIndex + env[0].length 25 25 26 26 s.overwrite(startIndex, endIndex, 'process.env') 27 - } 28 - 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 27 } 39 28 40 29 if (s) {