Skip to content

feat(unstable): --unstable-detect-cjs for respecting explicit "type": "commonjs" #26149

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

Merged
merged 16 commits into from
Oct 15, 2024

Conversation

dsherret
Copy link
Member

@dsherret dsherret commented Oct 11, 2024

When using the --unstable-detect-cjs flag or adding "unstable": ["detect-cjs"] to a deno.json, it will make a JS file CJS if the closest package.json contains "type": "commonjs" and the file is not an ESM module (no TLA, no import.meta, no import/export).

@dsherret dsherret marked this pull request as ready for review October 14, 2024 00:17
@dsherret dsherret changed the title feat(unstable): unstable flag for respecting explicit "type": "commonjs" feat(unstable): --unstable-cjs-detection flag for respecting explicit "type": "commonjs" Oct 14, 2024
@dsherret dsherret changed the title feat(unstable): --unstable-cjs-detection flag for respecting explicit "type": "commonjs" feat(unstable): --unstable-cjs-detection for respecting explicit "type": "commonjs" Oct 14, 2024
@dsherret dsherret requested a review from bartlomieju October 14, 2024 00:18
} else if self.idx > 3 {
let granular_flag = crate::UNSTABLE_GRANULAR_FLAGS.get(self.idx - 4)?;
} else if self.idx > 4 {
let granular_flag = crate::UNSTABLE_GRANULAR_FLAGS.get(self.idx - 5)?;
Copy link
Member Author

Choose a reason for hiding this comment

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

Would be better if the code didn't require changing numbers like this.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

@crowlKats crowlKats Oct 14, 2024

Choose a reason for hiding this comment

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

this could just be an else case instead, though the subtraction can't be avoided

Copy link
Member Author

@dsherret dsherret Oct 14, 2024

Choose a reason for hiding this comment

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

There is already an else. Why do we do this instead of just constructing a vector or pushing to a command?

Copy link
Member Author

Choose a reason for hiding this comment

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

I removed this code in fdbb7a4

Copy link
Contributor

@marvinhagemeister marvinhagemeister left a comment

Choose a reason for hiding this comment

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

Gave this a go on a test project and it works great! The config schema in cli/schemas/config-file.v1.json might also need to be update to get that fancy autocompletion for the unstable features.

@dsherret dsherret changed the title feat(unstable): --unstable-cjs-detection for respecting explicit "type": "commonjs" feat(unstable): --unstable-detect-cjs for respecting explicit "type": "commonjs" Oct 14, 2024
Arg::new("unstable-detect-cjs")
.long("unstable-detect-cjs")
.help("Reads the package.json type field in a project to treat .js files as .cjs")
.env("DENO_UNSTABLE_PACKAGE_JSON_TYPE")
Copy link
Member

Choose a reason for hiding this comment

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

Do we really need it? Is it used only for testing? If so, maybe we should handle it manually so it doesn't show up in --help output?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops, this should be the new name, but also I don't think we should bother with these env vars. It probably hurts startup time and I bet nobody uses it.

Copy link
Member

Choose a reason for hiding this comment

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

I agree, let's remove it then.

Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

Overall looks good - could you please add some test cases for deno compile? It appears there were changes in that code as well that are currently not exercised.

@dsherret
Copy link
Member Author

dsherret commented Oct 14, 2024

Overall looks good - could you please add some test cases for deno compile? It appears there were changes in that code as well that are currently not exercised.

I'll add a message to say it's not supported in deno compile (right now it will crash in most cases).

@bartlomieju
Copy link
Member

Overall looks good - could you please add some test cases for deno compile? It appears there were changes in that code as well that are currently not exercised.

I'll add a message to say it's not supported in deno compile (right now it will crash in most cases).

Sounds good.

@dsherret dsherret merged commit 1a0cb5b into denoland:main Oct 15, 2024
17 checks passed
@dsherret dsherret deleted the feat_unstable_support_type_commonjs branch October 15, 2024 00:48
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.

4 participants