@@ -37,7 +37,7 @@ const { getOptionValue } = require('internal/options');
3737const policy = getOptionValue ( '--experimental-policy' ) ?
3838 require ( 'internal/process/policy' ) :
3939 null ;
40- const { sep, relative, resolve } = require ( 'path' ) ;
40+ const { sep, relative, resolve, extname } = require ( 'path' ) ;
4141const preserveSymlinks = getOptionValue ( '--preserve-symlinks' ) ;
4242const preserveSymlinksMain = getOptionValue ( '--preserve-symlinks-main' ) ;
4343const typeFlag = getOptionValue ( '--input-type' ) ;
@@ -62,6 +62,9 @@ const userConditions = getOptionValue('--conditions');
6262const noAddons = getOptionValue ( '--no-addons' ) ;
6363const addonConditions = noAddons ? [ ] : [ 'node-addons' ] ;
6464
65+ const experimentalSpecifierResolution =
66+ getOptionValue ( '--experimental-specifier-resolution' ) ;
67+
6568const DEFAULT_CONDITIONS = ObjectFreeze ( [
6669 'node' ,
6770 'import' ,
@@ -467,11 +470,19 @@ function resolvePackageTargetString(
467470 const composeResult = ( resolved ) => {
468471 let format ;
469472 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' ) {
474475 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+ }
475486 }
476487 } catch ( err ) {
477488 if ( err . code === 'ERR_INVALID_FILE_URL_PATH' ) {
@@ -1112,4 +1123,6 @@ module.exports = {
11121123} ;
11131124
11141125// 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