-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: loading ESM pnpmfiles #9730
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1e0d8c9
feat: loading ESM pnpmfiles
zkochan 4986775
fix: esm loading
zkochan 8f0edcc
Merge branch 'v11' into esm-pnpmfiles
zkochan 7346de3
test: fix
zkochan caa7631
Merge branch 'v11' into esm-pnpmfiles
zkochan cbb137f
test: fix
zkochan c6652ee
docs: add changesets
zkochan ffd16ab
test: fix
zkochan ba616c2
refactor: don't load default from esm pnpmfile
zkochan 15c6fbd
Merge remote-tracking branch 'origin/v11' into esm-pnpmfiles
zkochan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@pnpm/pnpmfile": minor | ||
| "pnpm": minor | ||
| --- | ||
|
|
||
| Added support for pnpmfiles written in ESM. They should have the `.mjs` extension: `.pnpmfile.mjs` [#9730](https://github.com/pnpm/pnpm/pull/9730). |
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
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
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,8 @@ | ||||||||||||||||||
| import assert from 'assert' | ||||||||||||||||||
| import fs from 'fs' | ||||||||||||||||||
| import path from 'path' | ||||||||||||||||||
| import util from 'util' | ||||||||||||||||||
| import { pathToFileURL } from 'url' | ||||||||||||||||||
| import { createRequire } from 'module' | ||||||||||||||||||
| import { PnpmError } from '@pnpm/error' | ||||||||||||||||||
| import { logger } from '@pnpm/logger' | ||||||||||||||||||
|
|
@@ -37,9 +39,18 @@ export interface Pnpmfile { | |||||||||||||||||
| finders?: Finders | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| export function requirePnpmfile (pnpmFilePath: string, prefix: string): { pnpmfileModule: Pnpmfile | undefined } | undefined { | ||||||||||||||||||
| export async function requirePnpmfile (pnpmFilePath: string, prefix: string): Promise<{ pnpmfileModule: Pnpmfile | undefined } | undefined> { | ||||||||||||||||||
| try { | ||||||||||||||||||
| const pnpmfile: Pnpmfile = require(pnpmFilePath) | ||||||||||||||||||
| let pnpmfile: Pnpmfile | ||||||||||||||||||
| // Check if it's an ESM module (ends with .mjs) | ||||||||||||||||||
| if (pnpmFilePath.endsWith('.mjs')) { | ||||||||||||||||||
| const url = pathToFileURL(path.resolve(pnpmFilePath)).href | ||||||||||||||||||
| const module = await import(url) | ||||||||||||||||||
| pnpmfile = module.default || module | ||||||||||||||||||
| } else { | ||||||||||||||||||
| // Use require for CommonJS modules | ||||||||||||||||||
| pnpmfile = require(pnpmFilePath) | ||||||||||||||||||
| } | ||||||||||||||||||
| if (typeof pnpmfile === 'undefined') { | ||||||||||||||||||
| logger.warn({ | ||||||||||||||||||
| message: `Ignoring the pnpmfile at "${pnpmFilePath}". It exports "undefined".`, | ||||||||||||||||||
|
|
@@ -89,8 +100,9 @@ export function requirePnpmfile (pnpmFilePath: string, prefix: string): { pnpmfi | |||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| function pnpmFileExistsSync (pnpmFilePath: string): boolean { | ||||||||||||||||||
| const pnpmFileRealName = pnpmFilePath.endsWith('.cjs') | ||||||||||||||||||
| const pnpmFileRealName = pnpmFilePath.endsWith('.cjs') || pnpmFilePath.endsWith('.mjs') | ||||||||||||||||||
| ? pnpmFilePath | ||||||||||||||||||
| : `${pnpmFilePath}.cjs` | ||||||||||||||||||
| return fs.existsSync(pnpmFileRealName) | ||||||||||||||||||
|
Comment on lines
+102
to
105
|
||||||||||||||||||
| const pnpmFileRealName = pnpmFilePath.endsWith('.cjs') || pnpmFilePath.endsWith('.mjs') | |
| ? pnpmFilePath | |
| : `${pnpmFilePath}.cjs` | |
| return fs.existsSync(pnpmFileRealName) | |
| if (pnpmFilePath.endsWith('.cjs') || pnpmFilePath.endsWith('.mjs')) { | |
| return fs.existsSync(pnpmFilePath) | |
| } | |
| return fs.existsSync(`${pnpmFilePath}.cjs`) || fs.existsSync(`${pnpmFilePath}.mjs`) |
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
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
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
Oops, something went wrong.
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.
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.
The fallback logic
module.default || modulemay be unclear. Consider being more explicit about when to usedefault(for default exports) vs the module itself (for named exports likeexport const hooks = {}). The current logic could mask issues if both exist.