-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
modulePreloadPolyfill.spec.ts
58 lines (54 loc) · 1.51 KB
/
modulePreloadPolyfill.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
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
import type { ModuleFormat, RollupOutput } from 'rollup'
import { build } from '../../../build'
import { modulePreloadPolyfillId } from '../../../plugins/modulePreloadPolyfill'
const __dirname = resolve(fileURLToPath(import.meta.url), '..')
const buildProject = ({ format = 'es' as ModuleFormat } = {}) =>
build({
root: resolve(__dirname, 'packages/build-project'),
logLevel: 'silent',
build: {
write: false,
rollupOptions: {
input: 'main.js',
output: {
format,
},
treeshake: {
moduleSideEffects: false,
},
},
minify: false,
},
plugins: [
{
name: 'test',
resolveId(id) {
if (id === 'main.js') {
return `\0${id}`
}
},
load(id) {
if (id === '\0main.js') {
return `import '${modulePreloadPolyfillId}'`
}
},
},
],
}) as Promise<RollupOutput>
describe('load', () => {
it('loads modulepreload polyfill', async ({ expect }) => {
const { output } = await buildProject()
expect(output).toHaveLength(1)
expect(output[0].code).toMatchSnapshot()
})
it("doesn't load modulepreload polyfill when format is cjs", async ({
expect,
}) => {
const { output } = await buildProject({ format: 'cjs' })
expect(output).toHaveLength(1)
expect(output[0].code).toMatchSnapshot()
})
})