Skip to content

Commit b80df42

Browse files
authored
Support packages that emit auxiliary assets (#95)
* fix: support auxiliary build assets * Use Rspack naming for WebAssembly assets
1 parent 466c7f9 commit b80df42

5 files changed

Lines changed: 59 additions & 0 deletions

File tree

.changeset/calm-assets-build.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'package-build-stats': patch
3+
---
4+
5+
Support packages that emit auxiliary assets such as WebAssembly files.

src/config/makeRspackConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ export default function makeRspackConfig({
198198
},
199199
output: {
200200
filename: '[name].bundle.js',
201+
webassemblyModuleFilename: '[hash].bundle.wasm',
201202
path: outputPath,
202203
},
203204
externals: ({ request }, callback) =>

src/getDependencySizeTree.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,14 @@ async function bundleSizeTree(
186186
const makeModule = (
187187
mod: RspackModule,
188188
): { path: string; source: string } | null => {
189+
if (
190+
mod.moduleType &&
191+
!mod.moduleType.startsWith('javascript') &&
192+
!mod.moduleType.startsWith('json')
193+
) {
194+
return null
195+
}
196+
189197
const identifier = mod.identifier || ''
190198
const resolvedPath = modulePath(identifier)
191199
const source = normaliseModuleSource(mod)

tests/fast/getDependencySizeTree.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,32 @@ describe('getDependencySizeTree - accuracy', () => {
137137
}
138138
})
139139

140+
it('does not pass non-JavaScript modules to the JavaScript minifier', async () => {
141+
const base = '/project'
142+
const javascriptSource = 'export const value = 42'
143+
const stats = createStats([
144+
{
145+
identifier: `${base}/node_modules/wasm-package/index.js`,
146+
moduleType: 'javascript/esm',
147+
source: javascriptSource,
148+
},
149+
{
150+
identifier: `webassembly/async|${base}/node_modules/wasm-package/module.wasm|evaluation`,
151+
moduleType: 'webassembly/async',
152+
source: Buffer.from([0x00, 0x01, 0x02, 0x03]),
153+
},
154+
])
155+
156+
const result = await getDependencySizeTree('fixture-pkg', stats)
157+
158+
expect(result).toEqual([
159+
{
160+
name: 'wasm-package',
161+
approximateSize: await minifiedUtf8Size(javascriptSource),
162+
},
163+
])
164+
})
165+
140166
it('aggregates nested pnpm, scoped, buffer, and virtual deps into accurate package sizes', async () => {
141167
const base = '/project'
142168
const levelOneSource = 'export const levelOne = () => "one"'
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { describe, expect, test } from 'vitest'
2+
3+
import makeRspackConfig from '../../src/config/makeRspackConfig.js'
4+
5+
describe('makeRspackConfig', () => {
6+
test('uses the bundle filename convention for WebAssembly modules', () => {
7+
const config = makeRspackConfig({
8+
packageName: 'fixture',
9+
entry: '/tmp/index.js',
10+
externals: {
11+
externalPackages: [],
12+
externalBuiltIns: [],
13+
},
14+
outputPath: '/tmp/output',
15+
})
16+
17+
expect(config.output?.webassemblyModuleFilename).toBe('[hash].bundle.wasm')
18+
})
19+
})

0 commit comments

Comments
 (0)