-
Notifications
You must be signed in to change notification settings - Fork 0
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
treat extensionless as .js #1
base: master
Are you sure you want to change the base?
Conversation
So this works but the problem then specifically is that it will support eg: import wasm from './x.wasm' being loaded as an ES module! Which no one will be able to support to land. The next logical step is to explicitly do this only for a file without any extension ( That is something I also never considered as a possibility, because it doesn't ambiguate from files that just use |
lib/internal/modules/run_main.js
Outdated
@@ -21,9 +21,6 @@ function resolveMainPath(main) { | |||
} | |||
|
|||
function shouldUseESMLoader(mainPath) { | |||
const userLoader = getOptionValue('--experimental-loader'); | |||
if (userLoader) | |||
return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the --loader
flag absolutely must enforce the ESM loader, because otherwise the loader will not be applied at all, when it should definitely be used.
Fixing the extension issue on its own should be enough to support the use case of loaders resolving extensionless files though I believe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes I was expecting this might be the case. And yeah, the first commit by itself should take care of it.
I was going off of this from the issue:
./entrypoint is executed as a CommonJS module whether or not --loader is passed.
It's a little unclear to me where the boundary between the ESM and CJS loaders is, as the ESM loader still seems to be able to load CommonJS files.
This reverts commit 69a29ee.
I'm not sure I follow, why would I've added another commit that treats any unknown extensions the same way as .js, with the exception thrown only in specific, prohibited cases (such as .wasm without the flag). but that might be overreaching for a first PR. |
Ah yeah sorry I missed that you are explicitly handling the no extension case as I mentioned at the end there. Ok this seems good to me then actually... do you want to try a PR? Note if there is pushback, the main-only constraint was my initial suggestion but let's see how it goes. |
Ah, nice! I actually added that other commit just now, GH seems a little flaky atm and it failed before. It will handle unknown extensions as well, but throw if |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest keeping what you had originally here and just running a PR against Node.js core with a careful description and link to the corresponding issues that it solves.
Explicitly unsupported extensions is not really a scalable approach for Node.js given that the project doesn't necessarily know what extensions might need to be supported in future. This is the standard case of "correctness" trumps "practicality", but unfortunately that is just the calculus.
'.mjs': 'module' | ||
'.mjs': 'module', | ||
'.json': 'unsupported', | ||
'.wasm': 'unsupported' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't do these explicit opt-outs - the problem is that we can't predict what file extensions might need to be supported by the module system in future.
Got it. I'll work on a PR later today. This was an attempt to support the use-case you mentioned (periods used in file names without having a "real" extension). I don't see another way to do that other than replace opt-in with opt-out, but that should be a pretty obscure use-case. |
Ok let me know if you want a final review before posting up to core. |
@guybedford
These two separate commits address the issue of using extensionless modules:
34a0f30 - extensionless files will be treated as .js
69a29ee - using the
--loader
flag will not force using ESM loader