import-meta-resolve
Advanced tools
Comparing version 3.1.1 to 4.0.0
@@ -34,6 +34,8 @@ /** | ||
} catch (error) { | ||
// See: <https://github.com/nodejs/node/blob/45f5c9b/lib/internal/modules/esm/initialize_import_meta.js#L34> | ||
const exception = /** @type {ErrnoException} */ (error) | ||
if ( | ||
exception.code === 'ERR_UNSUPPORTED_DIR_IMPORT' && | ||
(exception.code === 'ERR_UNSUPPORTED_DIR_IMPORT' || | ||
exception.code === 'ERR_MODULE_NOT_FOUND') && | ||
typeof exception.url === 'string' | ||
@@ -40,0 +42,0 @@ ) { |
@@ -13,3 +13,2 @@ export namespace codes { | ||
let ERR_INVALID_ARG_VALUE: new (...args: any[]) => Error; | ||
let ERR_UNSUPPORTED_ESM_URL_SCHEME: new (...args: any[]) => Error; | ||
} | ||
@@ -16,0 +15,0 @@ export type ErrnoExceptionFields = { |
@@ -17,6 +17,5 @@ /** | ||
// Manually “tree shaken” from: | ||
// <https://github.com/nodejs/node/blob/6668c4d/lib/internal/errors.js> | ||
// Last checked on: Jan 6, 2023. | ||
// <https://github.com/nodejs/node/blob/45f5c9b/lib/internal/errors.js> | ||
// Last checked on: Nov 2, 2023. | ||
import v8 from 'node:v8' | ||
import process from 'node:process' | ||
import assert from 'node:assert' | ||
@@ -28,4 +27,2 @@ // Needed for types. | ||
const isWindows = process.platform === 'win32' | ||
const own = {}.hasOwnProperty | ||
@@ -233,6 +230,8 @@ | ||
* @param {string} base | ||
* @param {string} [type] | ||
* @param {boolean} [exactUrl] | ||
*/ | ||
(path, base, type = 'package') => { | ||
return `Cannot find ${type} '${path}' imported from ${base}` | ||
(path, base, exactUrl = false) => { | ||
return `Cannot find ${ | ||
exactUrl ? 'module' : 'package' | ||
} '${path}' imported from ${base}` | ||
}, | ||
@@ -324,23 +323,2 @@ Error | ||
codes.ERR_UNSUPPORTED_ESM_URL_SCHEME = createError( | ||
'ERR_UNSUPPORTED_ESM_URL_SCHEME', | ||
/** | ||
* @param {URL} url | ||
* @param {Array<string>} supported | ||
*/ | ||
(url, supported) => { | ||
let message = `Only URLs with a scheme in: ${formatList( | ||
supported | ||
)} are supported by the default ESM loader` | ||
if (isWindows && url.protocol.length === 2) { | ||
message += '. On Windows, absolute paths must be valid file:// URLs' | ||
} | ||
message += `. Received protocol '${url.protocol}'` | ||
return message | ||
}, | ||
Error | ||
) | ||
/** | ||
@@ -347,0 +325,0 @@ * Utility function for registering the error codes. Only used here. Exported |
@@ -1,2 +0,1 @@ | ||
/// <reference types="node" resolution-mode="require"/> | ||
/** | ||
@@ -10,13 +9,5 @@ * @param {URL} url | ||
}): string | null; | ||
/** | ||
* @param {string} url | ||
* @param {{parentURL: string}} context | ||
* @returns {null | string | void} | ||
*/ | ||
export function defaultGetFormat(url: string, context: { | ||
parentURL: string; | ||
}): null | string | void; | ||
export type ProtocolHandler = (parsed: URL, context: { | ||
parentURL: string; | ||
source?: Buffer; | ||
}, ignoreErrors: boolean) => string | null | void; | ||
import { URL } from 'node:url'; |
// Manually “tree shaken” from: | ||
// <https://github.com/nodejs/node/blob/3e74590/lib/internal/modules/esm/get_format.js> | ||
// Last checked on: Apr 24, 2023. | ||
// <https://github.com/nodejs/node/blob/45f5c9b/lib/internal/modules/esm/get_format.js> | ||
// Last checked on: Nov 2, 2023. | ||
import {URL, fileURLToPath} from 'node:url' | ||
import {fileURLToPath} from 'node:url' | ||
import {getPackageType} from './resolve-get-package-type.js' | ||
@@ -40,3 +40,3 @@ import {codes} from './errors.js' | ||
* @param {URL} parsed | ||
* @param {{parentURL: string}} context | ||
* @param {{parentURL: string, source?: Buffer}} context | ||
* @param {boolean} ignoreErrors | ||
@@ -109,5 +109,24 @@ * @returns {string | null | void} | ||
if (ext === '.js') { | ||
return getPackageType(url) === 'module' ? 'module' : 'commonjs' | ||
const packageType = getPackageType(url) | ||
if (packageType !== 'none') { | ||
return packageType | ||
} | ||
return 'commonjs' | ||
} | ||
if (ext === '') { | ||
const packageType = getPackageType(url) | ||
// Legacy behavior | ||
if (packageType === 'none' || packageType === 'commonjs') { | ||
return 'commonjs' | ||
} | ||
// Note: we don’t implement WASM, so we don’t need | ||
// `getFormatOfExtensionlessFile` from `formats`. | ||
return 'module' | ||
} | ||
const format = extensionFormatMap[ext] | ||
@@ -135,20 +154,9 @@ if (format) return format | ||
export function defaultGetFormatWithoutErrors(url, context) { | ||
if (!hasOwnProperty.call(protocolHandlers, url.protocol)) { | ||
const protocol = url.protocol | ||
if (!hasOwnProperty.call(protocolHandlers, protocol)) { | ||
return null | ||
} | ||
return protocolHandlers[url.protocol](url, context, true) || null | ||
return protocolHandlers[protocol](url, context, true) || null | ||
} | ||
/** | ||
* @param {string} url | ||
* @param {{parentURL: string}} context | ||
* @returns {null | string | void} | ||
*/ | ||
export function defaultGetFormat(url, context) { | ||
const parsed = new URL(url) | ||
return hasOwnProperty.call(protocolHandlers, parsed.protocol) | ||
? protocolHandlers[parsed.protocol](parsed, context, false) | ||
: null | ||
} |
/// <reference types="node" resolution-mode="require"/> | ||
/** | ||
* @param {string} path | ||
* @param {URL | string} specifier Note: `specifier` is actually optional, not base. | ||
* @param {URL} [base] | ||
* @param {URL | string} resolved | ||
* @returns {PackageConfig} | ||
*/ | ||
export function getPackageConfig(path: string, specifier: URL | string, base?: URL | undefined): PackageConfig; | ||
/** | ||
* @param {URL} resolved | ||
* @returns {PackageConfig} | ||
*/ | ||
export function getPackageScopeConfig(resolved: URL): PackageConfig; | ||
export type ErrnoException = import('./errors.js').ErrnoException; | ||
export type PackageType = 'commonjs' | 'module' | 'none'; | ||
export type PackageConfig = { | ||
pjsonPath: string; | ||
exists: boolean; | ||
main: string | undefined; | ||
name: string | undefined; | ||
type: PackageType; | ||
exports: Record<string, unknown> | undefined; | ||
imports: Record<string, unknown> | undefined; | ||
}; | ||
export function getPackageScopeConfig(resolved: URL | string): PackageConfig; | ||
export type PackageConfig = import('./package-json-reader.js').PackageConfig; | ||
import { URL } from 'node:url'; |
// Manually “tree shaken” from: | ||
// <https://github.com/nodejs/node/blob/3e74590/lib/internal/modules/esm/package_config.js> | ||
// Last checked on: Apr 24, 2023. | ||
// <https://github.com/nodejs/node/blob/45f5c9b/lib/internal/modules/esm/package_config.js> | ||
// Last checked on: Nov 2, 2023. | ||
/** | ||
* @typedef {import('./errors.js').ErrnoException} ErrnoException | ||
* | ||
* @typedef {'commonjs' | 'module' | 'none'} PackageType | ||
* | ||
* @typedef PackageConfig | ||
* @property {string} pjsonPath | ||
* @property {boolean} exists | ||
* @property {string | undefined} main | ||
* @property {string | undefined} name | ||
* @property {PackageType} type | ||
* @property {Record<string, unknown> | undefined} exports | ||
* @property {Record<string, unknown> | undefined} imports | ||
* @typedef {import('./package-json-reader.js').PackageConfig} PackageConfig | ||
*/ | ||
import {URL, fileURLToPath} from 'node:url' | ||
import {codes} from './errors.js' | ||
import packageJsonReader from './package-json-reader.js' | ||
const {ERR_INVALID_PACKAGE_CONFIG} = codes | ||
/** @type {Map<string, PackageConfig>} */ | ||
const packageJsonCache = new Map() | ||
/** | ||
* @param {string} path | ||
* @param {URL | string} specifier Note: `specifier` is actually optional, not base. | ||
* @param {URL} [base] | ||
* @param {URL | string} resolved | ||
* @returns {PackageConfig} | ||
*/ | ||
export function getPackageConfig(path, specifier, base) { | ||
const existing = packageJsonCache.get(path) | ||
if (existing !== undefined) { | ||
return existing | ||
} | ||
export function getPackageScopeConfig(resolved) { | ||
let packageJSONUrl = new URL('package.json', resolved) | ||
const source = packageJsonReader.read(path).string | ||
if (source === undefined) { | ||
/** @type {PackageConfig} */ | ||
const packageConfig = { | ||
pjsonPath: path, | ||
exists: false, | ||
main: undefined, | ||
name: undefined, | ||
type: 'none', | ||
exports: undefined, | ||
imports: undefined | ||
while (true) { | ||
const packageJSONPath = packageJSONUrl.pathname | ||
if (packageJSONPath.endsWith('node_modules/package.json')) { | ||
break | ||
} | ||
packageJsonCache.set(path, packageConfig) | ||
return packageConfig | ||
} | ||
/** @type {Record<string, unknown>} */ | ||
let packageJson | ||
try { | ||
packageJson = JSON.parse(source) | ||
} catch (error) { | ||
const exception = /** @type {ErrnoException} */ (error) | ||
throw new ERR_INVALID_PACKAGE_CONFIG( | ||
path, | ||
(base ? `"${specifier}" from ` : '') + fileURLToPath(base || specifier), | ||
exception.message | ||
const packageConfig = packageJsonReader.read( | ||
fileURLToPath(packageJSONUrl), | ||
{specifier: resolved} | ||
) | ||
} | ||
const {exports, imports, main, name, type} = packageJson | ||
if (packageConfig.exists) { | ||
return packageConfig | ||
} | ||
/** @type {PackageConfig} */ | ||
const packageConfig = { | ||
pjsonPath: path, | ||
exists: true, | ||
main: typeof main === 'string' ? main : undefined, | ||
name: typeof name === 'string' ? name : undefined, | ||
type: type === 'module' || type === 'commonjs' ? type : 'none', | ||
// @ts-expect-error Assume `Record<string, unknown>`. | ||
exports, | ||
// @ts-expect-error Assume `Record<string, unknown>`. | ||
imports: imports && typeof imports === 'object' ? imports : undefined | ||
} | ||
packageJsonCache.set(path, packageConfig) | ||
return packageConfig | ||
} | ||
const lastPackageJSONUrl = packageJSONUrl | ||
packageJSONUrl = new URL('../package.json', packageJSONUrl) | ||
/** | ||
* @param {URL} resolved | ||
* @returns {PackageConfig} | ||
*/ | ||
export function getPackageScopeConfig(resolved) { | ||
let packageJsonUrl = new URL('package.json', resolved) | ||
while (true) { | ||
const packageJsonPath = packageJsonUrl.pathname | ||
if (packageJsonPath.endsWith('node_modules/package.json')) break | ||
const packageConfig = getPackageConfig( | ||
fileURLToPath(packageJsonUrl), | ||
resolved | ||
) | ||
if (packageConfig.exists) return packageConfig | ||
const lastPackageJsonUrl = packageJsonUrl | ||
packageJsonUrl = new URL('../package.json', packageJsonUrl) | ||
// Terminates at root where ../package.json equals ../../package.json | ||
// (can't just check "/package.json" for Windows support). | ||
if (packageJsonUrl.pathname === lastPackageJsonUrl.pathname) break | ||
if (packageJSONUrl.pathname === lastPackageJSONUrl.pathname) { | ||
break | ||
} | ||
} | ||
const packageJsonPath = fileURLToPath(packageJsonUrl) | ||
/** @type {PackageConfig} */ | ||
const packageConfig = { | ||
pjsonPath: packageJsonPath, | ||
const packageJSONPath = fileURLToPath(packageJSONUrl) | ||
return { | ||
pjsonPath: packageJSONPath, | ||
exists: false, | ||
@@ -127,4 +55,2 @@ main: undefined, | ||
} | ||
packageJsonCache.set(packageJsonPath, packageConfig) | ||
return packageConfig | ||
} |
export default reader; | ||
export type ErrnoException = import('./errors.js').ErrnoException; | ||
export type PackageType = 'commonjs' | 'module' | 'none'; | ||
export type PackageConfig = { | ||
pjsonPath: string; | ||
exists: boolean; | ||
main: string | undefined; | ||
name: string | undefined; | ||
type: PackageType; | ||
exports: Record<string, unknown> | undefined; | ||
imports: Record<string, unknown> | undefined; | ||
}; | ||
declare namespace reader { | ||
@@ -8,6 +18,8 @@ export { read }; | ||
* @param {string} jsonPath | ||
* @returns {{string: string | undefined}} | ||
* @param {{specifier: URL | string, base?: URL}} options | ||
* @returns {PackageConfig} | ||
*/ | ||
declare function read(jsonPath: string): { | ||
string: string | undefined; | ||
}; | ||
declare function read(jsonPath: string, { base, specifier }: { | ||
specifier: URL | string; | ||
base?: URL; | ||
}): PackageConfig; |
// Manually “tree shaken” from: | ||
// <https://github.com/nodejs/node/blob/3e74590/lib/internal/modules/package_json_reader.js> | ||
// Last checked on: Apr 24, 2023. | ||
// <https://github.com/nodejs/node/blob/45f5c9b/lib/internal/modules/package_json_reader.js> | ||
// Last checked on: Nov 2, 2023. | ||
// Removed the native dependency. | ||
@@ -9,2 +9,13 @@ // Also: no need to cache, we do that in resolve already. | ||
* @typedef {import('./errors.js').ErrnoException} ErrnoException | ||
* | ||
* @typedef {'commonjs' | 'module' | 'none'} PackageType | ||
* | ||
* @typedef PackageConfig | ||
* @property {string} pjsonPath | ||
* @property {boolean} exists | ||
* @property {string | undefined} main | ||
* @property {string | undefined} name | ||
* @property {PackageType} type | ||
* @property {Record<string, unknown> | undefined} exports | ||
* @property {Record<string, unknown> | undefined} imports | ||
*/ | ||
@@ -14,3 +25,12 @@ | ||
import path from 'node:path' | ||
import {fileURLToPath} from 'node:url' | ||
import {codes} from './errors.js' | ||
const hasOwnProperty = {}.hasOwnProperty | ||
const {ERR_INVALID_PACKAGE_CONFIG} = codes | ||
/** @type {Map<string, PackageConfig>} */ | ||
const cache = new Map() | ||
const reader = {read} | ||
@@ -21,22 +41,92 @@ export default reader | ||
* @param {string} jsonPath | ||
* @returns {{string: string | undefined}} | ||
* @param {{specifier: URL | string, base?: URL}} options | ||
* @returns {PackageConfig} | ||
*/ | ||
function read(jsonPath) { | ||
function read(jsonPath, {base, specifier}) { | ||
const existing = cache.get(jsonPath) | ||
if (existing) { | ||
return existing | ||
} | ||
/** @type {string | undefined} */ | ||
let string | ||
try { | ||
const string = fs.readFileSync( | ||
path.toNamespacedPath(path.join(path.dirname(jsonPath), 'package.json')), | ||
'utf8' | ||
) | ||
return {string} | ||
string = fs.readFileSync(path.toNamespacedPath(jsonPath), 'utf8') | ||
} catch (error) { | ||
const exception = /** @type {ErrnoException} */ (error) | ||
if (exception.code === 'ENOENT') { | ||
return {string: undefined} | ||
// Throw all other errors. | ||
/* c8 ignore next 4 */ | ||
if (exception.code !== 'ENOENT') { | ||
throw exception | ||
} | ||
} | ||
throw exception | ||
/** @type {PackageConfig} */ | ||
const result = { | ||
exists: false, | ||
pjsonPath: jsonPath, | ||
main: undefined, | ||
name: undefined, | ||
type: 'none', // Ignore unknown types for forwards compatibility | ||
exports: undefined, | ||
imports: undefined | ||
} | ||
if (string !== undefined) { | ||
/** @type {Record<string, unknown>} */ | ||
let parsed | ||
try { | ||
parsed = JSON.parse(string) | ||
} catch (error_) { | ||
const cause = /** @type {ErrnoException} */ (error_) | ||
const error = new ERR_INVALID_PACKAGE_CONFIG( | ||
jsonPath, | ||
(base ? `"${specifier}" from ` : '') + fileURLToPath(base || specifier), | ||
cause.message | ||
) | ||
// @ts-expect-error: fine. | ||
error.cause = cause | ||
throw error | ||
} | ||
result.exists = true | ||
if ( | ||
hasOwnProperty.call(parsed, 'name') && | ||
typeof parsed.name === 'string' | ||
) { | ||
result.name = parsed.name | ||
} | ||
if ( | ||
hasOwnProperty.call(parsed, 'main') && | ||
typeof parsed.main === 'string' | ||
) { | ||
result.main = parsed.main | ||
} | ||
if (hasOwnProperty.call(parsed, 'exports')) { | ||
// @ts-expect-error: assume valid. | ||
result.exports = parsed.exports | ||
} | ||
if (hasOwnProperty.call(parsed, 'imports')) { | ||
// @ts-expect-error: assume valid. | ||
result.imports = parsed.imports | ||
} | ||
// Ignore unknown types for forwards compatibility | ||
if ( | ||
hasOwnProperty.call(parsed, 'type') && | ||
(parsed.type === 'commonjs' || parsed.type === 'module') | ||
) { | ||
result.type = parsed.type | ||
} | ||
} | ||
cache.set(jsonPath, result) | ||
return result | ||
} |
@@ -6,2 +6,2 @@ /** | ||
export function getPackageType(url: URL): PackageType; | ||
export type PackageType = import('./package-config.js').PackageType; | ||
export type PackageType = import('./package-json-reader.js').PackageType; |
// Manually “tree shaken” from: | ||
// <https://github.com/nodejs/node/blob/3e74590/lib/internal/modules/esm/resolve.js> | ||
// Last checked on: Apr 24, 2023. | ||
// <https://github.com/nodejs/node/blob/45f5c9b/lib/internal/modules/esm/resolve.js> | ||
// Last checked on: Nov 2, 2023. | ||
// | ||
@@ -11,3 +11,3 @@ // This file solves a circular dependency. | ||
/** | ||
* @typedef {import('./package-config.js').PackageType} PackageType | ||
* @typedef {import('./package-json-reader.js').PackageType} PackageType | ||
*/ | ||
@@ -14,0 +14,0 @@ |
// Manually “tree shaken” from: | ||
// <https://github.com/nodejs/node/blob/3e74590/lib/internal/modules/esm/resolve.js> | ||
// Last checked on: Apr 24, 2023. | ||
// <https://github.com/nodejs/node/blob/45f5c9b/lib/internal/modules/esm/resolve.js> | ||
// Last checked on: Nov 2, 2023. | ||
@@ -18,3 +18,4 @@ /** | ||
import {codes} from './errors.js' | ||
import {getPackageConfig, getPackageScopeConfig} from './package-config.js' | ||
import {getPackageScopeConfig} from './package-config.js' | ||
import packageJsonReader from './package-json-reader.js' | ||
import {getConditionsSet} from './utils.js' | ||
@@ -24,5 +25,2 @@ | ||
// To do: potentially enable? | ||
const experimentalNetworkImports = false | ||
const { | ||
@@ -36,4 +34,3 @@ ERR_NETWORK_IMPORT_DISALLOWED, | ||
ERR_PACKAGE_PATH_NOT_EXPORTED, | ||
ERR_UNSUPPORTED_DIR_IMPORT, | ||
ERR_UNSUPPORTED_ESM_URL_SCHEME | ||
ERR_UNSUPPORTED_DIR_IMPORT | ||
} = codes | ||
@@ -74,2 +71,7 @@ | ||
) { | ||
// @ts-expect-error: apparently it does exist, TS. | ||
if (process.noDeprecation) { | ||
return | ||
} | ||
const pjsonPath = fileURLToPath(packageJsonUrl) | ||
@@ -97,29 +99,35 @@ const double = doubleSlashRegEx.exec(isTarget ? target : request) !== null | ||
* @param {URL} base | ||
* @param {unknown} [main] | ||
* @param {string} [main] | ||
* @returns {void} | ||
*/ | ||
function emitLegacyIndexDeprecation(url, packageJsonUrl, base, main) { | ||
// @ts-expect-error: apparently it does exist, TS. | ||
if (process.noDeprecation) { | ||
return | ||
} | ||
const format = defaultGetFormatWithoutErrors(url, {parentURL: base.href}) | ||
if (format !== 'module') return | ||
const path = fileURLToPath(url.href) | ||
const urlPath = fileURLToPath(url.href) | ||
const pkgPath = fileURLToPath(new URL('.', packageJsonUrl)) | ||
const basePath = fileURLToPath(base) | ||
if (main) | ||
if (!main) { | ||
process.emitWarning( | ||
`Package ${pkgPath} has a "main" field set to ${JSON.stringify(main)}, ` + | ||
`excluding the full filename and extension to the resolved file at "${path.slice( | ||
pkgPath.length | ||
)}", imported from ${basePath}.\n Automatic extension resolution of the "main" field is` + | ||
'deprecated for ES modules.', | ||
`No "main" or "exports" field defined in the package.json for ${pkgPath} resolving the main entry point "${urlPath.slice( | ||
pkgPath.length | ||
)}", imported from ${basePath}.\nDefault "index" lookups for the main are deprecated for ES modules.`, | ||
'DeprecationWarning', | ||
'DEP0151' | ||
) | ||
else | ||
} else if (path.resolve(pkgPath, main) !== urlPath) { | ||
process.emitWarning( | ||
`No "main" or "exports" field defined in the package.json for ${pkgPath} resolving the main entry point "${path.slice( | ||
pkgPath.length | ||
)}", imported from ${basePath}.\nDefault "index" lookups for the main are deprecated for ES modules.`, | ||
`Package ${pkgPath} has a "main" field set to "${main}", ` + | ||
`excluding the full filename and extension to the resolved file at "${urlPath.slice( | ||
pkgPath.length | ||
)}", imported from ${basePath}.\n Automatic extension resolution of the "main" field is ` + | ||
'deprecated for ES modules.', | ||
'DeprecationWarning', | ||
'DEP0151' | ||
) | ||
} | ||
} | ||
@@ -227,3 +235,3 @@ | ||
function finalizeResolution(resolved, base, preserveSymlinks) { | ||
if (encodedSepRegEx.exec(resolved.pathname) !== null) | ||
if (encodedSepRegEx.exec(resolved.pathname) !== null) { | ||
throw new ERR_INVALID_MODULE_SPECIFIER( | ||
@@ -234,5 +242,16 @@ resolved.pathname, | ||
) | ||
} | ||
const filePath = fileURLToPath(resolved) | ||
/** @type {string} */ | ||
let filePath | ||
try { | ||
filePath = fileURLToPath(resolved) | ||
} catch (error) { | ||
const cause = /** @type {ErrnoException} */ (error) | ||
Object.defineProperty(cause, 'input', {value: String(resolved)}) | ||
Object.defineProperty(cause, 'module', {value: String(base)}) | ||
throw cause | ||
} | ||
const stats = tryStatSync( | ||
@@ -250,7 +269,10 @@ filePath.endsWith('/') ? filePath.slice(-1) : filePath | ||
if (!stats.isFile()) { | ||
throw new ERR_MODULE_NOT_FOUND( | ||
const error = new ERR_MODULE_NOT_FOUND( | ||
filePath || resolved.pathname, | ||
base && fileURLToPath(base), | ||
'module' | ||
true | ||
) | ||
// @ts-expect-error Add this for `import.meta.resolve`. | ||
error.url = String(resolved) | ||
throw error | ||
} | ||
@@ -659,2 +681,7 @@ | ||
function emitTrailingSlashPatternDeprecation(match, pjsonUrl, base) { | ||
// @ts-expect-error: apparently it does exist, TS. | ||
if (process.noDeprecation) { | ||
return | ||
} | ||
const pjsonPath = fileURLToPath(pjsonUrl) | ||
@@ -998,3 +1025,6 @@ if (emittedPackageWarnings.has(pjsonPath + '|' + match)) return | ||
// Package match. | ||
const packageConfig = getPackageConfig(packageJsonPath, specifier, base) | ||
const packageConfig = packageJsonReader.read(packageJsonPath, { | ||
base, | ||
specifier | ||
}) | ||
if (packageConfig.exports !== undefined && packageConfig.exports !== null) { | ||
@@ -1018,3 +1048,3 @@ return packageExportsResolve( | ||
throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base)) | ||
throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base), false) | ||
} | ||
@@ -1195,38 +1225,2 @@ | ||
/** | ||
* @param {URL} url | ||
*/ | ||
function throwIfUnsupportedURLProtocol(url) { | ||
// Avoid accessing the `protocol` property due to the lazy getters. | ||
const protocol = url.protocol | ||
if (protocol !== 'file:' && protocol !== 'data:' && protocol !== 'node:') { | ||
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(url) | ||
} | ||
} | ||
/** | ||
* @param {URL | undefined} parsed | ||
* @param {boolean} experimentalNetworkImports | ||
*/ | ||
function throwIfUnsupportedURLScheme(parsed, experimentalNetworkImports) { | ||
// Avoid accessing the `protocol` property due to the lazy getters. | ||
const protocol = parsed?.protocol | ||
if ( | ||
protocol && | ||
protocol !== 'file:' && | ||
protocol !== 'data:' && | ||
(!experimentalNetworkImports || | ||
(protocol !== 'https:' && protocol !== 'http:')) | ||
) { | ||
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME( | ||
parsed, | ||
['file', 'data'].concat( | ||
experimentalNetworkImports ? ['https', 'http'] : [] | ||
) | ||
) | ||
} | ||
} | ||
/** | ||
* @param {string} specifier | ||
@@ -1261,7 +1255,3 @@ * @param {{parentURL?: string, conditions?: Array<string>}} context | ||
if ( | ||
protocol === 'data:' || | ||
(experimentalNetworkImports && | ||
(protocol === 'https:' || protocol === 'http:')) | ||
) { | ||
if (protocol === 'data:') { | ||
return {url: parsed.href, format: null} | ||
@@ -1287,4 +1277,2 @@ } | ||
throwIfUnsupportedURLScheme(parsed, experimentalNetworkImports) | ||
const conditions = getConditionsSet(context.conditions) | ||
@@ -1294,4 +1282,2 @@ | ||
throwIfUnsupportedURLProtocol(url) | ||
return { | ||
@@ -1298,0 +1284,0 @@ // Do NOT cast `url` to a string: that will work even when there are real |
// Manually “tree shaken” from: | ||
// <https://github.com/nodejs/node/blob/3e74590/lib/internal/modules/esm/utils.js> | ||
// Last checked on: Apr 24, 2023. | ||
// <https://github.com/nodejs/node/blob/45f5c9b/lib/internal/modules/esm/utils.js> | ||
// Last checked on: Nov 2, 2023. | ||
@@ -15,2 +15,5 @@ import {codes} from './errors.js' | ||
/** | ||
* Returns the default conditions for ES module loading. | ||
*/ | ||
function getDefaultConditions() { | ||
@@ -20,2 +23,5 @@ return DEFAULT_CONDITIONS | ||
/** | ||
* Returns the default conditions for ES module loading, as a Set. | ||
*/ | ||
function getDefaultConditionsSet() { | ||
@@ -22,0 +28,0 @@ return DEFAULT_CONDITIONS_SET |
{ | ||
"name": "import-meta-resolve", | ||
"version": "3.1.1", | ||
"version": "4.0.0", | ||
"description": "Resolve things like Node.js — ponyfill for `import.meta.resolve`", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -166,4 +166,2 @@ # import-meta-resolve | ||
— when `conditions` is incorrect | ||
* `'ERR_UNSUPPORTED_ESM_URL_SCHEME'` | ||
— when an unexpected protocol is found (`'xss:alert(1)'`) | ||
@@ -185,6 +183,13 @@ ## Algorithm | ||
* no support for CLI flags: | ||
`--experimental-json-modules`, `--experimental-wasm-modules`, | ||
`--experimental-policy`, `--experimental-network-imports`, `--no-addons`, | ||
`--input-type`, `--preserve-symlinks`, | ||
`--preserve-symlinks-main`, nor `--conditions` work | ||
`--conditions`, | ||
`--experimental-default-type`, | ||
`--experimental-json-modules`, | ||
`--experimental-network-imports`, | ||
`--experimental-policy`, | ||
`--experimental-wasm-modules`, | ||
`--input-type`, | ||
`--no-addons`, | ||
`--preserve-symlinks`, nor | ||
`--preserve-symlinks-main` | ||
work | ||
* no support for `WATCH_REPORT_DEPENDENCIES` env variable | ||
@@ -191,0 +196,0 @@ * no attempt is made to add a suggestion based on how things used to work in |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
253
76945
2095