[2.x] fix: anchor webpack-config extension source detection to the real src directory#4770
Open
PrimateCoder wants to merge 1 commit into
Open
[2.x] fix: anchor webpack-config extension source detection to the real src directory#4770PrimateCoder wants to merge 1 commit into
PrimateCoder wants to merge 1 commit into
Conversation
… dir
The config identifies an extension's own source files (under its `src/`
dir) using loose "src" matching: `include: /src/` in index.cjs,
`resource.split(path.sep).includes('src')` / `resource.includes('/src/')`
in RegisterAsyncChunksPlugin, and `absolutePathToImport.includes('src')`
in autoChunkNameLoader.
When a project is checked out under a path that itself contains a "src"
segment (e.g. ~/src/my-ext), node_modules paths also contain "src", so:
the auto-export loader runs on third-party modules (e.g. @babel/runtime
helpers) and emits invalid flarum.reg.add(...) (build fails with "Module
parse failed"); RegisterAsyncChunksPlugin treats node_modules as
extension source and throws "Cannot read properties of undefined
(reading 'includes')"; and autoChunkNameLoader produces chunk names that
leak the checkout path.
Anchor all of these to the extension's actual src directory
(path.resolve(process.cwd(), 'src') / this.rootContext) using directory
prefix matching, and harden the _source._value access. No-op for
checkouts not under a "src" path. Adds regression tests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes a build failure in
js-packages/webpack-configthat occurs when an extension (or core) is checked out under a filesystem path that itself contains asrcsegment (e.g.~/src/my-ext).Problem
The webpack config identifies an extension's own source files (under its
src/dir) using loose "src" matching in several places:index.cjs: loader rules useinclude: /src/— a regex matching the substring "src" anywhere in a module's absolute path.RegisterAsyncChunksPlugin.cjs:module.resource.split(path.sep).includes('src')andmodule.resource.includes('/src/').autoChunkNameLoader.cjs:absolutePathToImport.includes('src')then.split('src/')[1].When the project lives under a path containing a
srcsegment, the absolute paths ofnode_modulesfiles also contain "src", so:@babel/runtime/helpers/esm/defineProperty.js, which containsexport { _defineProperty as default }) and emits invalidflarum.reg.add('…', { _defineProperty as default: … }), failing the build withModule parse failed: Unexpected token.RegisterAsyncChunksPlugintreatsnode_modulesmodules as extension source and throwsTypeError: Cannot read properties of undefined (reading 'includes')when such a module's_source._valueis undefined.autoChunkNameLoadersplits on the firstsrc/in the path, so an extension with dynamic imports gets chunk names that leak the checkout path (e.g.acme/js/…instead offorum/Lazy).This doesn't reproduce in normal CI checkouts because their paths don't contain a
srcsegment — which is why it's easy to miss.Fix
Anchor every "is this file part of the extension's
src?" check to the extension's actualsrcdirectory (path.resolve(process.cwd(), 'src'), or the loader'sthis.rootContext) and match by directory prefix instead of loose substring/segment matching. Also hardens a_source._value?.access with optional chaining.This is a no-op for checkouts not under a
srcpath (where/src/only ever matched the project's ownsrc).process.cwd()is already the basis for entry points, the output dir, andcomposer.json/package.jsonresolution throughout this config, so this introduces no new assumption.Tests
Adds
tests/srcPathAnchoring.test.js(self-contained; does not depend on the monorepocomposer.json):<cwd>/srcinclude rather than a/src/regex, and do not matchnode_modulesunder an unrelated…/src/…path;autoChunkNameLoaderproduces chunk names relative to the extension'ssrceven when the checkout path contains asrcsegment;ext:/flarum/) imports still convert toflarum.reg.asyncModuleImport(...).These fail against the pre-fix code and pass after it.
Verification
Built a real Flarum 2.x extension whose checkout path contains a
srcsegment: the build previously failed with both errors above and now succeeds. A dynamic import yieldsaddChunkModule('…','…','<ns>','forum/Lazy')(correctlysrc-relative) with no checkout-path leakage, and the produced bundle is otherwise byte-identical.