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

treat extensionless as .js #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/get_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function defaultGetFormat(url, context, defaultGetFormatUnused) {
} else if (parsed.protocol === 'file:') {
const ext = extname(parsed.pathname);
let format;
if (ext === '.js') {
if (!ext || ext === '.js') {
format = getPackageType(parsed.href) === 'module' ? 'module' : 'commonjs';
} else {
format = extensionFormatMap[ext];
Expand Down
5 changes: 1 addition & 4 deletions lib/internal/modules/run_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ function resolveMainPath(main) {
}

function shouldUseESMLoader(mainPath) {
const userLoader = getOptionValue('--experimental-loader');
if (userLoader)
return true;

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?

Copy link
Owner Author

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.

const esModuleSpecifierResolution =
getOptionValue('--es-module-specifier-resolution');
if (esModuleSpecifierResolution === 'node')
Expand All @@ -41,7 +38,7 @@ function runMainESM(mainPath) {
const esmLoader = require('internal/process/esm_loader');
const { pathToFileURL } = require('internal/url');
const { hasUncaughtExceptionCaptureCallback } =
require('internal/process/execution');
require('internal/process/execution');
return esmLoader.initializeLoader().then(() => {
const main = path.isAbsolute(mainPath) ?
pathToFileURL(mainPath).href : mainPath;
Expand Down