/
optimize-deps.spec.ts
163 lines (134 loc) · 5.72 KB
/
optimize-deps.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
import {
browserErrors,
browserLogs,
getColor,
isBuild,
isServe,
page
} from '~utils'
test('default + named imports from cjs dep (react)', async () => {
expect(await page.textContent('.cjs button')).toBe('count is 0')
await page.click('.cjs button')
expect(await page.textContent('.cjs button')).toBe('count is 1')
})
test('named imports from webpacked cjs (phoenix)', async () => {
expect(await page.textContent('.cjs-phoenix')).toBe('ok')
})
test('default import from webpacked cjs (clipboard)', async () => {
expect(await page.textContent('.cjs-clipboard')).toBe('ok')
})
test('dynamic imports from cjs dep (react)', async () => {
expect(await page.textContent('.cjs-dynamic button')).toBe('count is 0')
await page.click('.cjs-dynamic button')
expect(await page.textContent('.cjs-dynamic button')).toBe('count is 1')
})
test('dynamic named imports from webpacked cjs (phoenix)', async () => {
expect(await page.textContent('.cjs-dynamic-phoenix')).toBe('ok')
})
test('dynamic default import from webpacked cjs (clipboard)', async () => {
expect(await page.textContent('.cjs-dynamic-clipboard')).toBe('ok')
})
test('dynamic default import from cjs (cjs-dynamic-dep-cjs-compiled-from-esm)', async () => {
expect(await page.textContent('.cjs-dynamic-dep-cjs-compiled-from-esm')).toBe(
'ok'
)
})
test('dynamic default import from cjs (cjs-dynamic-dep-cjs-compiled-from-cjs)', async () => {
expect(await page.textContent('.cjs-dynamic-dep-cjs-compiled-from-cjs')).toBe(
'ok'
)
})
test('dedupe', async () => {
expect(await page.textContent('.dedupe button')).toBe('count is 0')
await page.click('.dedupe button')
expect(await page.textContent('.dedupe button')).toBe('count is 1')
})
test('cjs browser field (axios)', async () => {
expect(await page.textContent('.cjs-browser-field')).toBe('pong')
})
test('dep from linked dep (lodash-es)', async () => {
expect(await page.textContent('.deps-linked')).toBe('fooBarBaz')
})
test('forced include', async () => {
expect(await page.textContent('.force-include')).toMatch(`[success]`)
})
test('import * from optimized dep', async () => {
expect(await page.textContent('.import-star')).toMatch(`[success]`)
})
test('import from dep with process.env.NODE_ENV', async () => {
expect(await page.textContent('.node-env')).toMatch(isBuild ? 'prod' : 'dev')
})
test('import from dep with .notjs files', async () => {
expect(await page.textContent('.not-js')).toMatch(`[success]`)
})
test('Import from dependency which uses relative path which needs to be resolved by main field', async () => {
expect(await page.textContent('.relative-to-main')).toMatch(`[success]`)
})
test('dep with dynamic import', async () => {
expect(await page.textContent('.dep-with-dynamic-import')).toMatch(
`[success]`
)
})
test('dep with css import', async () => {
expect(await getColor('.dep-linked-include')).toBe('red')
})
test('CJS dep with css import', async () => {
expect(await getColor('.cjs-with-assets')).toBe('blue')
})
test('dep w/ non-js files handled via plugin', async () => {
expect(await page.textContent('.plugin')).toMatch(`[success]`)
})
test('vue + vuex', async () => {
expect(await page.textContent('.vue')).toMatch(`[success]`)
})
// When we use the Rollup CommonJS plugin instead of esbuild prebundling,
// the esbuild plugins won't apply to dependencies
test('esbuild-plugin', async () => {
expect(await page.textContent('.esbuild-plugin')).toMatch(
`Hello from an esbuild plugin`
)
})
test('import from hidden dir', async () => {
expect(await page.textContent('.hidden-dir')).toBe('hello!')
})
test('import optimize-excluded package that imports optimized-included package', async () => {
expect(await page.textContent('.nested-include')).toBe('nested-include')
})
test('import aliased package with colon', async () => {
expect(await page.textContent('.url')).toBe('vitejs.dev')
})
test('variable names are reused in different scripts', async () => {
expect(await page.textContent('.reused-variable-names')).toBe('reused')
})
test('flatten id should generate correctly', async () => {
expect(await page.textContent('.clonedeep-slash')).toBe('clonedeep-slash')
expect(await page.textContent('.clonedeep-dot')).toBe('clonedeep-dot')
})
test.runIf(isServe)('error on builtin modules usage', () => {
expect(browserLogs).toEqual(
expect.arrayContaining([
// from dep-with-builtin-module-esm top-level try-catch
expect.stringContaining(
'dep-with-builtin-module-esm Error: Module "fs" has been externalized for browser compatibility. Cannot access "fs.readFileSync" in client code.'
),
expect.stringContaining(
'dep-with-builtin-module-esm Error: Module "path" has been externalized for browser compatibility. Cannot access "path.join" in client code.'
),
// from dep-with-builtin-module-cjs top-level try-catch
expect.stringContaining(
'dep-with-builtin-module-cjs Error: Module "path" has been externalized for browser compatibility. Cannot access "path.join" in client code.'
)
])
)
expect(browserErrors.map((error) => error.message)).toEqual(
expect.arrayContaining([
// from user source code
'Module "buffer" has been externalized for browser compatibility. Cannot access "buffer.Buffer" in client code.',
'Module "child_process" has been externalized for browser compatibility. Cannot access "child_process.execSync" in client code.',
// from dep-with-builtin-module-esm read()
'Module "fs" has been externalized for browser compatibility. Cannot access "fs.readFileSync" in client code.',
// from dep-with-builtin-module-esm read()
'Module "fs" has been externalized for browser compatibility. Cannot access "fs.readFileSync" in client code.'
])
)
})