@@ -37,7 +37,7 @@ const { getOptionValue } = require('internal/options');
37
37
const policy = getOptionValue ( '--experimental-policy' ) ?
38
38
require ( 'internal/process/policy' ) :
39
39
null ;
40
- const { sep, relative, resolve } = require ( 'path' ) ;
40
+ const { sep, relative, resolve, extname } = require ( 'path' ) ;
41
41
const preserveSymlinks = getOptionValue ( '--preserve-symlinks' ) ;
42
42
const preserveSymlinksMain = getOptionValue ( '--preserve-symlinks-main' ) ;
43
43
const typeFlag = getOptionValue ( '--input-type' ) ;
@@ -62,6 +62,9 @@ const userConditions = getOptionValue('--conditions');
62
62
const noAddons = getOptionValue ( '--no-addons' ) ;
63
63
const addonConditions = noAddons ? [ ] : [ 'node-addons' ] ;
64
64
65
+ const experimentalSpecifierResolution =
66
+ getOptionValue ( '--experimental-specifier-resolution' ) ;
67
+
65
68
const DEFAULT_CONDITIONS = ObjectFreeze ( [
66
69
'node' ,
67
70
'import' ,
@@ -467,11 +470,19 @@ function resolvePackageTargetString(
467
470
const composeResult = ( resolved ) => {
468
471
let format ;
469
472
try {
470
- // Extension has higher priority than package.json type descriptor
471
- if ( StringPrototypeEndsWith ( resolved . href , '.mjs' ) ) {
472
- format = 'module' ;
473
- } else {
473
+ const ext = extname ( resolved . pathname ) ;
474
+ if ( ext === '.js' ) {
474
475
format = getPackageType ( resolved ) ;
476
+ } else {
477
+ format = extensionFormatMap [ ext ] ;
478
+ }
479
+ if ( ! format ) {
480
+ if ( experimentalSpecifierResolution === 'node' ) {
481
+ process . emitWarning (
482
+ 'The Node.js specifier resolution in ESM is experimental.' ,
483
+ 'ExperimentalWarning' ) ;
484
+ format = legacyExtensionFormatMap [ ext ] ;
485
+ }
475
486
}
476
487
} catch ( err ) {
477
488
if ( err . code === 'ERR_INVALID_FILE_URL_PATH' ) {
@@ -1112,4 +1123,6 @@ module.exports = {
1112
1123
} ;
1113
1124
1114
1125
// cycle
1115
- const { defaultGetFormat } = require ( 'internal/modules/esm/get_format' ) ;
1126
+ const { defaultGetFormat,
1127
+ extensionFormatMap,
1128
+ legacyExtensionFormatMap } = require ( 'internal/modules/esm/get_format' ) ;
0 commit comments