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

Closed

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');

WinterCG PR: wintercg/import-meta-registry#9

@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
Member

@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.

@avivkeller avivkeller 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 4 times, most recently from 0be122b to 62df36f Compare November 5, 2024 17:03
Copy link
Member

@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 (44652ff).
Report is 42 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff            @@
##             main   #55730    +/-   ##
========================================
  Coverage   88.40%   88.40%            
========================================
  Files         654      654            
  Lines      187655   187773   +118     
  Branches    36111    36131    +20     
========================================
+ Hits       165889   165997   +108     
- Misses      14996    15008    +12     
+ Partials     6770     6768     -2     
Files with missing lines Coverage Δ
lib/internal/modules/esm/initialize_import_meta.js 100.00% <100.00%> (ø)

... and 39 files with indirect coverage changes

@jasnell
Copy link
Member

jasnell commented Nov 5, 2024

This needs to go through WinterCG before landing.

For wintercg, the most that needs to happen is a PR adding the new thing to https://github.com/wintercg/import-meta-registry

@marco-ippolito marco-ippolito added blocked PRs that are blocked by other issues or PRs. and removed blocked PRs that are blocked by other issues or PRs. labels Nov 6, 2024
@marco-ippolito
Copy link
Member Author

WinterCG PR: wintercg/import-meta-registry#9
According to winterCG it needs to wait for a week before landing.

Copy link
Contributor

@ShogunPanda ShogunPanda left a comment

Choose a reason for hiding this comment

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

LGTM!

@guybedford
Copy link
Contributor

I've posted my arguments against this in the WinterCG issue here - wintercg/import-meta-registry#9 (comment). If Node.js does not want to discuss this in a WinterCG context and would rather make it a project issue I'm happy to bring discussion back here too.

Copy link
Contributor

@aduh95 aduh95 left a comment

Choose a reason for hiding this comment

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

I would much prefer to see a different API, ideally one that could be supported by other runtimes, to address that specific use-case. Adding a request for changes to make sure this doesn't land before the cross runtime solution have been ruled out.

lib/internal/modules/esm/initialize_import_meta.js Outdated Show resolved Hide resolved
@marco-ippolito
Copy link
Member Author

marco-ippolito commented Nov 8, 2024

I would much prefer to see a different API, ideally one that could be supported by other runtimes, to address that specific use-case. Adding a request for changes to make sure this doesn't land before the cross runtime solution have been ruled out.

The feedback is not very useful honestly. If you propose changes to the API please suggest what kind of API you would like to see.

Note: the PR already has the "blocked" label, so I don't see the reason to request changes

I also dont agree with the changes proposed, all other import.meta apis are writable, so it makes sense to rely on the setter.

@aduh95
Copy link
Contributor

aduh95 commented Nov 8, 2024

all other import.meta apis are writable, so it makes sense to rely on the setter

I really don't see why the former would imply the latter 😅

I also dont agree with the changes proposed

The setOwnProperty helper is there precisely so we don't have to write the same ObjectDefineProperty is multiple places, and arguably makes the code more readable, what's not to love about that?

@marco-ippolito
Copy link
Member Author

@guybedford
Copy link
Contributor

I've posted https://github.com/guybedford/proposal-import-sync to follow-up exploration in this space, and will add it to the agenda for the TC39 modules group meeting on Thursday. If anyone is interested in discussing this you are welcome to attend, the event is available on the public TC39 calendar here - https://calendar.google.com/calendar/u/0/event?eid=c2luNm9jcm4wbTJyNXNxY2JkbGZtdmdwZWNfMjAyNDExMTRUMTYwMDAwWiAzN2EyMTA3ZGZmMTUxOTNiNDJmZmZhMDkxYmM5OTkxNjU2OTVkNDNiN2U0ZjQzYjY1ZWFiMDkzZGEyNzU3YTNhQGc.

@joyeecheung
Copy link
Member

joyeecheung commented Dec 5, 2024

By the way I don't think import.sync is functionally a proper replacement for import.meta.require in that default exports would still be treated differently and there is no way to customize what it returns directly. I was just looking at https://tailwindcss.com/docs/configuration for some bug in #56140 and started to think, whether tailwind can just load ESM .js config files now that we have require(esm), but then you'd need to properly replace the API

module.exports = {
  plugins: [
    require('@tailwindcss/forms'),
  ],
}

Now if you do this

const config = {
  plugins: [
    ????('@tailwindcss/forms'),
  ],
}

export default config;
export { config as "module.exports" };

The ???? still has to be require(). If the plugins are shipped as CJS, import.sync() is going to return { default: plugin, 'module.exports': plugin } instead of plugin. Or if the plugins are shipped as ESM (since we support require(esm) now), import.sync() is still going to return { default: plugin } instead of plugin, so in neither case is it going to be a proper replacement for require(), unless there is a way to make import.sync() support full customization of what it returns, much like the "module.exports" marker we use to support what require(esm) returns.

The example may be easier to workaround if users have control over the config and can just unwrap it, but compatibility issues can still arise from fragmented versions or if the return values get encapsulated and passed around (imagine the API just takes specifiers instead of giving control to users how it should be loaded), and this return value mismatch can happen to many other use cases. In that vein having a non-functional replacement just means it'll encourage people to do const require = createRequire(import.meta.url) to workaround its caveat, which compared to import.meta.require is likely to make the dependency harder to detect for tree shaking/bundling, instead of eaiser.

@marco-ippolito
Copy link
Member Author

marco-ippolito commented Dec 5, 2024

I think this is still worth pursuing (in a different form), I will reiterate on this once I'm back from vacation

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.