Skip to content

Commit 26ecd5a

Browse files
authoredJun 13, 2022
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
})
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,37 @@
11
import fs from 'fs'
22
import path from 'path'
3-
import type { Page } from 'playwright-chromium'
43
import { test } from 'vitest'
54
import { isBuild, page, testDir, untilUpdated } from '~utils'
65

76
test('normal', async () => {
8-
await page.click('.ping')
97
await untilUpdated(() => page.textContent('.pong'), 'pong')
10-
await untilUpdated(
11-
() => page.textContent('.mode'),
12-
process.env.NODE_ENV // match workerImport.js
13-
)
8+
await untilUpdated(() => page.textContent('.mode'), process.env.NODE_ENV)
149
await untilUpdated(
1510
() => page.textContent('.bundle-with-plugin'),
1611
'worker bundle with plugin success!'
1712
)
1813
})
1914

2015
test('TS output', async () => {
21-
await page.click('.ping-ts-output')
2216
await untilUpdated(() => page.textContent('.pong-ts-output'), 'pong')
2317
})
2418

2519
test('inlined', async () => {
26-
await page.click('.ping-inline')
2720
await untilUpdated(() => page.textContent('.pong-inline'), 'pong')
2821
})
2922

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

5527
test('worker emitted and import.meta.url in nested worker (serve)', async () => {
56-
expect(await page.textContent('.nested-worker')).toMatch('/worker-nested')
57-
expect(await page.textContent('.nested-worker-module')).toMatch('/sub-worker')
58-
expect(await page.textContent('.nested-worker-constructor')).toMatch(
28+
await untilUpdated(() => page.textContent('.nested-worker'), '/worker-nested')
29+
await untilUpdated(
30+
() => page.textContent('.nested-worker-module'),
31+
'/sub-worker'
32+
)
33+
await untilUpdated(
34+
() => page.textContent('.nested-worker-constructor'),
5935
'"type":"constructor"'
6036
)
6137
})
@@ -86,32 +62,42 @@ describe.runIf(isBuild)('build', () => {
8662
})
8763

8864
test('worker emitted and import.meta.url in nested worker (build)', async () => {
89-
expect(await page.textContent('.nested-worker-module')).toMatch(
65+
await untilUpdated(
66+
() => page.textContent('.nested-worker-module'),
9067
'"type":"module"'
9168
)
92-
expect(await page.textContent('.nested-worker-constructor')).toMatch(
69+
await untilUpdated(
70+
() => page.textContent('.nested-worker-constructor'),
9371
'"type":"constructor"'
9472
)
9573
})
9674
})
9775

9876
test('module worker', async () => {
99-
expect(await page.textContent('.shared-worker-import-meta-url')).toMatch(
77+
await untilUpdated(
78+
() => page.textContent('.shared-worker-import-meta-url'),
10079
'A string'
10180
)
10281
})
10382

10483
test('classic worker', async () => {
105-
expect(await page.textContent('.classic-worker')).toMatch('A classic')
106-
expect(await page.textContent('.classic-shared-worker')).toMatch('A classic')
84+
await untilUpdated(() => page.textContent('.classic-worker'), 'A classic')
85+
await untilUpdated(
86+
() => page.textContent('.classic-shared-worker'),
87+
'A classic'
88+
)
10789
})
10890

10991
test('url query worker', async () => {
110-
expect(await page.textContent('.simple-worker-url')).toMatch(
92+
await untilUpdated(
93+
() => page.textContent('.simple-worker-url'),
11194
'Hello from simple worker!'
11295
)
11396
})
11497

11598
test('import.meta.glob eager in worker', async () => {
116-
expect(await page.textContent('.importMetaGlobEager-worker')).toMatch('["')
99+
await untilUpdated(
100+
() => page.textContent('.importMetaGlobEager-worker'),
101+
'["'
102+
)
117103
})
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,62 @@
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

6449
describe.runIf(isBuild)('build', () => {
6550
// assert correct files
66-
test('inlined code generation', async () => {
67-
const chunksDir = path.resolve(testDir, 'dist/chunks')
51+
test('inlined code generation', () => {
52+
const chunksDir = path.resolve(testDir, 'dist/relative-base/chunks')
6853
const files = fs.readdirSync(chunksDir)
6954
const index = files.find((f) => f.includes('main-module'))
7055
const content = fs.readFileSync(path.resolve(chunksDir, index), 'utf-8')
71-
const workerEntriesDir = path.resolve(testDir, 'dist/worker-entries')
56+
const workerEntriesDir = path.resolve(
57+
testDir,
58+
'dist/relative-base/worker-entries'
59+
)
7260
const workerFiles = fs.readdirSync(workerEntriesDir)
7361
const worker = workerFiles.find((f) => f.includes('worker_entry.my-worker'))
7462
const workerContent = fs.readFileSync(
@@ -88,39 +76,65 @@ describe.runIf(isBuild)('build', () => {
8876
})
8977

9078
test('worker emitted and import.meta.url in nested worker (build)', async () => {
91-
expect(await page.textContent('.nested-worker-module')).toMatch(
92-
'"type":"module"'
79+
await untilUpdated(
80+
() => page.textContent('.nested-worker-module'),
81+
'"type":"module"',
82+
true
9383
)
94-
expect(await page.textContent('.nested-worker-constructor')).toMatch(
95-
'"type":"constructor"'
84+
await untilUpdated(
85+
() => page.textContent('.nested-worker-constructor'),
86+
'"type":"constructor"',
87+
true
9688
)
9789
})
9890
})
9991

10092
test('module worker', async () => {
101-
expect(await page.textContent('.shared-worker-import-meta-url')).toMatch(
102-
'A string'
93+
await untilUpdated(
94+
() => page.textContent('.shared-worker-import-meta-url'),
95+
'A string',
96+
true
10397
)
10498
})
10599

106100
test.runIf(isBuild)('classic worker', async () => {
107-
expect(await page.textContent('.classic-worker')).toMatch('A classic')
108-
expect(await page.textContent('.classic-shared-worker')).toMatch('A classic')
101+
await untilUpdated(
102+
() => page.textContent('.classic-worker'),
103+
'A classic',
104+
true
105+
)
106+
await untilUpdated(
107+
() => page.textContent('.classic-shared-worker'),
108+
'A classic',
109+
true
110+
)
109111
})
110112

111113
test.runIf(isBuild)('emit chunk', async () => {
112-
expect(await page.textContent('.emit-chunk-worker')).toMatch(
113-
'["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"}}]'
114+
await untilUpdated(
115+
() => page.textContent('.emit-chunk-worker'),
116+
'["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"}}]',
117+
true
114118
)
115-
expect(await page.textContent('.emit-chunk-dynamic-import-worker')).toMatch(
116-
'"A string./"'
119+
await untilUpdated(
120+
() => page.textContent('.emit-chunk-dynamic-import-worker'),
121+
'"A string./"',
122+
true
117123
)
118124
})
119125

120126
test('import.meta.glob in worker', async () => {
121-
expect(await page.textContent('.importMetaGlob-worker')).toMatch('["')
127+
await untilUpdated(
128+
() => page.textContent('.importMetaGlob-worker'),
129+
'["',
130+
true
131+
)
122132
})
123133

124134
test('import.meta.glob with eager in worker', async () => {
125-
expect(await page.textContent('.importMetaGlobEager-worker')).toMatch('["')
135+
await untilUpdated(
136+
() => page.textContent('.importMetaGlobEager-worker'),
137+
'["',
138+
true
139+
)
126140
})

‎playground/worker/__tests__/sourcemap/__snapshots__/sourcemap-worker.spec.ts.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`serve:worker-sourcemap > nested worker 1`] = `
44
{
5-
"mappings": "AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,uCAAsB,CAAC;AAClD;AACA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACzB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC1B,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACnC,CAAC,CAAC,CAAC;AACH,CAAC;AACD;AACA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAChB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;",
5+
"mappings": "AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,uCAAsB,CAAC;AAClD;AACA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACzB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACjC,CAAC;AACD;AACA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC/B;AACA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAChB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;",
66
"sources": [
77
"/root/possible-ts-output-worker.mjs?worker_file",
88
],

‎playground/worker/index.html

+27-17
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,40 @@
1+
<h2 class="format-iife">format iife:</h2>
2+
<div>Expected values: <span class="mode-true"></span></div>
3+
14
<p>worker template error match:</p>
25
<code>
36
const worker = new Worker(new URL('./worker.js', import.meta.url))
47
</code>
58

6-
<h2 class="format-iife">format iife:</h2>
7-
<div>Expected values: <span class="mode-true"></span></div>
8-
<button class="ping">Ping</button>
9+
<p>
10+
import myWorker from '../my-worker?worker'
11+
<span class="classname">.pong</span>
12+
<span class="classname">.mode</span>
13+
<span class="classname">.bundle-with-plugin</span>
14+
</p>
915
<div>
10-
Response from worker: <span class="pong"></span><span class="mode"></span>
16+
<div>Response from worker: <span class="pong"></span></div>
17+
<div>mode: <span class="mode"></span></div>
18+
<div>bundle-with-plugin: <span class="bundle-with-plugin"></span></div>
1119
</div>
12-
<div>bundle-with-plugin: <span class="bundle-with-plugin"></span></div>
1320

14-
<button class="ping-inline">Ping Inline Worker</button>
15-
<div>Response from inline worker: <span class="pong-inline"></span></div>
21+
<p>
22+
import InlineWorker from '../my-worker?worker&inline'
23+
<span class="classname">.pong-inline</span>
24+
</p>
25+
<code class="pong-inline"></code>
1626

17-
<button class="ping-ts-output">Ping Possible Compiled TS Worker</button>
18-
<div>
19-
Response from worker imported from code that might be compiled TS:
20-
<span class="pong-ts-output"></span>
21-
</div>
27+
<p>
28+
import TSOutputWorker from '../possible-ts-output-worker?worker'
29+
<span class="classname">.pong-ts-output</span>
30+
</p>
31+
<code class="pong-ts-output"></code>
2232

23-
<button class="tick-shared">Tick Shared Worker</button>
24-
<div>
25-
Tick from shared worker, it syncs between pages:
26-
<span class="tick-count">0</span>
27-
</div>
33+
<p>
34+
import mySharedWorker from '../my-shared-worker?sharedworker&name=shared'
35+
<span class="classname">.tick-count</span>
36+
</p>
37+
<code class="tick-count"></code>
2838

2939
<p>
3040
new Worker(new URL('./url-worker.js', import.meta.url), { type: 'module' })

‎playground/worker/my-shared-worker.ts

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
1-
let count = 0
21
const ports = new Set()
32

43
// @ts-expect-error
5-
onconnect = (event) => {
4+
self.onconnect = (event) => {
65
const port = event.ports[0]
76
ports.add(port)
8-
port.postMessage(count)
9-
port.onmessage = (message) => {
10-
if (message.data === 'tick') {
11-
count++
12-
ports.forEach((p: any) => {
13-
p.postMessage(count)
14-
})
15-
}
7+
port.postMessage('pong')
8+
port.onmessage = () => {
9+
ports.forEach((p: any) => {
10+
p.postMessage('pong')
11+
})
1612
}
1713
}
1814

1915
// for sourcemap
2016
console.log('my-shared-worker.js')
21-
22-
export {}

‎playground/worker/my-worker.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { mode, msg } from './modules/workerImport'
22
import { bundleWithPlugin } from './modules/test-plugin'
3-
// import { msg as msgFromDep } from 'dep-to-optimize'
3+
import { msg as msgFromDep } from 'dep-to-optimize'
44

55
self.onmessage = (e) => {
66
if (e.data === 'ping') {
7-
self.postMessage({ msg, mode, bundleWithPlugin }) // TODO: fix darwin, and add back: msgFromDep })
7+
self.postMessage({ msg, mode, bundleWithPlugin })
88
}
99
}
10+
self.postMessage({ msg, mode, bundleWithPlugin, msgFromDep })
1011

1112
// for sourcemap
1213
console.log('my-worker.js')
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { msg, mode } from './modules/workerImport'
1+
import { mode, msg } from './modules/workerImport'
22

33
self.onmessage = (e) => {
4-
if (e.data === 'ping') {
5-
self.postMessage({ msg, mode })
6-
}
4+
self.postMessage({ msg, mode })
75
}
86

7+
self.postMessage({ msg, mode })
8+
99
// for sourcemap
1010
console.log('possible-ts-output-worker.mjs')

‎playground/worker/vite.config-es.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ module.exports = vite.defineConfig({
3838
}
3939
}
4040
}
41-
]
41+
],
42+
cacheDir: 'node_modules/.vite/es'
4243
})

‎playground/worker/vite.config-relative-base.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
const path = require('path')
12
const vueJsx = require('@vitejs/plugin-vue-jsx')
23
const vite = require('vite')
3-
const path = require('path')
44

55
module.exports = vite.defineConfig({
66
base: './',
7-
enforce: 'pre',
87
worker: {
98
format: 'es',
109
plugins: [vueJsx()],
@@ -17,7 +16,7 @@ module.exports = vite.defineConfig({
1716
}
1817
},
1918
build: {
20-
outDir: 'dist',
19+
outDir: 'dist/relative-base',
2120
rollupOptions: {
2221
output: {
2322
assetFileNames: 'other-assets/[name]-[hash].[ext]',
@@ -26,6 +25,9 @@ module.exports = vite.defineConfig({
2625
}
2726
}
2827
},
28+
testConfig: {
29+
baseRoute: '/relative-base/'
30+
},
2931
plugins: [
3032
{
3133
name: 'resolve-format-es',
@@ -38,5 +40,6 @@ module.exports = vite.defineConfig({
3840
}
3941
}
4042
}
41-
]
43+
],
44+
cacheDir: 'node_modules/.vite/relative-base'
4245
})

‎playground/worker/vite.config-sourcemap.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,30 @@ module.exports = vite.defineConfig((sourcemap) => {
1212
}/`,
1313
worker: {
1414
format: 'iife',
15-
plugins: [vueJsx()]
15+
plugins: [vueJsx()],
16+
rollupOptions: {
17+
output: {
18+
assetFileNames: 'assets/[name].worker_asset[hash].[ext]',
19+
chunkFileNames: 'assets/[name].worker_chunk[hash].js',
20+
entryFileNames: 'assets/[name].worker_entry[hash].js'
21+
}
22+
}
1623
},
24+
cacheDir: `node_modules/.vite/iife-${
25+
typeof sourcemap === 'boolean' ? 'sourcemap' : 'sourcemap-' + sourcemap
26+
}`,
1727
build: {
1828
outDir: `dist/iife-${
1929
typeof sourcemap === 'boolean' ? 'sourcemap' : 'sourcemap-' + sourcemap
2030
}/`,
21-
sourcemap: sourcemap
31+
sourcemap: sourcemap,
32+
rollupOptions: {
33+
output: {
34+
assetFileNames: 'assets/[name].[hash].[ext]',
35+
chunkFileNames: 'assets/[name].[hash].js',
36+
entryFileNames: 'assets/[name].[hash].js'
37+
}
38+
}
2239
}
2340
}
2441
})

‎playground/worker/vite.config.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,24 @@ module.exports = vite.defineConfig({
55
base: '/iife/',
66
worker: {
77
format: 'iife',
8-
plugins: [vueJsx()]
8+
plugins: [vueJsx()],
9+
rollupOptions: {
10+
output: {
11+
assetFileNames: 'assets/worker_asset.[name].[ext]',
12+
chunkFileNames: 'assets/worker_chunk.[name].js',
13+
entryFileNames: 'assets/worker_entry.[name].js'
14+
}
15+
}
916
},
1017
build: {
11-
outDir: 'dist/iife'
12-
}
18+
outDir: 'dist/iife',
19+
rollupOptions: {
20+
output: {
21+
assetFileNames: 'assets/[name].[ext]',
22+
chunkFileNames: 'assets/[name].js',
23+
entryFileNames: 'assets/[name].js'
24+
}
25+
}
26+
},
27+
cacheDir: 'node_modules/.vite/iife'
1328
})

‎playground/worker/worker/main-module.js

+3-18
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,31 @@ function text(el, text) {
1212
document.querySelector('.mode-true').textContent = mode
1313

1414
const worker = new myWorker()
15+
worker.postMessage('ping')
1516
worker.addEventListener('message', (e) => {
1617
text('.pong', e.data.msg)
1718
text('.mode', e.data.mode)
1819
text('.bundle-with-plugin', e.data.bundleWithPlugin)
1920
})
2021

21-
document.querySelector('.ping').addEventListener('click', () => {
22-
worker.postMessage('ping')
23-
})
24-
2522
const inlineWorker = new InlineWorker()
23+
inlineWorker.postMessage('ping')
2624
inlineWorker.addEventListener('message', (e) => {
2725
text('.pong-inline', e.data.msg)
2826
})
2927

30-
document.querySelector('.ping-inline').addEventListener('click', () => {
31-
console.log('111')
32-
inlineWorker.postMessage('ping')
33-
})
34-
3528
const sharedWorker = new mySharedWorker()
36-
document.querySelector('.tick-shared').addEventListener('click', () => {
37-
sharedWorker.port.postMessage('tick')
38-
})
39-
4029
sharedWorker.port.addEventListener('message', (event) => {
4130
text('.tick-count', event.data)
4231
})
43-
4432
sharedWorker.port.start()
4533

4634
const tsOutputWorker = new TSOutputWorker()
35+
tsOutputWorker.postMessage('ping')
4736
tsOutputWorker.addEventListener('message', (e) => {
4837
text('.pong-ts-output', e.data.msg)
4938
})
5039

51-
document.querySelector('.ping-ts-output').addEventListener('click', () => {
52-
tsOutputWorker.postMessage('ping')
53-
})
54-
5540
const nestedWorker = new NestedWorker()
5641
nestedWorker.addEventListener('message', (ev) => {
5742
if (typeof ev.data === 'string') {

0 commit comments

Comments
 (0)
Please sign in to comment.