-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathgetDependencySizeTree.test.ts
More file actions
255 lines (228 loc) · 9 KB
/
Copy pathgetDependencySizeTree.test.ts
File metadata and controls
255 lines (228 loc) · 9 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import { describe, it, expect } from 'vitest'
import { minify } from 'oxc-minify'
import getDependencySizeTree from '../../src/getDependencySizeTree'
import { MinifyError } from '../../src/errors/CustomError'
type RspackStatsCompilation = Parameters<typeof getDependencySizeTree>[1]
// Oxc requires a filename and uses its extension to select the parse mode.
const MINIFY_TEST_FILENAME = 'dependency.js'
type FakeModule = {
identifier?: string
name?: string
moduleType?: string
source?: string | Buffer
modules?: FakeModule[]
}
type FakeStats = {
modules?: FakeModule[]
}
function createStats(modules: FakeModule[]): RspackStatsCompilation {
return { modules } as unknown as RspackStatsCompilation
}
async function minifiedUtf8Size(source: string | Buffer) {
const text = typeof source === 'string' ? source : source.toString('utf8')
const minifiedResult = await minify(MINIFY_TEST_FILENAME, text, {
compress: true,
mangle: true,
module: true,
})
return Buffer.byteLength(minifiedResult.code || '', 'utf8')
}
describe('getDependencySizeTree - accuracy', () => {
it('returns only top-level dependencies and normalizes package names', async () => {
const base = '/project'
// Simulate a module from a -> b (nested node_modules)
// The old webpack implementation only returned first-level children of the tree
// So even though 'b' is nested under 'a', only 'a' will be in the result
const stats: FakeStats = {
modules: [
{
identifier: `${base}/node_modules/a/index.js`,
name: 'a/index.js',
moduleType: 'javascript/esm',
source: 'export const A = 1',
},
{
identifier: `${base}/node_modules/a/node_modules/b/index.js`,
name: 'b/index.js',
moduleType: 'javascript/esm',
source: 'export const B = 2',
},
],
}
const result = await getDependencySizeTree(
'fixture-pkg',
createStats(stats.modules ?? []),
)
const names = result.map(r => r.name)
// Should only include 'a' (top-level), not 'b' (nested under 'a')
expect(names).toEqual(['a'])
expect(names.some(n => n.endsWith('/'))).toBe(false)
})
it('computes UTF-8 byte length correctly for astral characters (no double count)', async () => {
const base = '/project'
const source = 'var x = "😀";'
const stats: FakeStats = {
modules: [
{
identifier: `${base}/node_modules/emoji-pkg/index.js`,
name: 'emoji-pkg/index.js',
moduleType: 'javascript/esm',
source,
},
],
}
// Compute the expected size using the same minifier, but measure size via Buffer.byteLength
const minified = await minify(MINIFY_TEST_FILENAME, source, {
compress: true,
mangle: true,
module: true,
})
const expectedSize = Buffer.byteLength(minified.code || '', 'utf8')
const result = await getDependencySizeTree(
'fixture-pkg',
createStats(stats.modules ?? []),
)
const emojiEntry = result.find(r => r.name === 'emoji-pkg')
expect(emojiEntry).toBeDefined()
// BUG we expect to expose: current implementation over-counts surrogate pairs
expect(emojiEntry!.approximateSize).toBe(expectedSize)
})
it('throws MinifyError when minification fails on invalid code', async () => {
const base = '/project'
// Create source that is technically parseable by rspack but might fail minification
// Using null bytes or other special characters that could cause issues
const source = Buffer.from([0x00, 0x01, 0x02, 0x03]).toString('utf8')
const stats: FakeStats = {
modules: [
{
identifier: `${base}/node_modules/bad-pkg/index.js`,
name: 'bad-pkg/index.js',
moduleType: 'javascript/esm',
source,
},
],
}
try {
await getDependencySizeTree(
'fixture-pkg',
createStats(stats.modules ?? []),
)
// If it doesn't throw, that's okay - the minifier might handle it
// This test documents the expected behavior when minification fails
} catch (error) {
// If it does throw, it should be a MinifyError
if (error instanceof MinifyError) {
expect(error.name).toBe('MinifyError')
expect(error.originalError).toBeDefined()
}
// Allow the test to pass whether it throws or not, since minifiers can be robust
}
})
it('does not pass non-JavaScript modules to the JavaScript minifier', async () => {
const base = '/project'
const javascriptSource = 'export const value = 42'
const stats = createStats([
{
identifier: `${base}/node_modules/wasm-package/index.js`,
moduleType: 'javascript/esm',
source: javascriptSource,
},
{
identifier: `webassembly/async|${base}/node_modules/wasm-package/module.wasm|evaluation`,
moduleType: 'webassembly/async',
source: Buffer.from([0x00, 0x01, 0x02, 0x03]),
},
])
const result = await getDependencySizeTree('fixture-pkg', stats)
expect(result).toEqual([
{
name: 'wasm-package',
approximateSize: await minifiedUtf8Size(javascriptSource),
},
])
})
it('aggregates nested pnpm, scoped, buffer, and virtual deps into accurate package sizes', async () => {
const base = '/project'
const levelOneSource = 'export const levelOne = () => "one"'
const levelTwoMain = 'export const levelTwo = () => "two"'
const levelTwoUtil = 'export const helper = () => 42'
const levelThreeSource = 'export const levelThree = () => "three"'
const virtualSource = 'export const virtual = true'
const bufferSource = Buffer.from('export const bufferValue = 7', 'utf8')
const stats: FakeStats = {
modules: [
{
identifier: `${base}/node_modules/.pnpm/level-one@1.0.0/node_modules/level-one/index.js`,
moduleType: 'javascript/esm',
source: levelOneSource,
},
{
identifier: `javascript/esm|${base}/node_modules/.pnpm/wrapper@0.0.0/node_modules/wrapper/index.js`,
moduleType: 'javascript/esm',
modules: [
{
identifier: `${base}/node_modules/.pnpm/level-one@1.0.0/node_modules/level-one/node_modules/.pnpm/@scope+level-two@2.0.0/node_modules/@scope/level-two/index.js`,
moduleType: 'javascript/esm',
source: levelTwoMain,
},
{
identifier: `${base}/node_modules/.pnpm/level-one@1.0.0/node_modules/level-one/node_modules/.pnpm/@scope+level-two@2.0.0/node_modules/@scope/level-two/util.js`,
moduleType: 'javascript/esm',
source: levelTwoUtil,
},
{
identifier: `${base}/node_modules/.pnpm/level-one@1.0.0/node_modules/level-one/node_modules/.pnpm/@scope+level-two@2.0.0/node_modules/@scope/level-two/node_modules/.pnpm/level-three@3.0.0/node_modules/level-three/index.js`,
moduleType: 'javascript/esm',
source: levelThreeSource,
},
{
identifier: `${base}/node_modules/.pnpm/level-one@1.0.0/node_modules/level-one/node_modules/.pnpm/buffer-dep@4.0.0/node_modules/buffer-dep/index.js`,
moduleType: 'javascript/esm',
source: bufferSource,
},
{
identifier: `javascript/esm|${base}/node_modules/virtual-dep/index.js`,
moduleType: 'javascript/esm',
source: virtualSource,
},
{
identifier: `${base}/node_modules/.pnpm/level-one@1.0.0/node_modules/level-one/node_modules/without-source.js`,
moduleType: 'javascript/esm',
},
{
moduleType: 'javascript/esm',
source: 'export const ignored = 0',
},
],
},
{
identifier: `${base}/node_modules/external-runtime.js`,
name: 'external "react"',
moduleType: 'runtime',
source: 'ignored',
},
],
}
const result = await getDependencySizeTree(
'fixture-pkg',
createStats(stats.modules ?? []),
)
const resultMap = new Map(
result.map(dep => [dep.name, dep.approximateSize]),
)
const expectedLevelOne = await minifiedUtf8Size(levelOneSource)
const expectedLevelTwo =
(await minifiedUtf8Size(levelTwoMain)) +
(await minifiedUtf8Size(levelTwoUtil))
const expectedLevelThree = await minifiedUtf8Size(levelThreeSource)
const expectedBuffer = await minifiedUtf8Size(bufferSource)
const expectedVirtual = await minifiedUtf8Size(virtualSource)
expect(resultMap.get('level-one')).toBe(expectedLevelOne)
expect(resultMap.get('@scope/level-two')).toBe(expectedLevelTwo)
expect(resultMap.get('level-three')).toBe(expectedLevelThree)
expect(resultMap.get('buffer-dep')).toBe(expectedBuffer)
expect(resultMap.get('virtual-dep')).toBe(expectedVirtual)
// Only the actual dependencies referenced via node_modules should appear
expect(resultMap.size).toBe(5)
})
})