-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
Copy pathlib.spec.ts
82 lines (73 loc) · 2.52 KB
/
lib.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
import { describe, expect, test } from 'vitest'
import {
isBuild,
isServe,
page,
readFile,
serverLogs,
untilUpdated,
} from '~utils'
describe.runIf(isBuild)('build', () => {
test('es', async () => {
expect(await page.textContent('.es')).toBe('It works')
})
test('umd', async () => {
expect(await page.textContent('.umd')).toBe('It works')
const code = readFile('dist/my-lib-custom-filename.umd.cjs')
const noMinifyCode = readFile(
'dist/nominify/my-lib-custom-filename.umd.cjs',
)
// esbuild helpers are injected inside of the UMD wrapper
expect(code).toMatch(/^\(function\(/)
expect(noMinifyCode).toMatch(
/^\(function\(global.+?"use strict";var.+?function\smyLib\(/s,
)
})
test('iife', async () => {
expect(await page.textContent('.iife')).toBe('It works')
const code = readFile('dist/my-lib-custom-filename.iife.js')
const noMinifyCode = readFile(
'dist/nominify/my-lib-custom-filename.iife.js',
)
// esbuild helpers are injected inside of the IIFE wrapper
expect(code).toMatch(/^var MyLib=function\(\)\{\s*"use strict";/)
expect(noMinifyCode).toMatch(
/^var MyLib\s*=\s*function\(\)\s*\{\s*"use strict";/,
)
})
test('restrisct-helpers-injection', async () => {
const code = readFile(
'dist/helpers-injection/my-lib-custom-filename.iife.js',
)
expect(code).toMatch(
`'"use strict"; return (' + expressionSyntax + ").constructor;"`,
)
})
test('Library mode does not include `preload`', async () => {
await untilUpdated(
() => page.textContent('.dynamic-import-message'),
'hello vite',
)
const code = readFile('dist/lib/dynamic-import-message.es.mjs')
expect(code).not.toMatch('__vitePreload')
// Test that library chunks are hashed
expect(code).toMatch(/await import\("\.\/message-[a-z\d]{8}.js"\)/)
})
test('@import hoist', async () => {
serverLogs.forEach((log) => {
// no warning from esbuild css minifier
expect(log).not.toMatch('All "@import" rules must come first')
})
})
test('preserve process.env', () => {
const es = readFile('dist/my-lib-custom-filename.js')
const iife = readFile('dist/my-lib-custom-filename.iife.js')
const umd = readFile('dist/my-lib-custom-filename.umd.cjs')
expect(es).toMatch('process.env.NODE_ENV')
expect(iife).toMatch('process.env.NODE_ENV')
expect(umd).toMatch('process.env.NODE_ENV')
})
})
test.runIf(isServe)('dev', async () => {
expect(await page.textContent('.demo')).toBe('It works')
})