Skip to content
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

esm: add import.meta.require #55730

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

marco-ippolito
Copy link
Member

@marco-ippolito marco-ippolito commented Nov 5, 2024

History:

Consider the following cjs snippet:

function getJSON(path){
  const file = require(path);
}

The equivalent in esm would be something like (more or less):

import { createRequire } from "node:module"
function getJSON(path){
  const require = createRequire(import.meta.url);
  const file = require(path);
}
// or asyncronously
async function getJSON(path){
  const file = await import(path, { with: { type: 'json' } });
}

We could improve the dx with the new import.meta.require:

function getJSON(path){
  const file = import.meta.require(path);
}

Now that we have cjs/esm interoperability we can create import.meta.require to syncronously require modules in ESM.

It's just an alias for:

import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);

const foo = require('foo.js');

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/loaders

@nodejs-github-bot nodejs-github-bot added esm Issues and PRs related to the ECMAScript Modules implementation. needs-ci PRs that need a full CI run. labels Nov 5, 2024
Copy link
Contributor

@JakobJingleheimer JakobJingleheimer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did the discussion settle on a decision around the perf implications?

lib/internal/modules/esm/initialize_import_meta.js Outdated Show resolved Hide resolved
@marco-ippolito marco-ippolito force-pushed the import.meta.require branch 2 times, most recently from a833dcc to 8705725 Compare November 5, 2024 16:47
@marco-ippolito
Copy link
Member Author

Did the discussion settle on a decision around the perf implications?

Would be great to have some time at the collab-summit to discuss

Copy link
Member

@targos targos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there still demand for it? I'm not against it but I don't expect many people to need it when you can usually just import the common js modules.

doc/api/esm.md Outdated Show resolved Hide resolved
@ljharb
Copy link
Member

ljharb commented Nov 5, 2024

The primary use cases I can see for this is importing JSON files, so you don't have to write all the with boilerplate; and, to leverage the CJS resolution algorithm for ergonomic reasons.

@RedYetiDev RedYetiDev added the semver-minor PRs that contain new features and should be released in the next minor version. label Nov 5, 2024
@marco-ippolito marco-ippolito force-pushed the import.meta.require branch 3 times, most recently from 85e9542 to 0be122b Compare November 5, 2024 17:02
Copy link
Contributor

@JakobJingleheimer JakobJingleheimer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌 LGTM (assuming it's needed and the potential perf concern is 👍)

@guybedford
Copy link
Contributor

This seems a little weakly motivated in the PR description - it would help a lot to get more background on the specific use cases this is seeking to solve.

@marco-ippolito marco-ippolito marked this pull request as ready for review November 5, 2024 18:06
@marco-ippolito marco-ippolito added the blocked PRs that are blocked by other issues or PRs. label Nov 5, 2024
@marco-ippolito
Copy link
Member Author

marco-ippolito commented Nov 5, 2024

This needs to go through WinterCG before landing.

Copy link

codecov bot commented Nov 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.40%. Comparing base (58a7b00) to head (62df36f).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #55730   +/-   ##
=======================================
  Coverage   88.40%   88.40%           
=======================================
  Files         654      654           
  Lines      187655   187667   +12     
  Branches    36111    36114    +3     
=======================================
+ Hits       165889   165914   +25     
- Misses      14996    15001    +5     
+ Partials     6770     6752   -18     
Files with missing lines Coverage Δ
lib/internal/modules/esm/initialize_import_meta.js 100.00% <100.00%> (ø)

... and 27 files with indirect coverage changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked PRs that are blocked by other issues or PRs. esm Issues and PRs related to the ECMAScript Modules implementation. needs-ci PRs that need a full CI run. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants