fix: resolve directory imports with index.js in v2 addons - #707
Open
lifeart wants to merge 1 commit into
Open
Conversation
Two fixes for v2 addons that use directory structures with index.js files: 1. Add `fullySpecified: false` webpack rule for .js files. This disables webpack 5's strict ESM resolution, allowing directory imports like `./components/my-component` to resolve to `./components/my-component/index.js` in packages with "type": "module". 2. Add index fallback in AutoImportResolverPlugin. When module resolution fails, try appending `/index` to the specifier. This handles v2 addons whose exports field maps "./*" to "./dist/*.js" — the pattern produces a path like `dist/components/name.js` which doesn't exist, but `dist/components/name/index.js` does. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.
Problem
Two related issues affect v2 addons that use directory structures with
index.jsfiles:ESM strict resolution: When a v2 addon has
"type": "module"inpackage.json, webpack 5's strict ESM resolution requires fully specified paths. Directory imports like./components/my-componentfail because webpack won't automatically resolve to./components/my-component/index.js.Conditional exports with
.jspattern: When a v2 addon'spackage.jsonhas exports like"./*": { "default": "./dist/*.js" }, importingaddon/components/dir-componentmaps todist/components/dir-component.jswhich doesn't exist — the actual file isdist/components/dir-component/index.js.Solution
fullySpecified: falsewebpack rule for.jsfiles — disables strict ESM resolution, allowing webpack to naturally fall back toindex.jsfor directory imports.Index fallback in
AutoImportResolverPlugin— when module resolution fails, try appending/indexto the specifier. This makes the exports pattern producedist/components/dir-component/index.jswhich does exist. This integrates cleanly into the resolver plugin infrastructure from implement renamed-modules in webpack plugin to supportuse-ember-modules#705.Changes
resolver-plugin.ts: Add/indexfallback afterdefaultResolve()fails for package importswebpack.ts: AddfullySpecified: falserule for.jsfilesv2-addon-test.ts: Add two test addons:addon-with-index: ESMtype: moduleaddon with directory/index.js structureaddon-conditional-exports: addon with"./*": "./dist/*.js"exports pattern and directory componentsTest plan
.jspattern resolve directory imports via fallback🤖 Generated with Claude Code