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

WIP Make CJS suggestions when file ext is not provided #30699

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ function getExportsForCircularRequire(module) {
// 2. If the module is native: call
// `NativeModule.prototype.compileForPublicLoader()` and return the exports.
// 3. Otherwise, create a new module for the file and save it to the cache.
// Then have it load the file contents before returning its exports
// Then have it load the file contents before returning its exports
// object.
Module._load = function(request, parent, isMain) {
let relResolveCacheIdentifier;
Expand Down
31 changes: 30 additions & 1 deletion lib/internal/modules/esm/default_resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const { NativeModule } = require('internal/bootstrap/loaders');
const { extname } = require('path');
const { realpathSync } = require('fs');
const { getOptionValue } = require('internal/options');
const {
Module: CJSModule
} = require('internal/modules/cjs/loader');


const preserveSymlinks = getOptionValue('--preserve-symlinks');
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
Expand Down Expand Up @@ -93,7 +97,32 @@ function resolve(specifier, parentURL) {
throw new ERR_INPUT_TYPE_NOT_ALLOWED();
}

let url = moduleWrapResolve(specifier, parentURL);
let url;
try {
url = moduleWrapResolve(specifier, parentURL);
} catch (error) {
try {
// By default ES Modules does not support extensionless files.
// The idea is make suggestions when file ext is not provided
// Example: import pkg from './meow'
// Ref: https://github.com/nodejs/node/issues/30603

// Create a fake CJS Module using parentURL.
const currentModule = new CJSModule(parentURL);
// Resolve our especifier e.g: 'file'
// using as module parent our current module.
// So with our fake parent, paths to search in CJS will be provided.
const cjsResolved = CJSModule._resolveFilename(specifier,
currentModule,
false);
if (cjsResolved) {
error.message += '\nCJS would resolved: ' + cjsResolved;
}
} catch (cjsError) { // eslint-disable-line no-unused-vars
// ignore
}
throw error;
}

if (isMain ? !preserveSymlinksMain : !preserveSymlinks) {
const real = realpathSync(fileURLToPath(url), {
Expand Down
4 changes: 4 additions & 0 deletions test/es-module/test-esm-cjs-loader-suggestions.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import '../common/index.mjs';

import('../fixtures/es-modules/cjs')
.catch(err => console.error(err))
Copy link
Member Author

@juanarbol juanarbol Dec 26, 2019

Choose a reason for hiding this comment

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

@BridgeAR I'm having a issue here, when I run ./node test/es-modules/this-test.mjs does not work the CJS stuff, but when I change my cd to test/es-modules, and run ../../node this-test.mjs it works, you have any suggestions or something?

Copy link
Member

Choose a reason for hiding this comment

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

@nodejs/modules-active-members PTAL

4 changes: 2 additions & 2 deletions test/message/esm_loader_not_found.out
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(node:*) ExperimentalWarning: The ESM module loader is experimental.
(node:*) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
internal/modules/esm/default_resolve.js:*
let url = moduleWrapResolve(specifier, parentURL);
^
url = moduleWrapResolve(specifier, parentURL);
^

Error: Cannot find package 'i-dont-exist' imported from *
at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:*:*)
Expand Down