Skip to content

fix: resolve directory imports with index.js in v2 addons - #707

Open
lifeart wants to merge 1 commit into
embroider-build:mainfrom
lifeart:fix-index-imports-in-v2-addons
Open

fix: resolve directory imports with index.js in v2 addons#707
lifeart wants to merge 1 commit into
embroider-build:mainfrom
lifeart:fix-index-imports-in-v2-addons

Conversation

@lifeart

@lifeart lifeart commented Mar 26, 2026

Copy link
Copy Markdown

Problem

Two related issues affect v2 addons that use directory structures with index.js files:

  1. ESM strict resolution: When a v2 addon has "type": "module" in package.json, webpack 5's strict ESM resolution requires fully specified paths. Directory imports like ./components/my-component fail because webpack won't automatically resolve to ./components/my-component/index.js.

  2. Conditional exports with .js pattern: When a v2 addon's package.json has exports like "./*": { "default": "./dist/*.js" }, importing addon/components/dir-component maps to dist/components/dir-component.js which doesn't exist — the actual file is dist/components/dir-component/index.js.

Solution

  1. fullySpecified: false webpack rule for .js files — disables strict ESM resolution, allowing webpack to naturally fall back to index.js for directory imports.

  2. Index fallback in AutoImportResolverPlugin — when module resolution fails, try appending /index to the specifier. This makes the exports pattern produce dist/components/dir-component/index.js which does exist. This integrates cleanly into the resolver plugin infrastructure from implement renamed-modules in webpack plugin to support use-ember-modules #705.

Changes

  • resolver-plugin.ts: Add /index fallback after defaultResolve() fails for package imports
  • webpack.ts: Add fullySpecified: false rule for .js files
  • v2-addon-test.ts: Add two test addons:
    • addon-with-index: ESM type: module addon with directory/index.js structure
    • addon-conditional-exports: addon with "./*": "./dist/*.js" exports pattern and directory components

Test plan

  • All 13 v2-addon test scenarios pass (ember3, release, releaseWithModules, beta, canary, fastboot, shim-requires-auto-import, cross-talk)
  • New tests verify ESM directory imports resolve to index.js
  • New tests verify conditional exports with .js pattern resolve directory imports via fallback
  • Existing flat file imports continue to work with conditional exports

🤖 Generated with Claude Code

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant