-
-
Notifications
You must be signed in to change notification settings - Fork 62
fix: subpath exports resolution for CJS files in dual-format packages #227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ interface ResolveOptions { | |
| interface ResolveResult { | ||
| resolved: string; | ||
| isESM: boolean; | ||
| resolvedViaExports: boolean; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -149,7 +150,7 @@ export function resolveModule( | |
| specifier: string, | ||
| options: ResolveOptions, | ||
| ): ResolveResult { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here — import the shared |
||
| const { basedir, extensions = ['.js', '.json', '.node'] } = options; | ||
| const { basedir, extensions = ['.js', '.json', '.node', '.mjs', '.cjs'] } = options; | ||
|
|
||
| // First, try ESM-style resolution with exports field | ||
| const esmResolved = tryResolveESM(specifier, basedir); | ||
|
|
@@ -158,6 +159,7 @@ export function resolveModule( | |
| return { | ||
| resolved: esmResolved, | ||
| isESM: isESMFile(esmResolved), | ||
| resolvedViaExports: true, | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -170,5 +172,6 @@ export function resolveModule( | |
| return { | ||
| resolved, | ||
| isESM: false, // CJS resolution | ||
| resolvedViaExports: false, | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,7 @@ import { pc } from './colors'; | |
| import { follow } from './follow'; | ||
| import { log, wasReported } from './log'; | ||
| import * as detector from './detector'; | ||
| import { transformESMtoCJS, rewriteMjsRequirePaths } from './esm-transformer'; | ||
| import { transformESMtoCJS } from './esm-transformer'; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please keep the |
||
| import { | ||
| ConfigDictionary, | ||
| FileRecord, | ||
|
|
@@ -77,7 +77,7 @@ const win32 = process.platform === 'win32'; | |
|
|
||
| // Extensions to try when resolving modules | ||
| // Includes .mjs to support ESM files that get transformed to .js | ||
| const MODULE_RESOLVE_EXTENSIONS = ['.js', '.json', '.node', '.mjs']; | ||
| const MODULE_RESOLVE_EXTENSIONS = ['.js', '.json', '.node', '.mjs', '.cjs']; | ||
|
|
||
| /** | ||
| * Checks if a module is a core module | ||
|
|
@@ -1121,15 +1121,6 @@ class Walker { | |
| const derivatives2: Derivative[] = []; | ||
| stepDetect(record, marker, derivatives2); | ||
| await this.stepDerivatives(record, marker, derivatives2); | ||
|
|
||
| // After dependencies are resolved, rewrite .mjs require paths to .js | ||
| // since the packer renames .mjs files to .js in the snapshot | ||
| if (record.wasTransformed && record.body) { | ||
| record.body = Buffer.from( | ||
| rewriteMjsRequirePaths(record.body.toString('utf8')), | ||
| 'utf8', | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This extensions array is now duplicated in three places (
walker.ts:80,follow.ts:76,resolver.ts:152). Please extract it to a shared constant (e.g. exportMODULE_RESOLVE_EXTENSIONSfromcommon.ts) and import it everywhere.