|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | const { |
4 | | - JSONParse, |
5 | | - ObjectPrototypeHasOwnProperty, |
6 | 4 | SafeMap, |
7 | 5 | StringPrototypeEndsWith, |
8 | 6 | } = primordials; |
9 | 7 | const { URL, fileURLToPath } = require('internal/url'); |
10 | | -const { |
11 | | - ERR_INVALID_PACKAGE_CONFIG, |
12 | | -} = require('internal/errors').codes; |
13 | | - |
14 | | -const { filterOwnProperties } = require('internal/util'); |
15 | | - |
16 | 8 |
|
17 | 9 | /** |
18 | 10 | * @typedef {string | string[] | Record<string, unknown>} Exports |
@@ -42,59 +34,23 @@ function getPackageConfig(path, specifier, base) { |
42 | 34 | return existing; |
43 | 35 | } |
44 | 36 | const packageJsonReader = require('internal/modules/package_json_reader'); |
45 | | - const source = packageJsonReader.read(path).string; |
46 | | - if (source === undefined) { |
47 | | - const packageConfig = { |
48 | | - pjsonPath: path, |
49 | | - exists: false, |
50 | | - main: undefined, |
51 | | - name: undefined, |
52 | | - type: 'none', |
53 | | - exports: undefined, |
54 | | - imports: undefined, |
55 | | - }; |
56 | | - packageJSONCache.set(path, packageConfig); |
57 | | - return packageConfig; |
58 | | - } |
59 | | - |
60 | | - let packageJSON; |
61 | | - try { |
62 | | - packageJSON = JSONParse(source); |
63 | | - } catch (error) { |
64 | | - throw new ERR_INVALID_PACKAGE_CONFIG( |
65 | | - path, |
66 | | - (base ? `"${specifier}" from ` : '') + fileURLToPath(base || specifier), |
67 | | - error.message, |
68 | | - ); |
69 | | - } |
70 | | - |
71 | | - let { imports, main, name, type } = filterOwnProperties(packageJSON, ['imports', 'main', 'name', 'type']); |
72 | | - const exports = ObjectPrototypeHasOwnProperty(packageJSON, 'exports') ? packageJSON.exports : undefined; |
73 | | - if (typeof imports !== 'object' || imports === null) { |
74 | | - imports = undefined; |
75 | | - } |
76 | | - if (typeof main !== 'string') { |
77 | | - main = undefined; |
78 | | - } |
79 | | - if (typeof name !== 'string') { |
80 | | - name = undefined; |
81 | | - } |
82 | | - // Ignore unknown types for forwards compatibility |
83 | | - if (type !== 'module' && type !== 'commonjs') { |
84 | | - type = 'none'; |
85 | | - } |
| 37 | + const result = packageJsonReader.read(path); |
| 38 | + const packageJSON = result ?? { |
| 39 | + main: undefined, |
| 40 | + name: undefined, |
| 41 | + type: 'none', |
| 42 | + exports: undefined, |
| 43 | + imports: undefined, |
| 44 | + }; |
86 | 45 |
|
87 | | - const packageConfig = { |
| 46 | + const json = { |
| 47 | + __proto__: null, |
88 | 48 | pjsonPath: path, |
89 | | - exists: true, |
90 | | - main, |
91 | | - name, |
92 | | - type, |
93 | | - exports, |
94 | | - imports, |
| 49 | + exists: result !== undefined, |
| 50 | + ...packageJSON, |
95 | 51 | }; |
96 | | - packageJSONCache.set(path, packageConfig); |
97 | | - return packageConfig; |
| 52 | + packageJSONCache.set(path, json); |
| 53 | + return json; |
98 | 54 | } |
99 | 55 |
|
100 | 56 |
|
|
0 commit comments