@@ -4,6 +4,7 @@ import * as zlib from 'node:zlib';
44import { promisify } from 'node:util' ;
55import { build , transformWithEsbuild } from 'vite' ;
66import { visualizer } from 'rollup-plugin-visualizer' ;
7+ import { escapeFilename } from './strings.js' ;
78
89const gzipAsync = promisify ( zlib . gzip ) ;
910
@@ -48,7 +49,7 @@ function createReplacePlugin(replacements) {
4849 * @param {ObjectEntry } entry - Entry point (string or object)
4950 * @param {CommandLineArgs } args
5051 * @param {Record<string, string> } [replacements] - String replacements to apply
51- * @returns {Promise<import('vite').InlineConfig> }
52+ * @returns {Promise<{ config: import('vite').InlineConfig, treemapPath: string } > }
5253 */
5354async function createViteConfig ( entry , args , replacements = { } ) {
5455 const entryName = entry . id ;
@@ -78,13 +79,15 @@ async function createViteConfig(entry, args, replacements = {}) {
7879 const externalsArray = entry . externals || [ 'react' , 'react-dom' ] ;
7980
8081 // Ensure build directory exists
81- const outDir = path . join ( rootDir , 'build' , entryName ) ;
82+ const outDir = path . join ( rootDir , 'build' , escapeFilename ( entryName ) ) ;
8283 await fs . mkdir ( outDir , { recursive : true } ) ;
8384
85+ const treemapPath = path . join ( outDir , 'treemap.html' ) ;
86+
8487 /**
8588 * @type {import('vite').InlineConfig }
8689 */
87- const configuration = {
90+ const config = {
8891 configFile : false ,
8992 root : rootDir ,
9093
@@ -112,7 +115,7 @@ async function createViteConfig(entry, args, replacements = {}) {
112115 ? [
113116 // File sizes are not accurate, use it only for relative comparison
114117 visualizer ( {
115- filename : ` ${ outDir } .html` ,
118+ filename : treemapPath ,
116119 title : `Bundle Size Analysis: ${ entryName } ` ,
117120 projectRoot : rootDir ,
118121 open : false ,
@@ -171,7 +174,7 @@ async function createViteConfig(entry, args, replacements = {}) {
171174 ] ,
172175 } ;
173176
174- return configuration ;
177+ return { config , treemapPath } ;
175178}
176179
177180/**
@@ -275,19 +278,21 @@ async function processBundleSizes(output, entryName) {
275278 * @param {ObjectEntry } entry - The entry configuration
276279 * @param {CommandLineArgs } args - Command line arguments
277280 * @param {Record<string, string> } [replacements] - String replacements to apply
278- * @returns {Promise<Map<string, SizeSnapshotEntry>> }
281+ * @returns {Promise<{ sizes: Map<string, SizeSnapshotEntry>, treemapPath: string } > }
279282 */
280283export async function getBundleSizes ( entry , args , replacements ) {
281284 // Create vite configuration
282- const configuration = await createViteConfig ( entry , args , replacements ) ;
285+ const { config , treemapPath } = await createViteConfig ( entry , args , replacements ) ;
283286
284287 // Run vite build
285- const { output } = /** @type {import('vite').Rollup.RollupOutput } */ ( await build ( configuration ) ) ;
288+ const { output } = /** @type {import('vite').Rollup.RollupOutput } */ ( await build ( config ) ) ;
286289 const manifestChunk = output . find ( ( chunk ) => chunk . fileName === '.vite/manifest.json' ) ;
287290 if ( ! manifestChunk ) {
288291 throw new Error ( `Manifest file not found in output for entry: ${ entry . id } ` ) ;
289292 }
290293
291294 // Process the output to get bundle sizes
292- return processBundleSizes ( output , entry . id ) ;
295+ const sizes = await processBundleSizes ( output , entry . id ) ;
296+
297+ return { sizes, treemapPath } ;
293298}
0 commit comments