1
1
'use strict' ;
2
2
3
- const internalFS = require ( 'internal/fs/utils' ) ;
4
3
const { NativeModule } = require ( 'internal/bootstrap/loaders' ) ;
5
- const { extname } = require ( 'path' ) ;
6
- const { realpathSync, readFileSync } = require ( 'fs' ) ;
7
- const { getOptionValue } = require ( 'internal/options' ) ;
8
- const preserveSymlinks = getOptionValue ( '--preserve-symlinks' ) ;
9
- const preserveSymlinksMain = getOptionValue ( '--preserve-symlinks-main' ) ;
10
- const { ERR_INVALID_PACKAGE_CONFIG ,
11
- ERR_TYPE_MISMATCH ,
4
+ const { ERR_TYPE_MISMATCH ,
12
5
ERR_UNKNOWN_FILE_EXTENSION } = require ( 'internal/errors' ) . codes ;
6
+ const { getOptionValue } = require ( 'internal/options' ) ;
13
7
const experimentalJsonModules = getOptionValue ( '--experimental-json-modules' ) ;
14
8
const { resolve : moduleWrapResolve } = internalBinding ( 'module_wrap' ) ;
15
- const { pathToFileURL, fileURLToPath, URL } = require ( 'internal/url' ) ;
9
+ const { pathToFileURL, fileURLToPath } = require ( 'internal/url' ) ;
16
10
const asyncESM = require ( 'internal/process/esm_loader' ) ;
17
-
18
- const realpathCache = new Map ( ) ;
19
- // TOOD(@guybedford): Shared cache with C++
20
- const pjsonCache = new Map ( ) ;
11
+ const preserveSymlinks = getOptionValue ( '--preserve-symlinks' ) ;
12
+ const preserveSymlinksMain = getOptionValue ( '--preserve-symlinks-main' ) ;
13
+ const { extname } = require ( 'path' ) ;
21
14
22
15
const extensionFormatMap = {
23
16
'__proto__' : null ,
@@ -45,75 +38,6 @@ if (experimentalJsonModules) {
45
38
} ) ;
46
39
}
47
40
48
- function readPackageConfig ( path , parentURL ) {
49
- const existing = pjsonCache . get ( path ) ;
50
- if ( existing !== undefined )
51
- return existing ;
52
- try {
53
- return JSON . parse ( readFileSync ( path ) . toString ( ) ) ;
54
- } catch ( e ) {
55
- if ( e . code === 'ENOENT' ) {
56
- pjsonCache . set ( path , null ) ;
57
- return null ;
58
- } else if ( e instanceof SyntaxError ) {
59
- throw new ERR_INVALID_PACKAGE_CONFIG ( path , fileURLToPath ( parentURL ) ) ;
60
- }
61
- throw e ;
62
- }
63
- }
64
-
65
- function getPackageBoundaryConfig ( url , parentURL ) {
66
- let pjsonURL = new URL ( 'package.json' , url ) ;
67
- while ( true ) {
68
- const pcfg = readPackageConfig ( fileURLToPath ( pjsonURL ) , parentURL ) ;
69
- if ( pcfg )
70
- return pcfg ;
71
-
72
- const lastPjsonURL = pjsonURL ;
73
- pjsonURL = new URL ( '../package.json' , pjsonURL ) ;
74
-
75
- // Terminates at root where ../package.json equals ../../package.json
76
- // (can't just check "/package.json" for Windows support).
77
- if ( pjsonURL . pathname === lastPjsonURL . pathname )
78
- return ;
79
- }
80
- }
81
-
82
- function getModuleFormat ( url , isMain , parentURL ) {
83
- const pcfg = getPackageBoundaryConfig ( url , parentURL ) ;
84
-
85
- const legacy = ! pcfg || pcfg . type !== 'module' ;
86
-
87
- const ext = extname ( url . pathname ) ;
88
-
89
- let format = ( legacy ? legacyExtensionFormatMap : extensionFormatMap ) [ ext ] ;
90
-
91
- if ( ! format ) {
92
- if ( isMain )
93
- format = legacy ? 'commonjs' : 'module' ;
94
- else
95
- throw new ERR_UNKNOWN_FILE_EXTENSION ( fileURLToPath ( url ) ,
96
- fileURLToPath ( parentURL ) ) ;
97
- }
98
-
99
- // Check for mismatch between --type and file extension,
100
- // and between --type and the "type" field in package.json.
101
- if ( isMain && format !== 'module' && asyncESM . typeFlag === 'module' ) {
102
- // Conflict between package scope type and --type
103
- if ( ext === '.js' ) {
104
- if ( pcfg && pcfg . type )
105
- throw new ERR_TYPE_MISMATCH (
106
- fileURLToPath ( url ) , ext , asyncESM . typeFlag , 'scope' ) ;
107
- // Conflict between explicit extension (.mjs, .cjs) and --type
108
- } else {
109
- throw new ERR_TYPE_MISMATCH (
110
- fileURLToPath ( url ) , ext , asyncESM . typeFlag , 'extension' ) ;
111
- }
112
- }
113
-
114
- return format ;
115
- }
116
-
117
41
function resolve ( specifier , parentURL ) {
118
42
if ( NativeModule . canBeRequiredByUsers ( specifier ) ) {
119
43
return {
@@ -126,19 +50,39 @@ function resolve(specifier, parentURL) {
126
50
if ( isMain )
127
51
parentURL = pathToFileURL ( `${ process . cwd ( ) } /` ) . href ;
128
52
129
- let url = moduleWrapResolve ( specifier , parentURL ) ;
53
+ const { url, type } =
54
+ moduleWrapResolve ( specifier , parentURL ,
55
+ isMain ? ! preserveSymlinksMain : ! preserveSymlinks ) ;
56
+
57
+ const ext = extname ( url . pathname ) ;
58
+ let format =
59
+ ( type !== 2 ? legacyExtensionFormatMap : extensionFormatMap ) [ ext ] ;
60
+
61
+ if ( isMain && asyncESM . typeFlag ) {
62
+ // Conflict between explicit extension (.mjs, .cjs) and --type
63
+ if ( ext === '.cjs' && asyncESM . typeFlag === 'module' ||
64
+ ext === '.mjs' && asyncESM . typeFlag === 'commonjs' ) {
65
+ throw new ERR_TYPE_MISMATCH (
66
+ fileURLToPath ( url ) , ext , asyncESM . typeFlag , 'extension' ) ;
67
+ }
130
68
131
- if ( isMain ? ! preserveSymlinksMain : ! preserveSymlinks ) {
132
- const real = realpathSync ( fileURLToPath ( url ) , {
133
- [ internalFS . realpathCacheKey ] : realpathCache
134
- } ) ;
135
- const old = url ;
136
- url = pathToFileURL ( real ) ;
137
- url . search = old . search ;
138
- url . hash = old . hash ;
69
+ // Conflict between package scope type and --type
70
+ if ( ext === '.js' ) {
71
+ if ( type === 2 && asyncESM . typeFlag === 'commonjs' ||
72
+ type === 1 && asyncESM . typeFlag === 'module' ) {
73
+ throw new ERR_TYPE_MISMATCH (
74
+ fileURLToPath ( url ) , ext , asyncESM . typeFlag , 'scope' ) ;
75
+ }
76
+ }
139
77
}
140
78
141
- const format = getModuleFormat ( url , isMain , parentURL ) ;
79
+ if ( ! format ) {
80
+ if ( isMain )
81
+ format = type === 2 ? 'module' : 'commonjs' ;
82
+ else
83
+ throw new ERR_UNKNOWN_FILE_EXTENSION ( fileURLToPath ( url ) ,
84
+ fileURLToPath ( parentURL ) ) ;
85
+ }
142
86
143
87
return { url : `${ url } ` , format } ;
144
88
}
0 commit comments