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

module: expose module format by module loader #57777

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

Conversation

legendecas
Copy link
Member

Module loaders may change a requested module format. It is not safe to
determine a .js format by package.json#type as well. Expose a helper
function to frameworks that load js files to avoid relying on
import/require failover-and-retry.

Taking mocha as an example: https://github.com/mochajs/mocha/blob/b0c269616e775689f4f28eedc0a9c5e99048139b/lib/nodejs/esm-utils.js#L41-L53.

import has a different resolution method compared to require. So mocha
relies on import and failover to require. But this approach fails when
--experimental-strip-types is enabled by default. For a legacy TypeScript
module file, its module syntax should not be depended on. And in the case
of mochajs/mocha#5314, given that strip-types is
enabled, the error becomes SyntaxError and the failover bails out.

Frameworks like mocha should take advantage of Node.js module format
detection (including custom loaders) to use import or require
deterministically.

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/loaders
  • @nodejs/typescript

@nodejs-github-bot nodejs-github-bot added esm Issues and PRs related to the ECMAScript Modules implementation. module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run. labels Apr 6, 2025
Module loaders may change a requested module format. It is not safe to
determine a `.js` format by `package.json#type` as well. Expose a helper
function to frameworks that load js files to avoid relying on
`import`/`require` failover-and-retry.
@aduh95
Copy link
Contributor

aduh95 commented Apr 6, 2025

I have a WIP for that in #55756

@legendecas
Copy link
Member Author

I have a WIP for that in #55756

Ah, that's cool! Would you mind adopting tests in this PR as well?

Copy link

codecov bot commented Apr 6, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.26%. Comparing base (8456a12) to head (07f897a).
Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #57777      +/-   ##
==========================================
+ Coverage   90.23%   90.26%   +0.03%     
==========================================
  Files         630      630              
  Lines      185245   185253       +8     
  Branches    36301    36307       +6     
==========================================
+ Hits       167154   167223      +69     
+ Misses      11006    10982      -24     
+ Partials     7085     7048      -37     
Files with missing lines Coverage Δ
lib/internal/modules/esm/formats.js 98.92% <100.00%> (+0.27%) ⬆️
lib/internal/modules/esm/loader.js 95.18% <100.00%> (+0.14%) ⬆️
lib/module.js 100.00% <100.00%> (ø)

... and 43 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member

@marco-ippolito marco-ippolito left a comment

Choose a reason for hiding this comment

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

LGTM thanks for working on this ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
esm Issues and PRs related to the ECMAScript Modules implementation. module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants