@@ -58,9 +58,16 @@ const {
58
58
ERR_NETWORK_IMPORT_DISALLOWED ,
59
59
ERR_UNSUPPORTED_ESM_URL_SCHEME ,
60
60
} = require ( 'internal/errors' ) . codes ;
61
- const { Module : CJSModule } = require ( 'internal/modules/cjs/loader' ) ;
62
61
62
+ const { Module : CJSModule } = require ( 'internal/modules/cjs/loader' ) ;
63
63
const packageJsonReader = require ( 'internal/modules/package_json_reader' ) ;
64
+ const { getPackageConfig, getPackageScopeConfig } = require ( 'internal/modules/esm/package_config' ) ;
65
+
66
+ /**
67
+ * @typedef {import('internal/modules/esm/package_config.js').PackageConfig } PackageConfig
68
+ */
69
+
70
+
64
71
const userConditions = getOptionValue ( '--conditions' ) ;
65
72
const noAddons = getOptionValue ( '--no-addons' ) ;
66
73
const addonConditions = noAddons ? [ ] : [ 'node-addons' ] ;
@@ -74,18 +81,6 @@ const DEFAULT_CONDITIONS = ObjectFreeze([
74
81
75
82
const DEFAULT_CONDITIONS_SET = new SafeSet ( DEFAULT_CONDITIONS ) ;
76
83
77
- /**
78
- * @typedef {string | string[] | Record<string, unknown> } Exports
79
- * @typedef {'module' | 'commonjs' } PackageType
80
- * @typedef {{
81
- * pjsonPath: string,
82
- * exports?: ExportConfig,
83
- * name?: string,
84
- * main?: string,
85
- * type?: PackageType,
86
- * }} PackageConfig
87
- */
88
-
89
84
const emittedPackageWarnings = new SafeSet ( ) ;
90
85
91
86
function emitTrailingSlashPatternDeprecation ( match , pjsonUrl , base ) {
@@ -154,7 +149,6 @@ function getConditionsSet(conditions) {
154
149
}
155
150
156
151
const realpathCache = new SafeMap ( ) ;
157
- const packageJSONCache = new SafeMap ( ) ; /* string -> PackageConfig */
158
152
159
153
/**
160
154
* @param {string | URL } path
@@ -163,99 +157,6 @@ const packageJSONCache = new SafeMap(); /* string -> PackageConfig */
163
157
const tryStatSync =
164
158
( path ) => statSync ( path , { throwIfNoEntry : false } ) ?? new Stats ( ) ;
165
159
166
- /**
167
- * @param {string } path
168
- * @param {string } specifier
169
- * @param {string | URL | undefined } base
170
- * @returns {PackageConfig }
171
- */
172
- function getPackageConfig ( path , specifier , base ) {
173
- const existing = packageJSONCache . get ( path ) ;
174
- if ( existing !== undefined ) {
175
- return existing ;
176
- }
177
- const source = packageJsonReader . read ( path ) . string ;
178
- if ( source === undefined ) {
179
- const packageConfig = {
180
- pjsonPath : path ,
181
- exists : false ,
182
- main : undefined ,
183
- name : undefined ,
184
- type : 'none' ,
185
- exports : undefined ,
186
- imports : undefined ,
187
- } ;
188
- packageJSONCache . set ( path , packageConfig ) ;
189
- return packageConfig ;
190
- }
191
-
192
- let packageJSON ;
193
- try {
194
- packageJSON = JSONParse ( source ) ;
195
- } catch ( error ) {
196
- throw new ERR_INVALID_PACKAGE_CONFIG (
197
- path ,
198
- ( base ? `"${ specifier } " from ` : '' ) + fileURLToPath ( base || specifier ) ,
199
- error . message
200
- ) ;
201
- }
202
-
203
- let { imports, main, name, type } = packageJSON ;
204
- const { exports } = packageJSON ;
205
- if ( typeof imports !== 'object' || imports === null ) imports = undefined ;
206
- if ( typeof main !== 'string' ) main = undefined ;
207
- if ( typeof name !== 'string' ) name = undefined ;
208
- // Ignore unknown types for forwards compatibility
209
- if ( type !== 'module' && type !== 'commonjs' ) type = 'none' ;
210
-
211
- const packageConfig = {
212
- pjsonPath : path ,
213
- exists : true ,
214
- main,
215
- name,
216
- type,
217
- exports,
218
- imports,
219
- } ;
220
- packageJSONCache . set ( path , packageConfig ) ;
221
- return packageConfig ;
222
- }
223
-
224
- /**
225
- * @param {URL | string } resolved
226
- * @returns {PackageConfig }
227
- */
228
- function getPackageScopeConfig ( resolved ) {
229
- let packageJSONUrl = new URL ( './package.json' , resolved ) ;
230
- while ( true ) {
231
- const packageJSONPath = packageJSONUrl . pathname ;
232
- if ( StringPrototypeEndsWith ( packageJSONPath , 'node_modules/package.json' ) )
233
- break ;
234
- const packageConfig = getPackageConfig ( fileURLToPath ( packageJSONUrl ) ,
235
- resolved ) ;
236
- if ( packageConfig . exists ) return packageConfig ;
237
-
238
- const lastPackageJSONUrl = packageJSONUrl ;
239
- packageJSONUrl = new URL ( '../package.json' , packageJSONUrl ) ;
240
-
241
- // Terminates at root where ../package.json equals ../../package.json
242
- // (can't just check "/package.json" for Windows support).
243
- if ( packageJSONUrl . pathname === lastPackageJSONUrl . pathname ) break ;
244
- }
245
- const packageJSONPath = fileURLToPath ( packageJSONUrl ) ;
246
- const packageConfig = {
247
- pjsonPath : packageJSONPath ,
248
- exists : false ,
249
- main : undefined ,
250
- name : undefined ,
251
- type : 'none' ,
252
- exports : undefined ,
253
- imports : undefined ,
254
- } ;
255
- packageJSONCache . set ( packageJSONPath , packageConfig ) ;
256
- return packageConfig ;
257
- }
258
-
259
160
/**
260
161
* @param {string | URL } url
261
162
* @returns {boolean }
@@ -609,7 +510,7 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath,
609
510
610
511
/**
611
512
*
612
- * @param {Exports } exports
513
+ * @param {import('internal/modules/esm/package_config.js'). Exports } exports
613
514
* @param {URL } packageJSONUrl
614
515
* @param {string | URL | undefined } base
615
516
* @returns {boolean }
@@ -799,7 +700,7 @@ function packageImportsResolve(name, base, conditions) {
799
700
800
701
/**
801
702
* @param {URL } url
802
- * @returns {PackageType }
703
+ * @returns {import('internal/modules/esm/package_config.js'). PackageType }
803
704
*/
804
705
function getPackageType ( url ) {
805
706
const packageConfig = getPackageScopeConfig ( url ) ;
0 commit comments