Skip to content

Commit 26ecd5a

Browse files
authored
fix: objurl for type module, and concurrent tests (#8541)
1 parent b85802a commit 26ecd5a

20 files changed

+301
-232
lines changed

packages/vite/src/node/plugins/worker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
281281
export default function WorkerWrapper() {
282282
const objURL = blob && (window.URL || window.webkitURL).createObjectURL(blob);
283283
try {
284-
return objURL ? new ${workerConstructor}(objURL${workerOptions}) : new ${workerConstructor}("data:application/javascript;base64," + encodedJs${workerOptions});
284+
return objURL ? new ${workerConstructor}(objURL) : new ${workerConstructor}("data:application/javascript;base64," + encodedJs${workerOptions});
285285
} finally {
286286
objURL && (window.URL || window.webkitURL).revokeObjectURL(objURL);
287287
}

playground/assets/__tests__/relative-base/relative-base-assets.spec.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
getBg,
66
getColor,
77
isBuild,
8-
page
8+
page,
9+
viteConfig
910
} from '~utils'
1011

1112
const absoluteAssetMatch = isBuild
@@ -137,7 +138,8 @@ describe('css url() references', () => {
137138
describe.runIf(isBuild)('index.css URLs', () => {
138139
let css: string
139140
beforeAll(() => {
140-
css = findAssetFile(/index.*\.css$/, '', 'other-assets')
141+
const base = viteConfig ? viteConfig?.testConfig?.baseRoute : ''
142+
css = findAssetFile(/index.*\.css$/, base, 'other-assets')
141143
})
142144

143145
test('relative asset URL', () => {

playground/assets/vite.config-relative-base.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = {
88
base: './', // relative base to make dist portable
99
build: {
1010
...baseConfig.build,
11-
outDir: 'dist',
11+
outDir: 'dist/relative-base',
1212
watch: false,
1313
minify: false,
1414
assetsInlineLimit: 0,
@@ -19,5 +19,9 @@ module.exports = {
1919
assetFileNames: 'other-assets/[name].[hash][extname]'
2020
}
2121
}
22-
}
22+
},
23+
testConfig: {
24+
baseRoute: '/relative-base/'
25+
},
26+
cacheDir: 'node_modules/.vite/relative-base'
2327
}

playground/assets/vite.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ module.exports = {
1717
assetsInlineLimit: 8192, // 8kb
1818
manifest: true,
1919
watch: {}
20-
}
20+
},
21+
cacheDir: 'node_modules/.vite/foo'
2122
}

playground/test-utils.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export async function untilUpdated(
150150
runInBuild = false
151151
): Promise<void> {
152152
if (isBuild && !runInBuild) return
153-
const maxTries = process.env.CI ? 100 : 50
153+
const maxTries = process.env.CI ? 200 : 50
154154
for (let tries = 0; tries < maxTries; tries++) {
155155
const actual = (await poll()) ?? ''
156156
if (actual.indexOf(expected) > -1 || tries === maxTries - 1) {
@@ -162,12 +162,12 @@ export async function untilUpdated(
162162
}
163163
}
164164

165-
export const extractSourcemap = (content: string) => {
165+
export const extractSourcemap = (content: string): any => {
166166
const lines = content.trim().split('\n')
167167
return fromComment(lines[lines.length - 1]).toObject()
168168
}
169169

170-
export const formatSourcemapForSnapshot = (map: any) => {
170+
export const formatSourcemapForSnapshot = (map: any): any => {
171171
const root = normalizePath(testDir)
172172
const m = { ...map }
173173
delete m.file

playground/vitestGlobalSetup.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const DIR = path.join(os.tmpdir(), 'vitest_playwright_global_setup')
88

99
let browserServer: BrowserServer | undefined
1010

11-
export async function setup() {
11+
export async function setup(): Promise<void> {
1212
browserServer = await chromium.launchServer({
1313
headless: !process.env.VITE_DEBUG_SERVE,
1414
args: process.env.CI
@@ -41,7 +41,7 @@ export async function setup() {
4141
})
4242
}
4343

44-
export async function teardown() {
44+
export async function teardown(): Promise<void> {
4545
browserServer?.close()
4646
if (!process.env.VITE_PRESERVE_BUILD_ARTIFACTS) {
4747
fs.removeSync(path.resolve(__dirname, '../playground-temp'))

playground/vitestSetup.ts

+32-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as http from 'http'
2-
import { dirname, resolve } from 'path'
2+
import path, { dirname, resolve } from 'path'
33
import os from 'os'
4-
import path from 'path'
54
import sirv from 'sirv'
65
import fs from 'fs-extra'
76
import { chromium } from 'playwright-chromium'
@@ -25,7 +24,10 @@ export const workspaceRoot = path.resolve(__dirname, '../')
2524
export const isBuild = !!process.env.VITE_TEST_BUILD
2625
export const isServe = !isBuild
2726
export const isWindows = process.platform === 'win32'
28-
export const viteBinPath = path.join(workspaceRoot, 'packages/vite/bin/vite.js')
27+
export const viteBinPath = path.posix.join(
28+
workspaceRoot,
29+
'packages/vite/bin/vite.js'
30+
)
2931

3032
// #endregion
3133

@@ -49,6 +51,11 @@ export let testDir: string
4951
* Test folder name
5052
*/
5153
export let testName: string
54+
/**
55+
* current test using vite inline config
56+
* when using server.js is not possible to get the config
57+
*/
58+
export let viteConfig: InlineConfig | undefined
5259

5360
export const serverLogs: string[] = []
5461
export const browserLogs: string[] = []
@@ -61,7 +68,17 @@ export let browser: Browser = undefined!
6168
export let viteTestUrl: string = ''
6269
export let watcher: RollupWatcher | undefined = undefined
6370

64-
export function setViteUrl(url: string) {
71+
declare module 'vite' {
72+
interface InlineConfig {
73+
testConfig?: {
74+
// relative base output use relative path
75+
// rewrite the url to truth file path
76+
baseRoute: string
77+
}
78+
}
79+
}
80+
81+
export function setViteUrl(url: string): void {
6582
viteTestUrl = url
6683
}
6784

@@ -156,7 +173,7 @@ beforeAll(async (s) => {
156173
}
157174
})
158175

159-
export async function startDefaultServe() {
176+
export async function startDefaultServe(): Promise<void> {
160177
const testCustomConfig = resolve(dirname(testPath), 'vite.config.js')
161178
let config: InlineConfig | undefined
162179
if (fs.existsSync(testCustomConfig)) {
@@ -193,9 +210,9 @@ export async function startDefaultServe() {
193210

194211
if (!isBuild) {
195212
process.env.VITE_INLINE = 'inline-serve'
196-
server = await (
197-
await createServer(mergeConfig(options, config || {}))
198-
).listen()
213+
const testConfig = mergeConfig(options, config || {})
214+
viteConfig = testConfig
215+
server = await (await createServer(testConfig)).listen()
199216
// use resolved port/base from server
200217
const base = server.config.base === '/' ? '' : server.config.base
201218
viteTestUrl = `http://localhost:${server.config.server.port}${base}`
@@ -210,7 +227,9 @@ export async function startDefaultServe() {
210227
}
211228
})
212229
options.plugins = [resolvedPlugin()]
213-
const rollupOutput = await build(mergeConfig(options, config || {}))
230+
const testConfig = mergeConfig(options, config || {})
231+
viteConfig = testConfig
232+
const rollupOutput = await build(testConfig)
214233
const isWatch = !!resolvedConfig!.build.watch
215234
// in build watch,call startStaticServer after the build is complete
216235
if (isWatch) {
@@ -245,11 +264,15 @@ function startStaticServer(config?: InlineConfig): Promise<string> {
245264

246265
// start static file server
247266
const serve = sirv(resolve(rootDir, 'dist'), { dev: !!config?.build?.watch })
267+
const baseDir = config?.testConfig?.baseRoute
248268
const httpServer = (server = http.createServer((req, res) => {
249269
if (req.url === '/ping') {
250270
res.statusCode = 200
251271
res.end('pong')
252272
} else {
273+
if (baseDir) {
274+
req.url = path.posix.join(baseDir, req.url)
275+
}
253276
serve(req, res)
254277
}
255278
}))
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,48 @@
11
import fs from 'fs'
22
import path from 'path'
3-
import type { Page } from 'playwright-chromium'
43
import { isBuild, page, testDir, untilUpdated } from '~utils'
54

65
test('normal', async () => {
7-
await page.click('.ping')
8-
await untilUpdated(() => page.textContent('.pong'), 'pong')
6+
await untilUpdated(() => page.textContent('.pong'), 'pong', true)
97
await untilUpdated(
108
() => page.textContent('.mode'),
11-
process.env.NODE_ENV // match workerImport.js
9+
process.env.NODE_ENV,
10+
true
1211
)
1312
await untilUpdated(
1413
() => page.textContent('.bundle-with-plugin'),
15-
'worker bundle with plugin success!'
14+
'worker bundle with plugin success!',
15+
true
1616
)
1717
})
1818

1919
test('TS output', async () => {
20-
await page.click('.ping-ts-output')
21-
await untilUpdated(() => page.textContent('.pong-ts-output'), 'pong')
20+
await untilUpdated(() => page.textContent('.pong-ts-output'), 'pong', true)
2221
})
2322

2423
test('inlined', async () => {
25-
await page.click('.ping-inline')
26-
await untilUpdated(() => page.textContent('.pong-inline'), 'pong')
24+
await untilUpdated(() => page.textContent('.pong-inline'), 'pong', true)
2725
})
2826

29-
const waitSharedWorkerTick = (
30-
(resolvedSharedWorkerCount: number) => async (page: Page) => {
31-
await untilUpdated(async () => {
32-
const count = await page.textContent('.tick-count')
33-
// ignore the initial 0
34-
return count === '1' ? 'page loaded' : ''
35-
}, 'page loaded')
36-
// test.concurrent sequential is not guaranteed
37-
// force page to wait to ensure two pages overlap in time
38-
resolvedSharedWorkerCount++
39-
if (resolvedSharedWorkerCount < 2) return
40-
41-
await untilUpdated(() => {
42-
return resolvedSharedWorkerCount === 2 ? 'all pages loaded' : ''
43-
}, 'all pages loaded')
44-
}
45-
)(0)
46-
47-
test.each([[true], [false]])('shared worker', async (doTick) => {
48-
if (doTick) {
49-
await page.click('.tick-shared')
50-
}
51-
await waitSharedWorkerTick(page)
27+
test('shared worker', async () => {
28+
await untilUpdated(() => page.textContent('.tick-count'), 'pong', true)
5229
})
5330

5431
test('worker emitted and import.meta.url in nested worker (serve)', async () => {
55-
expect(await page.textContent('.nested-worker')).toMatch(
56-
'worker-nested-worker'
32+
await untilUpdated(
33+
() => page.textContent('.nested-worker'),
34+
'worker-nested-worker',
35+
true
5736
)
58-
expect(await page.textContent('.nested-worker-module')).toMatch('sub-worker')
59-
expect(await page.textContent('.nested-worker-constructor')).toMatch(
60-
'"type":"constructor"'
37+
await untilUpdated(
38+
() => page.textContent('.nested-worker-module'),
39+
'sub-worker',
40+
true
41+
)
42+
await untilUpdated(
43+
() => page.textContent('.nested-worker-constructor'),
44+
'"type":"constructor"',
45+
true
6146
)
6247
})
6348

@@ -87,45 +72,73 @@ describe.runIf(isBuild)('build', () => {
8772
})
8873

8974
test('worker emitted and import.meta.url in nested worker (build)', async () => {
90-
expect(await page.textContent('.nested-worker-module')).toMatch(
91-
'"type":"module"'
75+
await untilUpdated(
76+
() => page.textContent('.nested-worker-module'),
77+
'"type":"module"',
78+
true
9279
)
93-
expect(await page.textContent('.nested-worker-constructor')).toMatch(
94-
'"type":"constructor"'
80+
await untilUpdated(
81+
() => page.textContent('.nested-worker-constructor'),
82+
'"type":"constructor"',
83+
true
9584
)
9685
})
9786
})
9887

9988
test('module worker', async () => {
100-
expect(await page.textContent('.shared-worker-import-meta-url')).toMatch(
101-
'A string'
89+
await untilUpdated(
90+
() => page.textContent('.shared-worker-import-meta-url'),
91+
'A string',
92+
true
10293
)
10394
})
10495

10596
test('classic worker', async () => {
106-
expect(await page.textContent('.classic-worker')).toMatch('A classic')
107-
expect(await page.textContent('.classic-shared-worker')).toMatch('A classic')
97+
await untilUpdated(
98+
() => page.textContent('.classic-worker'),
99+
'A classic',
100+
true
101+
)
102+
await untilUpdated(
103+
() => page.textContent('.classic-shared-worker'),
104+
'A classic',
105+
true
106+
)
108107
})
109108

110109
test('emit chunk', async () => {
111-
expect(await page.textContent('.emit-chunk-worker')).toMatch(
112-
'["A string",{"type":"emit-chunk-sub-worker","data":"A string"},{"type":"module-and-worker:worker","data":"A string"},{"type":"module-and-worker:module","data":"module and worker"},{"type":"emit-chunk-sub-worker","data":{"module":"module and worker","msg1":"module1","msg2":"module2","msg3":"module3"}}]'
110+
await untilUpdated(
111+
() => page.textContent('.emit-chunk-worker'),
112+
'["A string",{"type":"emit-chunk-sub-worker","data":"A string"},{"type":"module-and-worker:worker","data":"A string"},{"type":"module-and-worker:module","data":"module and worker"},{"type":"emit-chunk-sub-worker","data":{"module":"module and worker","msg1":"module1","msg2":"module2","msg3":"module3"}}]',
113+
true
113114
)
114-
expect(await page.textContent('.emit-chunk-dynamic-import-worker')).toMatch(
115-
'"A string/es/"'
115+
await untilUpdated(
116+
() => page.textContent('.emit-chunk-dynamic-import-worker'),
117+
'"A string/es/"',
118+
true
116119
)
117120
})
118121

119122
test('url query worker', async () => {
120-
expect(await page.textContent('.simple-worker-url')).toMatch(
121-
'Hello from simple worker!'
123+
await untilUpdated(
124+
() => page.textContent('.simple-worker-url'),
125+
'Hello from simple worker!',
126+
true
122127
)
123128
})
124129

125130
test('import.meta.glob in worker', async () => {
126-
expect(await page.textContent('.importMetaGlob-worker')).toMatch('["')
131+
await untilUpdated(
132+
() => page.textContent('.importMetaGlob-worker'),
133+
'["',
134+
true
135+
)
127136
})
128137

129138
test('import.meta.glob with eager in worker', async () => {
130-
expect(await page.textContent('.importMetaGlobEager-worker')).toMatch('["')
139+
await untilUpdated(
140+
() => page.textContent('.importMetaGlobEager-worker'),
141+
'["',
142+
true
143+
)
131144
})

0 commit comments

Comments
 (0)