11'use strict' ;
22
3- const { URL } = require ( 'url' ) ;
4- const CJSmodule = require ( 'internal/modules/cjs/loader' ) ;
53const internalFS = require ( 'internal/fs/utils' ) ;
64const { NativeModule } = require ( 'internal/bootstrap/loaders' ) ;
75const { extname } = require ( 'path' ) ;
86const { realpathSync } = require ( 'fs' ) ;
97const { getOptionValue } = require ( 'internal/options' ) ;
108const preserveSymlinks = getOptionValue ( '--preserve-symlinks' ) ;
119const preserveSymlinksMain = getOptionValue ( '--preserve-symlinks-main' ) ;
12- const {
13- ERR_MODULE_NOT_FOUND ,
14- ERR_UNKNOWN_FILE_EXTENSION
15- } = require ( 'internal/errors' ) . codes ;
10+ const { ERR_UNSUPPORTED_FILE_EXTENSION } = require ( 'internal/errors' ) . codes ;
1611const { resolve : moduleWrapResolve } = internalBinding ( 'module_wrap' ) ;
1712const { pathToFileURL, fileURLToPath } = require ( 'internal/url' ) ;
1813
1914const realpathCache = new Map ( ) ;
2015
21- function search ( target , base ) {
22- try {
23- return moduleWrapResolve ( target , base ) ;
24- } catch ( e ) {
25- e . stack ; // cause V8 to generate stack before rethrow
26- let error = e ;
27- try {
28- const questionedBase = new URL ( base ) ;
29- const tmpMod = new CJSmodule ( questionedBase . pathname , null ) ;
30- tmpMod . paths = CJSmodule . _nodeModulePaths (
31- new URL ( './' , questionedBase ) . pathname ) ;
32- const found = CJSmodule . _resolveFilename ( target , tmpMod ) ;
33- error = new ERR_MODULE_NOT_FOUND ( target , fileURLToPath ( base ) , found ) ;
34- } catch {
35- // ignore
36- }
37- throw error ;
38- }
39- }
40-
4116const extensionFormatMap = {
4217 '__proto__' : null ,
43- '.mjs' : 'esm'
18+ '.mjs' : 'esm' ,
19+ '.js' : 'esm'
20+ } ;
21+
22+ const legacyExtensionFormatMap = {
23+ '__proto__' : null ,
24+ '.js' : 'cjs' ,
25+ '.json' : 'cjs' ,
26+ '.mjs' : 'esm' ,
27+ '.node' : 'cjs'
4428} ;
4529
4630function resolve ( specifier , parentURL ) {
@@ -51,10 +35,14 @@ function resolve(specifier, parentURL) {
5135 } ;
5236 }
5337
54- let url = search ( specifier ,
55- parentURL || pathToFileURL ( `${ process . cwd ( ) } /` ) . href ) ;
56-
5738 const isMain = parentURL === undefined ;
39+ if ( isMain )
40+ parentURL = pathToFileURL ( `${ process . cwd ( ) } /` ) . href ;
41+
42+ const resolved = moduleWrapResolve ( specifier , parentURL , isMain ) ;
43+
44+ let url = resolved . url ;
45+ const legacy = resolved . legacy ;
5846
5947 if ( isMain ? ! preserveSymlinksMain : ! preserveSymlinks ) {
6048 const real = realpathSync ( fileURLToPath ( url ) , {
@@ -67,14 +55,14 @@ function resolve(specifier, parentURL) {
6755 }
6856
6957 const ext = extname ( url . pathname ) ;
58+ let format = ( legacy ? legacyExtensionFormatMap : extensionFormatMap ) [ ext ] ;
7059
71- let format = extensionFormatMap [ ext ] ;
7260 if ( ! format ) {
7361 if ( isMain )
74- format = 'cjs' ;
62+ format = legacy ? 'cjs' : 'esm ';
7563 else
76- throw new ERR_UNKNOWN_FILE_EXTENSION ( url . pathname ,
77- fileURLToPath ( parentURL ) ) ;
64+ throw new ERR_UNSUPPORTED_FILE_EXTENSION ( fileURLToPath ( url ) ,
65+ fileURLToPath ( parentURL ) ) ;
7866 }
7967
8068 return { url : `${ url } ` , format } ;
0 commit comments