@@ -59,9 +59,16 @@ const {
59
59
ERR_NETWORK_IMPORT_DISALLOWED ,
60
60
ERR_UNSUPPORTED_ESM_URL_SCHEME ,
61
61
} = require ( 'internal/errors' ) . codes ;
62
- const { Module : CJSModule } = require ( 'internal/modules/cjs/loader' ) ;
63
62
63
+ const { Module : CJSModule } = require ( 'internal/modules/cjs/loader' ) ;
64
64
const packageJsonReader = require ( 'internal/modules/package_json_reader' ) ;
65
+ const { getPackageConfig, getPackageScopeConfig } = require ( 'internal/modules/esm/package_config' ) ;
66
+
67
+ /**
68
+ * @typedef {import('internal/modules/esm/package_config.js').PackageConfig } PackageConfig
69
+ */
70
+
71
+
65
72
const userConditions = getOptionValue ( '--conditions' ) ;
66
73
const noAddons = getOptionValue ( '--no-addons' ) ;
67
74
const addonConditions = noAddons ? [ ] : [ 'node-addons' ] ;
@@ -75,18 +82,6 @@ const DEFAULT_CONDITIONS = ObjectFreeze([
75
82
76
83
const DEFAULT_CONDITIONS_SET = new SafeSet ( DEFAULT_CONDITIONS ) ;
77
84
78
- /**
79
- * @typedef {string | string[] | Record<string, unknown> } Exports
80
- * @typedef {'module' | 'commonjs' } PackageType
81
- * @typedef {{
82
- * pjsonPath: string,
83
- * exports?: ExportConfig,
84
- * name?: string,
85
- * main?: string,
86
- * type?: PackageType,
87
- * }} PackageConfig
88
- */
89
-
90
85
const emittedPackageWarnings = new SafeSet ( ) ;
91
86
92
87
/**
@@ -179,7 +174,6 @@ function getConditionsSet(conditions) {
179
174
}
180
175
181
176
const realpathCache = new SafeMap ( ) ;
182
- const packageJSONCache = new SafeMap ( ) ; /* string -> PackageConfig */
183
177
184
178
/**
185
179
* @param {string | URL } path
@@ -188,99 +182,6 @@ const packageJSONCache = new SafeMap(); /* string -> PackageConfig */
188
182
const tryStatSync =
189
183
( path ) => statSync ( path , { throwIfNoEntry : false } ) ?? new Stats ( ) ;
190
184
191
- /**
192
- * @param {string } path
193
- * @param {string } specifier
194
- * @param {string | URL | undefined } base
195
- * @returns {PackageConfig }
196
- */
197
- function getPackageConfig ( path , specifier , base ) {
198
- const existing = packageJSONCache . get ( path ) ;
199
- if ( existing !== undefined ) {
200
- return existing ;
201
- }
202
- const source = packageJsonReader . read ( path ) . string ;
203
- if ( source === undefined ) {
204
- const packageConfig = {
205
- pjsonPath : path ,
206
- exists : false ,
207
- main : undefined ,
208
- name : undefined ,
209
- type : 'none' ,
210
- exports : undefined ,
211
- imports : undefined ,
212
- } ;
213
- packageJSONCache . set ( path , packageConfig ) ;
214
- return packageConfig ;
215
- }
216
-
217
- let packageJSON ;
218
- try {
219
- packageJSON = JSONParse ( source ) ;
220
- } catch ( error ) {
221
- throw new ERR_INVALID_PACKAGE_CONFIG (
222
- path ,
223
- ( base ? `"${ specifier } " from ` : '' ) + fileURLToPath ( base || specifier ) ,
224
- error . message
225
- ) ;
226
- }
227
-
228
- let { imports, main, name, type } = packageJSON ;
229
- const { exports } = packageJSON ;
230
- if ( typeof imports !== 'object' || imports === null ) imports = undefined ;
231
- if ( typeof main !== 'string' ) main = undefined ;
232
- if ( typeof name !== 'string' ) name = undefined ;
233
- // Ignore unknown types for forwards compatibility
234
- if ( type !== 'module' && type !== 'commonjs' ) type = 'none' ;
235
-
236
- const packageConfig = {
237
- pjsonPath : path ,
238
- exists : true ,
239
- main,
240
- name,
241
- type,
242
- exports,
243
- imports,
244
- } ;
245
- packageJSONCache . set ( path , packageConfig ) ;
246
- return packageConfig ;
247
- }
248
-
249
- /**
250
- * @param {URL | string } resolved
251
- * @returns {PackageConfig }
252
- */
253
- function getPackageScopeConfig ( resolved ) {
254
- let packageJSONUrl = new URL ( './package.json' , resolved ) ;
255
- while ( true ) {
256
- const packageJSONPath = packageJSONUrl . pathname ;
257
- if ( StringPrototypeEndsWith ( packageJSONPath , 'node_modules/package.json' ) )
258
- break ;
259
- const packageConfig = getPackageConfig ( fileURLToPath ( packageJSONUrl ) ,
260
- resolved ) ;
261
- if ( packageConfig . exists ) return packageConfig ;
262
-
263
- const lastPackageJSONUrl = packageJSONUrl ;
264
- packageJSONUrl = new URL ( '../package.json' , packageJSONUrl ) ;
265
-
266
- // Terminates at root where ../package.json equals ../../package.json
267
- // (can't just check "/package.json" for Windows support).
268
- if ( packageJSONUrl . pathname === lastPackageJSONUrl . pathname ) break ;
269
- }
270
- const packageJSONPath = fileURLToPath ( packageJSONUrl ) ;
271
- const packageConfig = {
272
- pjsonPath : packageJSONPath ,
273
- exists : false ,
274
- main : undefined ,
275
- name : undefined ,
276
- type : 'none' ,
277
- exports : undefined ,
278
- imports : undefined ,
279
- } ;
280
- packageJSONCache . set ( packageJSONPath , packageConfig ) ;
281
- return packageConfig ;
282
- }
283
-
284
185
/**
285
186
* @param {string | URL } url
286
187
* @returns {boolean }
@@ -634,7 +535,7 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath,
634
535
635
536
/**
636
537
*
637
- * @param {Exports } exports
538
+ * @param {import('internal/modules/esm/package_config.js'). Exports } exports
638
539
* @param {URL } packageJSONUrl
639
540
* @param {string | URL | undefined } base
640
541
* @returns {boolean }
@@ -838,7 +739,7 @@ function packageImportsResolve(name, base, conditions) {
838
739
839
740
/**
840
741
* @param {URL } url
841
- * @returns {PackageType }
742
+ * @returns {import('internal/modules/esm/package_config.js'). PackageType }
842
743
*/
843
744
function getPackageType ( url ) {
844
745
const packageConfig = getPackageScopeConfig ( url ) ;
0 commit comments