diff --git a/.changeset/rsbuild-manifest-module-chunks-once.md b/.changeset/rsbuild-manifest-module-chunks-once.md new file mode 100644 index 0000000000..02febd9d84 --- /dev/null +++ b/.changeset/rsbuild-manifest-module-chunks-once.md @@ -0,0 +1,5 @@ +--- +'@tanstack/start-plugin-core': patch +--- + +Rsbuild: the Start manifest pass that runs after every client compilation (builds and each dev update) reads an imported module's chunks once per compilation and counts the module once per importing chunk, instead of once per import connection. The manifest is unchanged; the pass takes about 40 % less time (on a 333-route app, tens of milliseconds less per HMR update). diff --git a/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts b/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts index d19ad87ed6..a7dae0c379 100644 --- a/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts +++ b/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts @@ -219,6 +219,7 @@ function computeDynamicImports(chunk: RspackCompilationChunk): Array { function computeStaticDependencyChunks( compilation: RspackCompilation, chunk: RspackCompilationChunk, + chunksOfModule: (module: RspackModule) => Array, ): Array { const importedChunks: Array = [] const seen = new Set([chunk]) @@ -258,20 +259,28 @@ function computeStaticDependencyChunks( } availableChunks ??= new Set() + // Every connection to a module adds the same chunks, so a module counts + // once, from its first active connection (an inactive one adds nothing and + // leaves the module to a later active one). + const counted = new Set() for (const module of modules) { for (const dependency of module.dependencies) { const connection = compilation.moduleGraph.getConnection(dependency) const importedModule = connection?.module - if (!connection || !importedModule || localModules.has(importedModule)) { + if ( + !connection || + !importedModule || + localModules.has(importedModule) || + counted.has(importedModule) + ) { continue } if (connection.getActiveState(runtime) === false) { continue } + counted.add(importedModule) - for (const importedChunk of compilation.chunkGraph.getModuleChunksIterable( - importedModule, - )) { + for (const importedChunk of chunksOfModule(importedModule)) { if (availableChunks.has(importedChunk) && !seen.has(importedChunk)) { seen.add(importedChunk) importedChunks.push(importedChunk) @@ -290,17 +299,34 @@ type GetStaticDependencyChunks = ( /** * Memoize static dependency resolution per chunk. Every chunk is resolved once * for its own `imports`, and CSS-only chunks are resolved again from each - * importer that reaches them. + * importer that reaches them. A module's chunks are read once, for every chunk + * that imports it. Both memos live as long as the resolver, which is created + * per normalized compilation. */ function createStaticDependencyChunkResolver( compilation: RspackCompilation, ): GetStaticDependencyChunks { const cache = new Map>() + const moduleChunks = new Map>() + const chunksOfModule = (module: RspackModule) => { + let chunks = moduleChunks.get(module) + if (!chunks) { + chunks = Array.from( + compilation.chunkGraph.getModuleChunksIterable(module), + ) + moduleChunks.set(module, chunks) + } + return chunks + } return (chunk) => { let importedChunks = cache.get(chunk) if (!importedChunks) { - importedChunks = computeStaticDependencyChunks(compilation, chunk) + importedChunks = computeStaticDependencyChunks( + compilation, + chunk, + chunksOfModule, + ) cache.set(chunk, importedChunks) } return importedChunks diff --git a/packages/start-plugin-core/tests/rsbuild/normalized-client-build.test.ts b/packages/start-plugin-core/tests/rsbuild/normalized-client-build.test.ts index ba9a033c80..66e6c58cea 100644 --- a/packages/start-plugin-core/tests/rsbuild/normalized-client-build.test.ts +++ b/packages/start-plugin-core/tests/rsbuild/normalized-client-build.test.ts @@ -469,6 +469,43 @@ describe('normalizeRspackClientBuild', () => { ]) }) + test('imports the chunk of a side-effect-free module used after its inactive import', async () => { + // The import statement's own connection to `lib.js` is inactive (no side + // effects); the later connection for the used `lib` binding is active. + const { clientBuild } = await buildClientFixture( + { + 'index.js': `globalThis.route = () => import(/* webpackChunkName: "alpha" */ './alpha.js?tsr-split=component')`, + 'alpha.js': ` + import { lib } from './lib.js' + export const render = () => lib() + 'alpha' + `, + 'lib.js': `export const lib = () => 'lib'`, + }, + { + module: { rules: [{ test: /lib\.js$/, sideEffects: false }] }, + optimization: { + splitChunks: { + minSize: 0, + cacheGroups: { + default: false, + defaultVendors: false, + vendor: { + name: 'vendor', + test: /lib\.js$/, + chunks: 'all', + enforce: true, + }, + }, + }, + }, + }, + ) + + expect(clientBuild.chunksByFileName.get('alpha.js')?.imports).toEqual([ + 'vendor.js', + ]) + }) + test('uses a dependency in the parent group without importing its copy in another entry', async () => { const { clientBuild } = await buildClientFixture( {