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] Detect modules with get-package-type (#201) #274

Merged

Conversation

lehni
Copy link
Contributor

@lehni lehni commented Mar 20, 2022

I noticed that the only way to load scripts in module mode is by explicitly adding the .mjs extension. With this PR, the type setting in a project's package.json file is also consulted. This will be more and more the way ESM-only projects are to be identified.

@bjornstar
Copy link
Collaborator

I'd like to see a test for this. Can you try to add one?

@lehni
Copy link
Contributor Author

lehni commented Mar 21, 2022

@bjornstar yes I will do that. I first wanted to check if it's going in the right direction before spending more time on it.

@lehni
Copy link
Contributor Author

lehni commented Mar 22, 2022

@bjornstar test added!

lib/wrap.js Show resolved Hide resolved
@@ -94,7 +95,7 @@ module.exports = function (
isPaused = false;
const cmd = nodeArgs.concat(wrapper, script, scriptArgs);

if (extname(script) === '.mjs') {
if (extname(script) === '.mjs' || getPackageType.sync(script) === 'module') {
Copy link
Collaborator

@bjornstar bjornstar Mar 22, 2022

Choose a reason for hiding this comment

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

This spot is a little more tricky because we have the child in the parent scope.

let's return the isESM promise here and I'll see how I can make it cleaner after it's merged.

Copy link
Contributor Author

@lehni lehni Mar 22, 2022

Choose a reason for hiding this comment

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

Funnily enough, this spot was the easy one. I just make start() async and await here, and it works, all tests pass.

    if (extname(script) === '.mjs' || await getPackageType(script) === 'module') {

@bjornstar bjornstar merged commit 6d58dd1 into fgnass:master Mar 22, 2022
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.

None yet

2 participants