/
iife-worker.spec.ts
181 lines (162 loc) · 5.09 KB
/
iife-worker.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import fs from 'node:fs'
import path from 'node:path'
import { describe, expect, test } from 'vitest'
import {
isBuild,
isServe,
page,
readManifest,
testDir,
untilUpdated,
viteTestUrl,
} from '~utils'
test('normal', async () => {
await untilUpdated(() => page.textContent('.pong'), 'pong')
await untilUpdated(() => page.textContent('.mode'), process.env.NODE_ENV)
await untilUpdated(
() => page.textContent('.bundle-with-plugin'),
'worker bundle with plugin success!',
)
await untilUpdated(
() => page.textContent('.asset-url'),
isBuild ? '/iife/assets/worker_asset-vite.svg' : '/iife/vite.svg',
true,
)
})
test('named', async () => {
await untilUpdated(() => page.textContent('.pong-named'), 'namedWorker', true)
})
test('TS output', async () => {
await untilUpdated(() => page.textContent('.pong-ts-output'), 'pong')
})
test('inlined', async () => {
await untilUpdated(() => page.textContent('.pong-inline'), 'pong')
})
test('named inlined', async () => {
await untilUpdated(
() => page.textContent('.pong-inline-named'),
'namedInlineWorker',
true,
)
})
test('shared worker', async () => {
await untilUpdated(() => page.textContent('.tick-count'), 'pong')
})
test('named shared worker', async () => {
await untilUpdated(() => page.textContent('.tick-count-named'), 'pong', true)
})
test('inline shared worker', async () => {
await untilUpdated(() => page.textContent('.pong-shared-inline'), 'pong')
})
test('worker emitted and import.meta.url in nested worker (serve)', async () => {
await untilUpdated(() => page.textContent('.nested-worker'), '/worker-nested')
await untilUpdated(
() => page.textContent('.nested-worker-module'),
'/sub-worker',
)
await untilUpdated(
() => page.textContent('.nested-worker-constructor'),
'"type":"constructor"',
)
})
describe.runIf(isBuild)('build', () => {
// assert correct files
test('inlined code generation', async () => {
const assetsDir = path.resolve(testDir, 'dist/iife/assets')
const files = fs.readdirSync(assetsDir)
expect(files.length).toBe(20)
const index = files.find((f) => f.includes('main-module'))
const content = fs.readFileSync(path.resolve(assetsDir, index), 'utf-8')
const worker = files.find((f) => f.includes('worker_entry-my-worker'))
const workerContent = fs.readFileSync(
path.resolve(assetsDir, worker),
'utf-8',
)
// worker should have all imports resolved and no exports
expect(workerContent).not.toMatch(`import`)
expect(workerContent).not.toMatch(`export`)
// chunk
expect(content).toMatch(`new Worker("/iife/assets`)
expect(content).toMatch(`new SharedWorker("/iife/assets`)
// inlined
expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`)
expect(content).toMatch(`window.Blob`)
})
test('worker emitted and import.meta.url in nested worker (build)', async () => {
await untilUpdated(
() => page.textContent('.nested-worker-module'),
'"type":"module"',
)
await untilUpdated(
() => page.textContent('.nested-worker-constructor'),
'"type":"constructor"',
)
})
test('should not emit worker manifest', async () => {
const manifest = readManifest('iife')
expect(manifest['index.html']).toBeDefined()
})
})
test('module worker', async () => {
await untilUpdated(
async () => page.textContent('.worker-import-meta-url'),
/A\sstring.*\/iife\/.+url-worker\.js.+url-worker\.js/,
true,
)
await untilUpdated(
() => page.textContent('.worker-import-meta-url-resolve'),
/A\sstring.*\/iife\/.+url-worker\.js.+url-worker\.js/,
true,
)
await untilUpdated(
() => page.textContent('.worker-import-meta-url-without-extension'),
/A\sstring.*\/iife\/.+url-worker\.js.+url-worker\.js/,
true,
)
await untilUpdated(
() => page.textContent('.shared-worker-import-meta-url'),
'A string',
true,
)
})
test('classic worker', async () => {
await untilUpdated(() => page.textContent('.classic-worker'), 'A classic')
await untilUpdated(
() => page.textContent('.classic-worker-import'),
'[success] classic-esm',
)
await untilUpdated(
() => page.textContent('.classic-shared-worker'),
'A classic',
)
})
test('url query worker', async () => {
await untilUpdated(
() => page.textContent('.simple-worker-url'),
'Hello from simple worker!',
)
})
test('import.meta.glob eager in worker', async () => {
await untilUpdated(
() => page.textContent('.importMetaGlobEager-worker'),
'["',
)
})
test.runIf(isServe)('sourcemap boundary', async () => {
const response = page.waitForResponse(/my-worker.ts\?type=module&worker_file/)
await page.goto(viteTestUrl)
const content = await (await response).text()
const { mappings } = decodeSourceMapUrl(content)
expect(mappings.startsWith(';')).toBeTruthy()
expect(mappings.endsWith(';')).toBeFalsy()
})
function decodeSourceMapUrl(content: string) {
return JSON.parse(
Buffer.from(
content.match(
/\/\/[#@]\ssourceMappingURL=\s*data:application\/json;base64,(\S+)/,
)?.[1],
'base64',
).toString(),
)
}