feat: add extra dist folders control and generate clientlibs for SplitChunks#145
feat: add extra dist folders control and generate clientlibs for SplitChunks#145infloent wants to merge 4 commits into
Conversation
easingthemes
left a comment
There was a problem hiding this comment.
Nice feature — backward-compatible defaults hold up (verified the default-config path is preserved) and the fixtures/tests are thorough. A few correctness issues surface once the new opt-in flags are used, plus one watch-mode regression that affects the default workflow. Details inline.
|
|
||
| const destFile = path.join(relativePath, fileName) | ||
| // Build reverse map: absolute src path -> dest key | ||
| const srcToDest = Object.fromEntries( |
There was a problem hiding this comment.
multiple: false projects get garbage dest keys in watch.
generateEntries only returns the {dest: src} object when config.general.multiple is true; otherwise it returns a plain array of source paths. Object.entries(array) then yields [index, srcPath], so srcToDest becomes { srcPath: "0", ... } and renderStyles(file, "0", config) is called with a numeric index as the dest.
Default config is multiple: true, so this only bites projects that override it — but the old watch code didn't use generateEntries and worked regardless, so it's a new regression. A guard for the array shape (or normalizing generateEntries output) would fix it.
|
|
||
| renderStyles(file, destFile, config) | ||
| }); | ||
| watcher.on('all', (event, file) => { |
There was a problem hiding this comment.
unlink events run the full Sass pipeline on a deleted file.
on('all', ...) fires for unlink/addDir/etc. On unlink the deleted path is still a key in srcToDest, so the if (!destFile) return guard doesn't short-circuit and renderStyles runs against a now-missing file (noisy error / wasted work). Suggest filtering: if (event !== 'add' && event !== 'change') return;.
| : []), | ||
| ]; | ||
|
|
||
| const distFileNameParts = [prefix, ...featureSegments, bundleKey, ext]; |
There was a problem hiding this comment.
Silent entry loss when two files collapse to the same dest key.
The matched sourceKey is dropped from both the folder list (only added when sourceKeyAsDistFolder is true, lines 30-32) and the filename (distFileNameParts here is prefix + featureSegments + bundleKey + ext, never the sourceKey). So two files differing only by their sourceKey suffix produce an identical destFile, and sources[destFile] = ... (line 52) silently overwrites the first — no warning.
Reachable via documented opt-in combos:
sourceKey: ['publishlibs','authorlibs']+fileNameDotSuffixesAsDistFolder: true+sourceKeyAsDistFolder: false:cmp.main.publishlibs.jsandcmp.main.authorlibs.jsboth →main/cmp.main.bundle.js.- Even with
sourceKeyAsDistFolder: true, ifexcludeFileNameDotSuffixeslists those sourceKey values, they re-collapse.
The new test at utils/generateEntries.test.js:112-116 actually asserts this collapse (4 files → 2 entries) as expected. Recommend detecting duplicate destFile keys in the loop (line 50-53) and throwing/warning instead of silently dropping.
| ); | ||
| } else { | ||
| const [chunkName] = chunks; | ||
| const { name, folder, fileName, extension } = getClientlib(chunkName); |
There was a problem hiding this comment.
A chunk name with no directory writes a malformed clientlib into the dist root.
If a chunk name has no / (e.g. runtimeChunk: { name: 'runtime' }), getClientlib returns folder='.', name='.', extension='runtime' (split('.').pop() on a dotless string). renderClientLibs then does path.join(destinationPath, '.') and writes .content.xml straight into dist/ with category myproj.., while no js.txt is emitted (the runtime extension field is never read). A single dotless/dirless chunk also slips past the collision check below (everything maps to '.').
Suggest validating path.dirname(chunkName) !== '.' and that the name has a real .js/.css extension before registering. (Default config is safe — verified runtime + treeshaking share commons/treeshaking.bundle.js, so commons/ trips the >1 warning and is skipped.)
| if (!clientLibs[folder]) { | ||
| clientLibs[folder] = { name, folder }; | ||
| } | ||
| clientLibs[folder][extension] = fileName; |
There was a problem hiding this comment.
Split-chunk clientlib can silently overwrite a source-derived js/scss field.
This writes into the same clientLibs[folder] map populated by regular source entries above (keyed only by folder + extension). If a user chunk resolves to a folder already produced by a real entry (e.g. commons/foo.bundle.js), this line replaces that folder's js, dropping the original from js.txt. Consider detecting when clientLibs[folder] already came from a source entry and warning/skipping rather than merging by reference.
| // (uses mod.resource first, falls back to mod.context). | ||
| module.exports = function checkChunk(moduleOrPath, excludes = [], includes = []) { | ||
| const module = (moduleOrPath && typeof moduleOrPath === 'object') | ||
| ? (moduleOrPath.resource || moduleOrPath.context || null) |
There was a problem hiding this comment.
Minor: switching mod.context → mod.resource || mod.context widens the excludedFromVendors (['babel','core-js']) substring match from the directory to the full file path including filename. Harmless for project source (the node_modules gate protects it), but it can misclassify individual files inside node_modules whose filenames contain those tokens. Worth a brief comment noting the intent.
Description
Currently source and dist folder structure must be mirrors.
Now a source folder can not contains more than one entrypoint file of type js or scss because of AEM requirement on the dist side where each clientlib needs it’s own folder with it's own .xml, .txt files.
This will fix this limitation and allow to have multiple entrypoints in the same source folder but generate extra dist folders as required for AEM clientlibs categories.
New options
All would default to
false/[]. Existing projects would be unaffected unless they opt in.Filename-based dist folders (
config.general)sourceKey: string or string[]'clientlibs'or['clientlibs', 'publishlibs', 'authorlibs'])sourceKeyAsDistFolder: truefileNameDotSuffixesAsDistFolder: trueexcludeFileNameDotSuffixes: string[]Split chunks clientlibs (
config.clientlibs)generateSplitChunksClientlibs: true.content.xml+js.txtfor webpacksplitChunks/runtimeChunkoutput foldersThis would be independent of the filename-based flags above.
checkChunkwould also exposemodule.resource(the full file path including filename) tosplitChunkscache grouptestfunctions, making it possible to also route files by name convention into dedicated chunk groups.The idea
All files for a component live in one folder (not mandatory). One
lsreturns the complete picture of what it does and how it splits, useful for developers navigating the codebase and for AI tools that rely on directory context to understand component scope.When an AI needs context to fix or extend a component, one directory read is enough instead of correlating multiple parallel trees, which means fewer tool calls and less context consumed.
With this options
And this example source structure (not a requirement)
The generated
diststructure will be:Related Issue
Fixes: Update chokidar usage to support v4 (glob patterns removal) -> as a prerequisite.
Fixes: Flexible source vs dist folders/clientlibs management
Types of changes
Checklist: