Repository navigation
Name dynamic chunks of index files after their directory - #6630
Conversation
A dynamic import that resolves to a directory's index file took the name of the file, so several such imports all produced chunks called "index". Directory names are used instead when the module is a dynamic entry whose alias is "index"; the name stays "index" when there is no directory to use.
A directory whose name starts with ".." made getDynamicAliasName return a
relative path fragment, which renderNamePattern then rejected as an invalid
"[name]" substitution, so a build that worked before failed. Test the
candidate through the same predicate that will judge it.
Cover the shapes that predicate must reject in the sample: a ".."-prefixed
directory and the ids without a parent folder ("index.js", "/index.js" and
"C:/index.js").
|
@clean99 is attempting to deploy a commit to the rollup-js Team on Vercel. A member of the Team first needs to authorize it. |
Thank you for your contribution! ❤️You can try out this pull request locally by installing Rollup via npm install clean99/rollup#contrib/rollup-index-chunk-namesNotice: To build the native dependencies, you need Rustup installed; the required nightly toolchain is then picked up automatically from |
There was a problem hiding this comment.
Note
Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.
🟢 Approval recommended
The change is narrowly scoped to improving dynamic index.* chunk naming while preserving existing naming sources, and it is backed by comprehensive new sample fixtures with pinned expected outputs across multiple formats plus only a minor documentation nit noted.
No open issues 🎉
What changed in this PR
This pull request updates dynamic chunk naming so that when a dynamic import resolves to a directory index.* file (e.g. import('./components/button') → components/button/index.js), the emitted chunk name uses the directory name when it is a safe filename fragment. This avoids multiple unrelated chunks all being named index and improves the clarity of [name]-[hash].js outputs. Resolves #4858.
Changes:
- Derive dynamic-entry chunk names from the parent directory for
index.*resolutions when the directory name is usable. - Preserve existing naming behavior for non-dynamic entries and other naming sources (e.g., explicit entry names,
preserveModules,manualChunks,emitFile, and ids without a usable parent). - Add a chunking-form sample with pinned expected outputs across ES/CJS/AMD/System, including edge cases (
..-prefixed directories and root/drive “index” ids).
Review note (nit):
test/chunking-form/samples/dynamic-import-directory-name/_config.js: Clarify wording about the “no parent folder” condition for/index.jsandC:/index.js(root/drive parent names are not usable for chunk naming).
| File | Summary |
|---|---|
test/chunking-form/samples/dynamic-import-directory-name/single.js |
Sample dynamically imported non-index module used as a baseline for naming. |
test/chunking-form/samples/dynamic-import-directory-name/main.js |
Entry sample exercising directory index.* dynamic imports and edge-case ids. |
test/chunking-form/samples/dynamic-import-directory-name/components/other/index.js |
Sample directory index.js target validating chunk name becomes other. |
test/chunking-form/samples/dynamic-import-directory-name/components/button/index.js |
Sample directory index.js target validating chunk name becomes button. |
test/chunking-form/samples/dynamic-import-directory-name/..components/index.js |
Edge case for ..-prefixed directory name (should not be used for chunk naming). |
test/chunking-form/samples/dynamic-import-directory-name/_expected/system/main.js |
Update pinned SystemJS output imports for new chunk names. |
test/chunking-form/samples/dynamic-import-directory-name/_expected/system/generated-single.js |
Pinned expected SystemJS chunk output for single. |
test/chunking-form/samples/dynamic-import-directory-name/_expected/system/generated-other.js |
Pinned expected SystemJS chunk output for other. |
test/chunking-form/samples/dynamic-import-directory-name/_expected/system/generated-index4.js |
Pinned expected SystemJS output for an index* collision/edge-case retention. |
test/chunking-form/samples/dynamic-import-directory-name/_expected/system/generated-index3.js |
Pinned expected SystemJS output for C:/index.js edge case (keeps index*). |
test/chunking-form/samples/dynamic-import-directory-name/_expected/system/generated-index2.js |
Pinned expected SystemJS output for /index.js edge case (keeps index*). |
test/chunking-form/samples/dynamic-import-directory-name/_expected/system/generated-index.js |
Pinned expected SystemJS output for index.js with no usable parent (keeps index). |
test/chunking-form/samples/dynamic-import-directory-name/_expected/system/generated-button.js |
Pinned expected SystemJS chunk output for button. |
test/chunking-form/samples/dynamic-import-directory-name/_expected/es/main.js |
Update pinned ES output imports for new chunk names. |
test/chunking-form/samples/dynamic-import-directory-name/_expected/es/generated-single.js |
Pinned expected ES chunk output for single. |
test/chunking-form/samples/dynamic-import-directory-name/_expected/es/generated-other.js |
Pinned expected ES chunk output for other. |
test/chunking-form/samples/dynamic-import-directory-name/_expected/es/generated-index4.js |
Pinned expected ES output for an index* collision/edge-case retention. |
test/chunking-form/samples/dynamic-import-directory-name/_expected/es/generated-index3.js |
Pinned expected ES output for C:/index.js edge case (keeps index*). |
test/chunking-form/samples/dynamic-import-directory-name/_expected/es/generated-index2.js |
Pinned expected ES output for /index.js edge case (keeps index*). |
test/chunking-form/samples/dynamic-import-directory-name/_expected/es/generated-index.js |
Pinned expected ES output for index.js with no usable parent (keeps index). |
test/chunking-form/samples/dynamic-import-directory-name/_expected/es/generated-button.js |
Pinned expected ES chunk output for button. |
test/chunking-form/samples/dynamic-import-directory-name/_expected/cjs/main.js |
Update pinned CJS output requires/imports for new chunk names. |
test/chunking-form/samples/dynamic-import-directory-name/_expected/cjs/generated-single.js |
Pinned expected CJS chunk output for single. |
test/chunking-form/samples/dynamic-import-directory-name/_expected/cjs/generated-other.js |
Pinned expected CJS chunk output for other. |
test/chunking-form/samples/dynamic-import-directory-name/_expected/cjs/generated-index4.js |
Pinned expected CJS output for an index* collision/edge-case retention. |
test/chunking-form/samples/dynamic-import-directory-name/_expected/cjs/generated-index3.js |
Pinned expected CJS output for C:/index.js edge case (keeps index*). |
test/chunking-form/samples/dynamic-import-directory-name/_expected/cjs/generated-index2.js |
Pinned expected CJS output for /index.js edge case (keeps index*). |
test/chunking-form/samples/dynamic-import-directory-name/_expected/cjs/generated-index.js |
Pinned expected CJS output for index.js with no usable parent (keeps index). |
test/chunking-form/samples/dynamic-import-directory-name/_expected/cjs/generated-button.js |
Pinned expected CJS chunk output for button. |
test/chunking-form/samples/dynamic-import-directory-name/_expected/amd/main.js |
Update pinned AMD output imports for new chunk names. |
test/chunking-form/samples/dynamic-import-directory-name/_expected/amd/generated-single.js |
Pinned expected AMD chunk output for single. |
test/chunking-form/samples/dynamic-import-directory-name/_expected/amd/generated-other.js |
Pinned expected AMD chunk output for other. |
test/chunking-form/samples/dynamic-import-directory-name/_expected/amd/generated-index4.js |
Pinned expected AMD output for an index* collision/edge-case retention. |
test/chunking-form/samples/dynamic-import-directory-name/_expected/amd/generated-index3.js |
Pinned expected AMD output for C:/index.js edge case (keeps index*). |
test/chunking-form/samples/dynamic-import-directory-name/_expected/amd/generated-index2.js |
Pinned expected AMD output for /index.js edge case (keeps index*). |
test/chunking-form/samples/dynamic-import-directory-name/_expected/amd/generated-index.js |
Pinned expected AMD output for index.js with no usable parent (keeps index). |
test/chunking-form/samples/dynamic-import-directory-name/_expected/amd/generated-button.js |
Pinned expected AMD chunk output for button. |
test/chunking-form/samples/dynamic-import-directory-name/_config.js |
Test config + resolver plugin to map ./dir → dir/index.js and inject edge-case ids. |
src/utils/relativeId.ts |
Add getDynamicAliasName to prefer directory names for dynamic index.* resolutions when safe. |
src/Chunk.ts |
Use the dynamic alias naming for dynamic entry facades while honoring predefined/user-provided names. |
Review effort: Balanced
Performance report
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #6630 +/- ##
==========================================
- Coverage 98.33% 98.33% -0.01%
==========================================
Files 380 380
Lines 14050 14042 -8
Branches 2996 2996
==========================================
- Hits 13816 13808 -8
Misses 192 192
Partials 42 42 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
This PR has been released as part of rollup@4.68.0. You can test it via |
This PR contains:
Are tests included?
Breaking Changes?
List any relevant issue numbers:
Description
A dynamic import that resolves to a directory's
indexfile, e.g.import('./components/button')resolving tocomponents/button/index.js, names its chunkindex. Two such imports give two chunks calledindex, and with the default[name]-[hash].jsnothing in the file name says which feature a chunk belongs to.For dynamic entries, the name is now taken from the directory when the file is an
indexfile, and only when that directory name is usable as a file name part: a name that starts with.., and the empty name of a root path, are path fragments, so those entries keepindex. Entry names,preserveModulespaths,manualChunksandemitFilenames, and ids without a parent directory (index.js,/index.js,C:/index.js) keep their current names, as the issue asked.The new chunking-form sample imports two directories and one plain file,
..-prefixed directories, and the three ids without a parent directory, and pins the emitted names and imports in all four formats.generated with AI