GeezerVison is showing github.com — module: merge config with `package_json_reader` · nodejs/node@9f54987
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/get_format.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,7 +19,7 @@ const { | 19 | 19 | const experimentalNetworkImports = | 20 | 20 | getOptionValue('--experimental-network-imports'); | 21 | 21 | const { containsModuleSyntax } = internalBinding('contextify'); | 22 | | -const { getPackageType } = require('internal/modules/esm/package_config'); | | 22 | +const { getPackageType } = require('internal/modules/package_json_reader'); | 23 | 23 | const { fileURLToPath } = require('internal/url'); | 24 | 24 | const { ERR_UNKNOWN_FILE_EXTENSION } = require('internal/errors').codes; | 25 | 25 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/module_job.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -228,7 +228,7 @@ class ModuleJob { | 228 | 228 | const packageConfig = | 229 | 229 | StringPrototypeStartsWith(this.module.url, 'file://') && | 230 | 230 | RegExpPrototypeExec(/\.js(\?[^#]*)?(#.*)?$/, this.module.url) !== null && | 231 | | - require('internal/modules/esm/package_config') | | 231 | + require('internal/modules/package_json_reader') | 232 | 232 | .getPackageScopeConfig(this.module.url); | 233 | 233 | if (packageConfig.type === 'module') { | 234 | 234 | e.message += | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/package_config.js -44Lines changed: 0 additions & 44 deletions Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/resolve.js +2-3Lines changed: 2 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -55,7 +55,6 @@ const { | 55 | 55 | } = require('internal/errors').codes; | 56 | 56 |
| 57 | 57 | const { Module: CJSModule } = require('internal/modules/cjs/loader'); | 58 | | -const { getPackageScopeConfig } = require('internal/modules/esm/package_config'); | 59 | 58 | const { getConditionsSet } = require('internal/modules/esm/utils'); | 60 | 59 | const packageJsonReader = require('internal/modules/package_json_reader'); | 61 | 60 | const { internalModuleStat } = internalBinding('fs'); | @@ -687,7 +686,7 @@ function packageImportsResolve(name, base, conditions) { | 687 | 686 | throw new ERR_INVALID_MODULE_SPECIFIER(name, reason, fileURLToPath(base)); | 688 | 687 | } | 689 | 688 | let packageJSONUrl; | 690 | | - const packageConfig = getPackageScopeConfig(base); | | 689 | + const packageConfig = packageJsonReader.getPackageScopeConfig(base); | 691 | 690 | if (packageConfig.exists) { | 692 | 691 | packageJSONUrl = pathToFileURL(packageConfig.pjsonPath); | 693 | 692 | const imports = packageConfig.imports; | @@ -796,7 +795,7 @@ function packageResolve(specifier, base, conditions) { | 796 | 795 | parsePackageName(specifier, base); | 797 | 796 |
| 798 | 797 | // ResolveSelf | 799 | | - const packageConfig = getPackageScopeConfig(base); | | 798 | + const packageConfig = packageJsonReader.getPackageScopeConfig(base); | 800 | 799 | if (packageConfig.exists) { | 801 | 800 | const packageJSONUrl = pathToFileURL(packageConfig.pjsonPath); | 802 | 801 | if (packageConfig.exports != null && packageConfig.name === packageName) { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/package_json_reader.js +34-2Lines changed: 34 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | 3 | const { | | 4 | + ArrayIsArray, | 4 | 5 | JSONParse, | 5 | 6 | StringPrototypeSlice, | 6 | 7 | StringPrototypeLastIndexOf, | @@ -149,11 +150,42 @@ function getNearestParentPackageJSON(checkPath) { | 149 | 150 | return { data, path }; | 150 | 151 | } | 151 | 152 |
| | 153 | +/** | | 154 | + * Returns the package configuration for the given resolved URL. | | 155 | + * @param {URL | string} resolved - The resolved URL. | | 156 | + * @returns {import('typings/internalBinding/modules').PackageConfig} - The package configuration. | | 157 | + */ | | 158 | +function getPackageScopeConfig(resolved) { | | 159 | + const result = modulesBinding.getPackageScopeConfig(`${resolved}`); | | 160 | + | | 161 | + if (ArrayIsArray(result)) { | | 162 | + return deserializePackageJSON(`${resolved}`, result, false /* checkIntegrity */); | | 163 | + } | | 164 | + | | 165 | + // This means that the response is a string | | 166 | + // and it is the path to the package.json file | | 167 | + return { | | 168 | + __proto__: null, | | 169 | + pjsonPath: result, | | 170 | + exists: false, | | 171 | + type: 'none', | | 172 | + }; | | 173 | +} | | 174 | + | | 175 | +/** | | 176 | + * Returns the package type for a given URL. | | 177 | + * @param {URL} url - The URL to get the package type for. | | 178 | + */ | | 179 | +function getPackageType(url) { | | 180 | + // TODO(@anonrig): Write a C++ function that returns only "type". | | 181 | + return getPackageScopeConfig(url).type; | | 182 | +} | | 183 | + | 152 | 184 | module.exports = { | 153 | 185 | checkPackageJSONIntegrity, | 154 | 186 | read, | 155 | 187 | readPackage, | 156 | 188 | getNearestParentPackageJSON, | 157 | | - | 158 | | - deserializePackageJSON, | | 189 | + getPackageScopeConfig, | | 190 | + getPackageType, | 159 | 191 | }; |
|