Skip to content

feat(napi/transform): expose env.module #10934

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

feat(napi/transform): expose env.module #10934

wants to merge 4 commits into from

Conversation

sxzz
Copy link
Member

@sxzz sxzz commented May 10, 2025

No description provided.

@Copilot Copilot AI review requested due to automatic review settings May 10, 2025 19:30
Copy link
Contributor

graphite-app bot commented May 10, 2025

How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

@github-actions github-actions bot added the C-enhancement Category - New feature or request label May 10, 2025
Copy link

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

This PR implements a new option for specifying the target module type in the transformation process. The changes include adding a new field "target_module" in the transform options, mapping its value to a new Module type in the conversion logic, and simplifying the isMuslFromReport function in the JS binding.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
napi/transform/src/transformer.rs Adds "target_module" to TransformOptions and maps it to the Module enum
napi/transform/index.js Simplifies the retrieval of the process report in isMuslFromReport

@sxzz sxzz force-pushed the feat/target-module branch from ee721b0 to c372151 Compare May 10, 2025 19:45
@sxzz sxzz force-pushed the feat/target-module branch from c372151 to 3285133 Compare May 10, 2025 19:45
@Boshen Boshen self-assigned this May 11, 2025
expect(ret.errors.length).toBe(0);
expect(ret.code).toBeDefined();
expect(ret.code).toMatchInlineSnapshot(`
"import * as foo from "mod";
Copy link
Member

Choose a reason for hiding this comment

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

This is not working as intended because we don't have esm to cjs tranformation yet @Dunqing ?

Comment on lines +128 to +135
it('should configure cjs', () => {
const ret = transform('test.js', code, { modules: 'cjs' });
expect(ret.errors.length).toBe(0);
expect(ret.code).toBeDefined();
expect(ret.code).toMatchInlineSnapshot(`
"import * as foo from "mod";
"
`);
Copy link
Contributor

Choose a reason for hiding this comment

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

The test for CommonJS module transformation doesn't appear to be validating the expected behavior. When setting modules: 'cjs', the snapshot still shows ES module syntax (import * as foo from "mod";) rather than CommonJS require() statements. This suggests either:

  1. The CommonJS transformation isn't being applied correctly in the implementation
  2. The test isn't properly configured to trigger the transformation

Consider updating the test to verify that imports are actually transformed to require() calls when using the 'cjs' option, or investigate if additional configuration is needed to enable the transformation.

Suggested change
it('should configure cjs', () => {
const ret = transform('test.js', code, { modules: 'cjs' });
expect(ret.errors.length).toBe(0);
expect(ret.code).toBeDefined();
expect(ret.code).toMatchInlineSnapshot(`
"import * as foo from "mod";
"
`);
it('should configure cjs', () => {
const ret = transform('test.js', code, { modules: 'cjs' });
expect(ret.errors.length).toBe(0);
expect(ret.code).toBeDefined();
expect(ret.code).toMatchInlineSnapshot(`
"const foo = require(\\"mod\\");
"
`);
});

Spotted by Diamond

Is this helpful? React 👍 or 👎 to let us know.

Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
@Boshen
Copy link
Member

Boshen commented May 14, 2025

@sxzz we don't have esm to cjs tranform, why do you need this option exposed?

@sxzz
Copy link
Member Author

sxzz commented May 14, 2025

It's not related to tsdown, but to my side project. esbuild has the ability to transform ESM to CJS, but Oxc can't do this.

@Boshen
Copy link
Member

Boshen commented May 14, 2025

Unfortunately we don't have a esm to cjs transformer yet.

@Boshen Boshen marked this pull request as draft May 14, 2025 04:40
@sxzz sxzz mentioned this pull request May 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category - New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants