Skip to content

feat: implement module type resolution algorithm ESM_FILE_FORMAT from the spec#535

Merged
Boshen merged 1 commit intomainfrom
05-28-feat_implement_module_type_resolution_algorithm_esm_file_format_from_the_spec
May 28, 2025
Merged

feat: implement module type resolution algorithm ESM_FILE_FORMAT from the spec#535
Boshen merged 1 commit intomainfrom
05-28-feat_implement_module_type_resolution_algorithm_esm_file_format_from_the_spec

Conversation

@Boshen
Copy link
Member

@Boshen Boshen commented May 28, 2025

resolves #527
fixes #508

This PR adds a new module_type option and also to the returned result.

export interface NapiResolveOptions {
  /**
   * Resolve [ResolveResult::moduleType].
   *
   * Default `false`
   */
  moduleType?: boolean
}
export interface ResolveResult {
  /**
   * Module type for this path.
   *
   * The module type is computed `ESM_FILE_FORMAT` from the [ESM resolution algorithm specification](https://nodejs.org/docs/latest/api/esm.html#resolution-algorithm-specification).
   *
   *  The algorithm uses the file extension or finds the closest `package.json` with the `type` field.
   */
  moduleType?: ModuleType

}

export declare const enum ModuleType {
  Module = 'module',
  CommonJs = 'commonjs',
  Json = 'json',
  Wasm = 'wasm',
  Addon = 'addon'
}

Node.js will treat the following as ES modules:

  • Files with an .mjs extension.
  • Files with a .js extension when the nearest parent package.json file contains a top-level "type" field with a value of "module".

Node.js will treat the following as CommonJS:

  • Files with a .cjs extension.
  • Files with a .js extension when the nearest parent package.json file contains a top-level field "type" with a value of "commonjs".

See

Copilot AI review requested due to automatic review settings May 28, 2025 07:51
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Adds support for the new module_type field on resolution results to follow Node’s ESM_FILE_FORMAT rules.

  • Expose ModuleType in CJS, browser, and main entrypoints
  • Add fixtures for ESM/​CJS package.json type detection
  • Update Rust example to print and toggle the new module_type

Reviewed Changes

Copilot reviewed 30 out of 30 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
napi/resolver.wasi.cjs Export ModuleType from the native N-API binding
napi/resolver.wasi-browser.js Export ModuleType from the browser shim
napi/index.js Export ModuleType in the main entrypoint
fixtures/pnpm/package.json Reorder devDependencies and add decimal.js, mathjs, minimatch
fixtures/misc/module-type/esm/package.json Add fixture with "type": "module"
fixtures/misc/module-type/cjs/package.json Add fixture with "type": "commonjs"
examples/resolver.rs Print module_type in example and switch to CJS mode
Comments suppressed due to low confidence (2)

napi/index.js:388

  • Add unit tests to verify the new module_type resolution in key scenarios (e.g., .mjs, .cjs, and .js with package.json type) to ensure correct behavior.
module.exports.ModuleType = nativeBinding.ModuleType

fixtures/pnpm/package.json:13

  • [nitpick] The devDependencies list order is inconsistent; consider sorting entries alphabetically for easier diffs and maintenance.
"minimatch": "10.0.1"

@Boshen Boshen marked this pull request as draft May 28, 2025 07:54
@codecov
Copy link

codecov bot commented May 28, 2025

Codecov Report

Attention: Patch coverage is 97.36842% with 1 line in your changes missing coverage. Please review.

Project coverage is 93.89%. Comparing base (2c363b9) to head (a377881).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
src/lib.rs 96.77% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #535      +/-   ##
==========================================
+ Coverage   93.84%   93.89%   +0.04%     
==========================================
  Files          13       13              
  Lines        2829     2867      +38     
==========================================
+ Hits         2655     2692      +37     
- Misses        174      175       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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

@codspeed-hq
Copy link

codspeed-hq bot commented May 28, 2025

CodSpeed Performance Report

Merging #535 will not alter performance

Comparing 05-28-feat_implement_module_type_resolution_algorithm_esm_file_format_from_the_spec (a377881) with main (2c363b9)

Summary

✅ 3 untouched benchmarks

@Boshen Boshen force-pushed the 05-28-feat_implement_module_type_resolution_algorithm_esm_file_format_from_the_spec branch from 34014b1 to 99f88ea Compare May 28, 2025 08:07
@Boshen Boshen force-pushed the 05-28-feat_implement_module_type_resolution_algorithm_esm_file_format_from_the_spec branch from 99f88ea to a377881 Compare May 28, 2025 08:19
@Boshen Boshen marked this pull request as ready for review May 28, 2025 08:25
@Boshen Boshen merged commit 2ac8377 into main May 28, 2025
18 checks passed
@Boshen Boshen deleted the 05-28-feat_implement_module_type_resolution_algorithm_esm_file_format_from_the_spec branch May 28, 2025 08:26
@oxc-bot oxc-bot mentioned this pull request May 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feature request: ESM_FILE_FORMAT Bug: moduleType is incorrect

2 participants