@dual-bundle/import-meta-resolve
Advanced tools
Comparing version 4.0.0 to 4.1.0
export namespace codes { | ||
let ERR_INVALID_ARG_TYPE: new (...args: any[]) => Error; | ||
let ERR_INVALID_MODULE_SPECIFIER: new (...args: any[]) => Error; | ||
let ERR_INVALID_PACKAGE_CONFIG: new (...args: any[]) => Error; | ||
let ERR_INVALID_PACKAGE_TARGET: new (...args: any[]) => Error; | ||
let ERR_MODULE_NOT_FOUND: new (...args: any[]) => Error; | ||
let ERR_NETWORK_IMPORT_DISALLOWED: new (...args: any[]) => Error; | ||
let ERR_PACKAGE_IMPORT_NOT_DEFINED: new (...args: any[]) => Error; | ||
let ERR_PACKAGE_PATH_NOT_EXPORTED: new (...args: any[]) => Error; | ||
let ERR_UNSUPPORTED_DIR_IMPORT: new (...args: any[]) => Error; | ||
let ERR_UNKNOWN_FILE_EXTENSION: new (...args: any[]) => Error; | ||
let ERR_INVALID_ARG_VALUE: new (...args: any[]) => Error; | ||
let ERR_INVALID_ARG_TYPE: new (...parameters: any[]) => Error; | ||
let ERR_INVALID_MODULE_SPECIFIER: new (...parameters: any[]) => Error; | ||
let ERR_INVALID_PACKAGE_CONFIG: new (...parameters: any[]) => Error; | ||
let ERR_INVALID_PACKAGE_TARGET: new (...parameters: any[]) => Error; | ||
let ERR_MODULE_NOT_FOUND: new (...parameters: any[]) => Error; | ||
let ERR_NETWORK_IMPORT_DISALLOWED: new (...parameters: any[]) => Error; | ||
let ERR_PACKAGE_IMPORT_NOT_DEFINED: new (...parameters: any[]) => Error; | ||
let ERR_PACKAGE_PATH_NOT_EXPORTED: new (...parameters: any[]) => Error; | ||
let ERR_UNSUPPORTED_DIR_IMPORT: new (...parameters: any[]) => Error; | ||
let ERR_UNSUPPORTED_RESOLVE_REQUEST: new (...parameters: any[]) => Error; | ||
let ERR_UNKNOWN_FILE_EXTENSION: new (...parameters: any[]) => Error; | ||
let ERR_INVALID_ARG_VALUE: new (...parameters: any[]) => Error; | ||
} | ||
@@ -22,2 +23,2 @@ export type ErrnoExceptionFields = { | ||
export type ErrnoException = Error & ErrnoExceptionFields; | ||
export type MessageFunction = (...args: Array<any>) => string; | ||
export type MessageFunction = (...parameters: Array<any>) => string; |
@@ -13,3 +13,3 @@ /** | ||
/** | ||
* @typedef {(...args: Array<any>) => string} MessageFunction | ||
* @typedef {(...parameters: Array<any>) => string} MessageFunction | ||
*/ | ||
@@ -19,3 +19,3 @@ | ||
// <https://github.com/nodejs/node/blob/45f5c9b/lib/internal/errors.js> | ||
// Last checked on: Nov 2, 2023. | ||
// Last checked on: Apr 29, 2024. | ||
import v8 from 'node:v8' | ||
@@ -192,3 +192,3 @@ import assert from 'node:assert' | ||
/** | ||
* @param {string} pkgPath | ||
* @param {string} packagePath | ||
* @param {string} key | ||
@@ -199,4 +199,4 @@ * @param {unknown} target | ||
*/ | ||
(pkgPath, key, target, isImport = false, base = undefined) => { | ||
const relError = | ||
(packagePath, key, target, isImport = false, base = undefined) => { | ||
const relatedError = | ||
typeof target === 'string' && | ||
@@ -210,5 +210,5 @@ !isImport && | ||
`Invalid "exports" main target ${JSON.stringify(target)} defined ` + | ||
`in the package config ${pkgPath}package.json${ | ||
`in the package config ${packagePath}package.json${ | ||
base ? ` imported from ${base}` : '' | ||
}${relError ? '; targets must start with "./"' : ''}` | ||
}${relatedError ? '; targets must start with "./"' : ''}` | ||
) | ||
@@ -221,5 +221,5 @@ } | ||
target | ||
)} defined for '${key}' in the package config ${pkgPath}package.json${ | ||
)} defined for '${key}' in the package config ${packagePath}package.json${ | ||
base ? ` imported from ${base}` : '' | ||
}${relError ? '; targets must start with "./"' : ''}` | ||
}${relatedError ? '; targets must start with "./"' : ''}` | ||
}, | ||
@@ -268,12 +268,12 @@ Error | ||
/** | ||
* @param {string} pkgPath | ||
* @param {string} packagePath | ||
* @param {string} subpath | ||
* @param {string} [base] | ||
*/ | ||
(pkgPath, subpath, base = undefined) => { | ||
(packagePath, subpath, base = undefined) => { | ||
if (subpath === '.') | ||
return `No "exports" main defined in ${pkgPath}package.json${ | ||
return `No "exports" main defined in ${packagePath}package.json${ | ||
base ? ` imported from ${base}` : '' | ||
}` | ||
return `Package subpath '${subpath}' is not defined by "exports" in ${pkgPath}package.json${ | ||
return `Package subpath '${subpath}' is not defined by "exports" in ${packagePath}package.json${ | ||
base ? ` imported from ${base}` : '' | ||
@@ -292,10 +292,16 @@ }` | ||
codes.ERR_UNSUPPORTED_RESOLVE_REQUEST = createError( | ||
'ERR_UNSUPPORTED_RESOLVE_REQUEST', | ||
'Failed to resolve module specifier "%s" from "%s": Invalid relative URL or base scheme is not hierarchical.', | ||
TypeError | ||
) | ||
codes.ERR_UNKNOWN_FILE_EXTENSION = createError( | ||
'ERR_UNKNOWN_FILE_EXTENSION', | ||
/** | ||
* @param {string} ext | ||
* @param {string} extension | ||
* @param {string} path | ||
*/ | ||
(ext, path) => { | ||
return `Unknown file extension "${ext}" for ${path}` | ||
(extension, path) => { | ||
return `Unknown file extension "${extension}" for ${path}` | ||
}, | ||
@@ -333,6 +339,6 @@ TypeError | ||
* @param {MessageFunction | string} value | ||
* @param {ErrorConstructor} def | ||
* @returns {new (...args: Array<any>) => Error} | ||
* @param {ErrorConstructor} constructor | ||
* @returns {new (...parameters: Array<any>) => Error} | ||
*/ | ||
function createError(sym, value, def) { | ||
function createError(sym, value, constructor) { | ||
// Special case for SystemError that formats the error message differently | ||
@@ -342,3 +348,3 @@ // The SystemErrors only have SystemError as their base classes. | ||
return makeNodeErrorWithCode(def, sym) | ||
return makeNodeErrorWithCode(constructor, sym) | ||
} | ||
@@ -355,5 +361,5 @@ | ||
/** | ||
* @param {Array<unknown>} args | ||
* @param {Array<unknown>} parameters | ||
*/ | ||
function NodeError(...args) { | ||
function NodeError(...parameters) { | ||
const limit = Error.stackTraceLimit | ||
@@ -364,3 +370,3 @@ if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = 0 | ||
if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = limit | ||
const message = getMessage(key, args, error) | ||
const message = getMessage(key, parameters, error) | ||
Object.defineProperties(error, { | ||
@@ -417,12 +423,12 @@ // Note: no need to implement `kIsNodeError` symbol, would be hard, | ||
* This function removes unnecessary frames from Node.js core errors. | ||
* @template {(...args: unknown[]) => unknown} T | ||
* @param {T} fn | ||
* @template {(...parameters: unknown[]) => unknown} T | ||
* @param {T} wrappedFunction | ||
* @returns {T} | ||
*/ | ||
function hideStackFrames(fn) { | ||
function hideStackFrames(wrappedFunction) { | ||
// We rename the functions that will be hidden to cut off the stacktrace | ||
// at the outermost one | ||
const hidden = nodeInternalPrefix + fn.name | ||
Object.defineProperty(fn, 'name', {value: hidden}) | ||
return fn | ||
const hidden = nodeInternalPrefix + wrappedFunction.name | ||
Object.defineProperty(wrappedFunction, 'name', {value: hidden}) | ||
return wrappedFunction | ||
} | ||
@@ -454,7 +460,7 @@ | ||
* @param {string} key | ||
* @param {Array<unknown>} args | ||
* @param {Array<unknown>} parameters | ||
* @param {Error} self | ||
* @returns {string} | ||
*/ | ||
function getMessage(key, args, self) { | ||
function getMessage(key, parameters, self) { | ||
const message = messages.get(key) | ||
@@ -465,7 +471,7 @@ assert(message !== undefined, 'expected `message` to be found') | ||
assert( | ||
message.length <= args.length, // Default options do not count. | ||
`Code: ${key}; The provided arguments length (${args.length}) does not ` + | ||
message.length <= parameters.length, // Default options do not count. | ||
`Code: ${key}; The provided arguments length (${parameters.length}) does not ` + | ||
`match the required ones (${message.length}).` | ||
) | ||
return Reflect.apply(message, self, args) | ||
return Reflect.apply(message, self, parameters) | ||
} | ||
@@ -477,10 +483,10 @@ | ||
assert( | ||
expectedLength === args.length, | ||
`Code: ${key}; The provided arguments length (${args.length}) does not ` + | ||
expectedLength === parameters.length, | ||
`Code: ${key}; The provided arguments length (${parameters.length}) does not ` + | ||
`match the required ones (${expectedLength}).` | ||
) | ||
if (args.length === 0) return message | ||
if (parameters.length === 0) return message | ||
args.unshift(message) | ||
return Reflect.apply(format, null, args) | ||
parameters.unshift(message) | ||
return Reflect.apply(format, null, parameters) | ||
} | ||
@@ -487,0 +493,0 @@ |
// Manually “tree shaken” from: | ||
// <https://github.com/nodejs/node/blob/45f5c9b/lib/internal/modules/esm/get_format.js> | ||
// Last checked on: Nov 2, 2023. | ||
// <https://github.com/nodejs/node/blob/7c3dce0/lib/internal/modules/esm/get_format.js> | ||
// Last checked on: Apr 29, 2024. | ||
import {fileURLToPath} from 'node:url' | ||
import {getPackageType} from './resolve-get-package-type.js' | ||
import {getPackageType} from './package-json-reader.js' | ||
import {codes} from './errors.js' | ||
@@ -105,5 +105,5 @@ | ||
function getFileProtocolModuleFormat(url, _context, ignoreErrors) { | ||
const ext = extname(url) | ||
const value = extname(url) | ||
if (ext === '.js') { | ||
if (value === '.js') { | ||
const packageType = getPackageType(url) | ||
@@ -118,3 +118,3 @@ | ||
if (ext === '') { | ||
if (value === '') { | ||
const packageType = getPackageType(url) | ||
@@ -132,3 +132,3 @@ | ||
const format = extensionFormatMap[ext] | ||
const format = extensionFormatMap[value] | ||
if (format) return format | ||
@@ -142,3 +142,3 @@ | ||
const filepath = fileURLToPath(url) | ||
throw new ERR_UNKNOWN_FILE_EXTENSION(ext, filepath) | ||
throw new ERR_UNKNOWN_FILE_EXTENSION(value, filepath) | ||
} | ||
@@ -145,0 +145,0 @@ |
@@ -1,16 +0,1 @@ | ||
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 { | ||
export { read }; | ||
} | ||
/** | ||
@@ -21,5 +6,27 @@ * @param {string} jsonPath | ||
*/ | ||
declare function read(jsonPath: string, { base, specifier }: { | ||
export function read(jsonPath: string, { base, specifier }: { | ||
specifier: URL | string; | ||
base?: URL; | ||
}): PackageConfig; | ||
/** | ||
* @param {URL | string} resolved | ||
* @returns {PackageConfig} | ||
*/ | ||
export function getPackageScopeConfig(resolved: URL | string): PackageConfig; | ||
/** | ||
* Returns the package type for a given URL. | ||
* @param {URL} url - The URL to get the package type for. | ||
* @returns {PackageType} | ||
*/ | ||
export function getPackageType(url: URL): PackageType; | ||
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; | ||
}; |
// Manually “tree shaken” from: | ||
// <https://github.com/nodejs/node/blob/45f5c9b/lib/internal/modules/package_json_reader.js> | ||
// Last checked on: Nov 2, 2023. | ||
// <https://github.com/nodejs/node/blob/7c3dce0/lib/internal/modules/package_json_reader.js> | ||
// Last checked on: Apr 29, 2024. | ||
// Removed the native dependency. | ||
@@ -15,7 +15,7 @@ // Also: no need to cache, we do that in resolve already. | ||
* @property {boolean} exists | ||
* @property {string | undefined} main | ||
* @property {string | undefined} name | ||
* @property {string | undefined} [main] | ||
* @property {string | undefined} [name] | ||
* @property {PackageType} type | ||
* @property {Record<string, unknown> | undefined} exports | ||
* @property {Record<string, unknown> | undefined} imports | ||
* @property {Record<string, unknown> | undefined} [exports] | ||
* @property {Record<string, unknown> | undefined} [imports] | ||
*/ | ||
@@ -35,5 +35,2 @@ | ||
const reader = {read} | ||
export default reader | ||
/** | ||
@@ -44,3 +41,3 @@ * @param {string} jsonPath | ||
*/ | ||
function read(jsonPath, {base, specifier}) { | ||
export function read(jsonPath, {base, specifier}) { | ||
const existing = cache.get(jsonPath) | ||
@@ -89,3 +86,2 @@ | ||
) | ||
// @ts-expect-error: fine. | ||
error.cause = cause | ||
@@ -134,1 +130,53 @@ throw error | ||
} | ||
/** | ||
* @param {URL | string} resolved | ||
* @returns {PackageConfig} | ||
*/ | ||
export function getPackageScopeConfig(resolved) { | ||
// Note: in Node, this is now a native module. | ||
let packageJSONUrl = new URL('package.json', resolved) | ||
while (true) { | ||
const packageJSONPath = packageJSONUrl.pathname | ||
if (packageJSONPath.endsWith('node_modules/package.json')) { | ||
break | ||
} | ||
const packageConfig = read(fileURLToPath(packageJSONUrl), { | ||
specifier: 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 | ||
} | ||
} | ||
const packageJSONPath = fileURLToPath(packageJSONUrl) | ||
// ^^ Note: in Node, this is now a native module. | ||
return { | ||
pjsonPath: packageJSONPath, | ||
exists: false, | ||
type: 'none' | ||
} | ||
} | ||
/** | ||
* Returns the package type for a given URL. | ||
* @param {URL} url - The URL to get the package type for. | ||
* @returns {PackageType} | ||
*/ | ||
export function getPackageType(url) { | ||
// To do @anonrig: Write a C++ function that returns only "type". | ||
return getPackageScopeConfig(url).type | ||
} |
@@ -30,4 +30,5 @@ /// <reference types="node" resolution-mode="require"/> | ||
}; | ||
export type Stats = import('node:fs').Stats; | ||
export type ErrnoException = import('./errors.js').ErrnoException; | ||
export type PackageConfig = import('./package-config.js').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/45f5c9b/lib/internal/modules/esm/resolve.js> | ||
// Last checked on: Nov 2, 2023. | ||
// <https://github.com/nodejs/node/blob/81a9a97/lib/internal/modules/esm/resolve.js> | ||
// Last checked on: Apr 29, 2024. | ||
/** | ||
* @typedef {import('node:fs').Stats} Stats | ||
* @typedef {import('./errors.js').ErrnoException} ErrnoException | ||
* @typedef {import('./package-config.js').PackageConfig} PackageConfig | ||
* @typedef {import('./package-json-reader.js').PackageConfig} PackageConfig | ||
*/ | ||
import assert from 'node:assert' | ||
import {Stats, statSync, realpathSync} from 'node:fs' | ||
import {statSync, realpathSync} from 'node:fs' | ||
import process from 'node:process' | ||
@@ -18,4 +19,3 @@ import {URL, fileURLToPath, pathToFileURL} from 'node:url' | ||
import {codes} from './errors.js' | ||
import {getPackageScopeConfig} from './package-config.js' | ||
import packageJsonReader from './package-json-reader.js' | ||
import {getPackageScopeConfig, read} from './package-json-reader.js' | ||
import {getConditionsSet} from './utils.js' | ||
@@ -33,3 +33,4 @@ | ||
ERR_PACKAGE_PATH_NOT_EXPORTED, | ||
ERR_UNSUPPORTED_DIR_IMPORT | ||
ERR_UNSUPPORTED_DIR_IMPORT, | ||
ERR_UNSUPPORTED_RESOLVE_REQUEST | ||
} = codes | ||
@@ -45,3 +46,3 @@ | ||
const patternRegEx = /\*/g | ||
const encodedSepRegEx = /%2f|%5c/i | ||
const encodedSeparatorRegEx = /%2f|%5c/i | ||
/** @type {Set<string>} */ | ||
@@ -110,8 +111,8 @@ const emittedPackageWarnings = new Set() | ||
const urlPath = fileURLToPath(url.href) | ||
const pkgPath = fileURLToPath(new URL('.', packageJsonUrl)) | ||
const packagePath = fileURLToPath(new URL('.', packageJsonUrl)) | ||
const basePath = fileURLToPath(base) | ||
if (!main) { | ||
process.emitWarning( | ||
`No "main" or "exports" field defined in the package.json for ${pkgPath} resolving the main entry point "${urlPath.slice( | ||
pkgPath.length | ||
`No "main" or "exports" field defined in the package.json for ${packagePath} resolving the main entry point "${urlPath.slice( | ||
packagePath.length | ||
)}", imported from ${basePath}.\nDefault "index" lookups for the main are deprecated for ES modules.`, | ||
@@ -121,7 +122,7 @@ 'DeprecationWarning', | ||
) | ||
} else if (path.resolve(pkgPath, main) !== urlPath) { | ||
} else if (path.resolve(packagePath, main) !== urlPath) { | ||
process.emitWarning( | ||
`Package ${pkgPath} has a "main" field set to "${main}", ` + | ||
`Package ${packagePath} has a "main" field set to "${main}", ` + | ||
`excluding the full filename and extension to the resolved file at "${urlPath.slice( | ||
pkgPath.length | ||
packagePath.length | ||
)}", imported from ${basePath}.\n Automatic extension resolution of the "main" field is ` + | ||
@@ -137,3 +138,3 @@ 'deprecated for ES modules.', | ||
* @param {string} path | ||
* @returns {Stats} | ||
* @returns {Stats | undefined} | ||
*/ | ||
@@ -145,3 +146,6 @@ function tryStatSync(path) { | ||
} catch { | ||
return new Stats() | ||
// Note: in Node code this returns `new Stats`, | ||
// but in Node 22 that’s marked as a deprecated internal API. | ||
// Which, well, we kinda are, but still to prevent that warning, | ||
// just yield `undefined`. | ||
} | ||
@@ -237,3 +241,3 @@ } | ||
function finalizeResolution(resolved, base, preserveSymlinks) { | ||
if (encodedSepRegEx.exec(resolved.pathname) !== null) { | ||
if (encodedSeparatorRegEx.exec(resolved.pathname) !== null) { | ||
throw new ERR_INVALID_MODULE_SPECIFIER( | ||
@@ -262,3 +266,3 @@ resolved.pathname, | ||
if (stats.isDirectory()) { | ||
if (stats && stats.isDirectory()) { | ||
const error = new ERR_UNSUPPORTED_DIR_IMPORT(filePath, fileURLToPath(base)) | ||
@@ -270,3 +274,3 @@ // @ts-expect-error Add this for `import.meta.resolve`. | ||
if (!stats.isFile()) { | ||
if (!stats || !stats.isFile()) { | ||
const error = new ERR_MODULE_NOT_FOUND( | ||
@@ -657,9 +661,9 @@ filePath || resolved.pathname, | ||
let i = 0 | ||
let j = -1 | ||
while (++j < keys.length) { | ||
const key = keys[j] | ||
const curIsConditionalSugar = key === '' || key[0] !== '.' | ||
let keyIndex = -1 | ||
while (++keyIndex < keys.length) { | ||
const key = keys[keyIndex] | ||
const currentIsConditionalSugar = key === '' || key[0] !== '.' | ||
if (i++ === 0) { | ||
isConditionalSugar = curIsConditionalSugar | ||
} else if (isConditionalSugar !== curIsConditionalSugar) { | ||
isConditionalSugar = currentIsConditionalSugar | ||
} else if (isConditionalSugar !== currentIsConditionalSugar) { | ||
throw new ERR_INVALID_PACKAGE_CONFIG( | ||
@@ -923,6 +927,2 @@ fileURLToPath(packageJsonUrl), | ||
// Note: In Node.js, `getPackageType` is here. | ||
// To prevent a circular dependency, we move it to | ||
// `resolve-get-package-type.js`. | ||
/** | ||
@@ -1015,3 +1015,3 @@ * @param {string} specifier | ||
const stat = tryStatSync(packageJsonPath.slice(0, -13)) | ||
if (!stat.isDirectory()) { | ||
if (!stat || !stat.isDirectory()) { | ||
lastPath = packageJsonPath | ||
@@ -1029,6 +1029,3 @@ packageJsonUrl = new URL( | ||
// Package match. | ||
const packageConfig = packageJsonReader.read(packageJsonPath, { | ||
base, | ||
specifier | ||
}) | ||
const packageConfig = read(packageJsonPath, {base, specifier}) | ||
if (packageConfig.exports !== undefined && packageConfig.exports !== null) { | ||
@@ -1099,4 +1096,7 @@ return packageExportsResolve( | ||
export function moduleResolve(specifier, base, conditions, preserveSymlinks) { | ||
// Note: The Node code supports `base` as a string (in this internal API) too, | ||
// we don’t. | ||
const protocol = base.protocol | ||
const isRemote = protocol === 'http:' || protocol === 'https:' | ||
const isData = protocol === 'data:' | ||
const isRemote = isData || protocol === 'http:' || protocol === 'https:' | ||
// Order swapped from spec for minor perf gain. | ||
@@ -1108,4 +1108,10 @@ // Ok since relative URLs cannot parse as URLs. | ||
if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) { | ||
resolved = new URL(specifier, base) | ||
} else if (!isRemote && specifier[0] === '#') { | ||
try { | ||
resolved = new URL(specifier, base) | ||
} catch (error_) { | ||
const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base) | ||
error.cause = error_ | ||
throw error | ||
} | ||
} else if (protocol === 'file:' && specifier[0] === '#') { | ||
resolved = packageImportsResolve(specifier, base, conditions) | ||
@@ -1115,6 +1121,11 @@ } else { | ||
resolved = new URL(specifier) | ||
} catch { | ||
if (!isRemote) { | ||
resolved = packageResolve(specifier, base, conditions) | ||
} catch (error_) { | ||
// Note: actual code uses `canBeRequiredWithoutScheme`. | ||
if (isRemote && !builtinModules.includes(specifier)) { | ||
const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base) | ||
error.cause = error_ | ||
throw error | ||
} | ||
resolved = packageResolve(specifier, base, conditions) | ||
} | ||
@@ -1252,2 +1263,5 @@ } | ||
let parsed | ||
/** @type {string | undefined} */ | ||
let protocol | ||
try { | ||
@@ -1259,3 +1273,3 @@ parsed = shouldBeTreatedAsRelativeOrAbsolutePath(specifier) | ||
// Avoid accessing the `protocol` property due to the lazy getters. | ||
const protocol = parsed.protocol | ||
protocol = parsed.protocol | ||
@@ -1281,2 +1295,11 @@ if (protocol === 'data:') { | ||
// This must come after checkIfDisallowedImport | ||
if (protocol === undefined && parsed) { | ||
protocol = parsed.protocol | ||
} | ||
if (protocol === 'node:') { | ||
return {url: specifier} | ||
} | ||
// This must come after checkIfDisallowedImport | ||
if (parsed && parsed.protocol === 'node:') return {url: specifier} | ||
@@ -1283,0 +1306,0 @@ |
// Manually “tree shaken” from: | ||
// <https://github.com/nodejs/node/blob/45f5c9b/lib/internal/modules/esm/utils.js> | ||
// Last checked on: Nov 2, 2023. | ||
// <https://github.com/nodejs/node/blob/81a9a97/lib/internal/modules/esm/utils.js> | ||
// Last checked on: Apr 29, 2024. | ||
@@ -5,0 +5,0 @@ import {codes} from './errors.js' |
{ | ||
"name": "@dual-bundle/import-meta-resolve", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "A fork of `import-meta-resolve` with commonjs + ESM support at the same time, AKA dual package.", | ||
@@ -46,11 +46,12 @@ "license": "MIT", | ||
"@types/semver": "^7.0.0", | ||
"c8": "^8.0.0", | ||
"esbuild": "^0.20.1", | ||
"c8": "^9.0.0", | ||
"esbuild": "^0.21.1", | ||
"f-ck": "^2.0.0", | ||
"prettier": "^3.0.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"remark-cli": "^12.0.0", | ||
"remark-preset-wooorm": "^10.0.0", | ||
"semver": "^7.0.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^5.0.0", | ||
"xo": "^0.56.0" | ||
"xo": "^0.58.0" | ||
}, | ||
@@ -57,0 +58,0 @@ "scripts": { |
146
readme.md
@@ -1,2 +0,2 @@ | ||
# [**@dual-bundle/import-meta-resolve**](https://github.com/dual-bundle/import-meta-resolve) | ||
# `@dual-bundle/import-meta-resolve` | ||
@@ -31,16 +31,16 @@ A fork of [`import-meta-resolve`](https://github.com/wooorm/import-meta-resolve) | ||
* [What is this?](#what-is-this) | ||
* [When to use this?](#when-to-use-this) | ||
* [Install](#install) | ||
* [Use](#use) | ||
* [API](#api) | ||
* [`resolve(specifier, parent)`](#resolvespecifier-parent) | ||
* [`moduleResolve(specifier, parent, conditions, preserveSymlinks)`](#moduleresolvespecifier-parent-conditions-preservesymlinks) | ||
* [`ErrnoException`](#errnoexception) | ||
* [Algorithm](#algorithm) | ||
* [Differences to Node](#differences-to-node) | ||
* [Types](#types) | ||
* [Compatibility](#compatibility) | ||
* [Contribute](#contribute) | ||
* [License](#license) | ||
* [What is this?](#what-is-this) | ||
* [When to use this?](#when-to-use-this) | ||
* [Install](#install) | ||
* [Use](#use) | ||
* [API](#api) | ||
* [`resolve(specifier, parent)`](#resolvespecifier-parent) | ||
* [`moduleResolve(specifier, parent, conditions, preserveSymlinks)`](#moduleresolvespecifier-parent-conditions-preservesymlinks) | ||
* [`ErrnoException`](#errnoexception) | ||
* [Algorithm](#algorithm) | ||
* [Differences to Node](#differences-to-node) | ||
* [Types](#types) | ||
* [Compatibility](#compatibility) | ||
* [Contribute](#contribute) | ||
* [License](#license) | ||
@@ -106,8 +106,8 @@ ## What is this? | ||
* `specifier` (`string`) | ||
— the module specifier to resolve relative to parent | ||
(`/example.js`, `./example.js`, `../example.js`, `some-package`, `fs`, etc) | ||
* `parent` (`string`, example: `import.meta.url`) | ||
— the absolute parent module URL to resolve from; you must pass | ||
`import.meta.url` or something else | ||
* `specifier` (`string`) | ||
— the module specifier to resolve relative to parent | ||
(`/example.js`, `./example.js`, `../example.js`, `some-package`, `fs`, etc) | ||
* `parent` (`string`, example: `import.meta.url`) | ||
— the absolute parent module URL to resolve from; you must pass | ||
`import.meta.url` or something else | ||
@@ -129,10 +129,10 @@ ###### Returns | ||
* `specifier` (`string`) | ||
— `/example.js`, `./example.js`, `../example.js`, `some-package`, `fs`, etc | ||
* `parent` (`URL`, example: `import.meta.url`) | ||
— full URL (to a file) that `specifier` is resolved relative from | ||
* `conditions` (`Set<string>`, default: `new Set(['node', 'import'])`) | ||
— conditions | ||
* `preserveSymlinks` (`boolean`, default: `false`) | ||
— keep symlinks instead of resolving them | ||
* `specifier` (`string`) | ||
— `/example.js`, `./example.js`, `../example.js`, `some-package`, `fs`, etc | ||
* `parent` (`URL`, example: `import.meta.url`) | ||
— full URL (to a file) that `specifier` is resolved relative from | ||
* `conditions` (`Set<string>`, default: `new Set(['node', 'import'])`) | ||
— conditions | ||
* `preserveSymlinks` (`boolean`, default: `false`) | ||
— keep symlinks instead of resolving them | ||
@@ -165,26 +165,26 @@ ###### Returns | ||
* `'ERR_INVALID_MODULE_SPECIFIER'` | ||
— when `specifier` is invalid (example: `'#'`) | ||
* `'ERR_INVALID_PACKAGE_CONFIG'` | ||
— when a `package.json` is invalid (example: invalid JSON) | ||
* `'ERR_INVALID_PACKAGE_TARGET'` | ||
— when a `package.json` `exports` or `imports` is invalid (example: when it | ||
does not start with `'./'`) | ||
* `'ERR_MODULE_NOT_FOUND'` | ||
— when `specifier` cannot be found in `parent` (example: `'some-missing-package'`) | ||
* `'ERR_NETWORK_IMPORT_DISALLOWED'` | ||
— thrown when trying to resolve a local file or builtin from a remote file | ||
(`node:fs` relative to `'https://example.com'`) | ||
* `'ERR_PACKAGE_IMPORT_NOT_DEFINED'` | ||
— when a local import is not defined in an import map (example: `'#local'` | ||
when not defined) | ||
* `'ERR_PACKAGE_PATH_NOT_EXPORTED'` | ||
— when an export is not defined in an export map (example: `'tape/index.js'`, | ||
which is not in its export map) | ||
* `'ERR_UNSUPPORTED_DIR_IMPORT'` | ||
— when attempting to import a directory (example: `'./lib/'`) | ||
* `'ERR_UNKNOWN_FILE_EXTENSION'` | ||
— when somehow reading a file that has an unexpected extensions (`'./readme.md'`) | ||
* `'ERR_INVALID_ARG_VALUE'` | ||
— when `conditions` is incorrect | ||
* `'ERR_INVALID_MODULE_SPECIFIER'` | ||
— when `specifier` is invalid (example: `'#'`) | ||
* `'ERR_INVALID_PACKAGE_CONFIG'` | ||
— when a `package.json` is invalid (example: invalid JSON) | ||
* `'ERR_INVALID_PACKAGE_TARGET'` | ||
— when a `package.json` `exports` or `imports` is invalid (example: when it | ||
does not start with `'./'`) | ||
* `'ERR_MODULE_NOT_FOUND'` | ||
— when `specifier` cannot be found in `parent` (example: `'some-missing-package'`) | ||
* `'ERR_NETWORK_IMPORT_DISALLOWED'` | ||
— thrown when trying to resolve a local file or builtin from a remote file | ||
(`node:fs` relative to `'https://example.com'`) | ||
* `'ERR_PACKAGE_IMPORT_NOT_DEFINED'` | ||
— when a local import is not defined in an import map (example: `'#local'` | ||
when not defined) | ||
* `'ERR_PACKAGE_PATH_NOT_EXPORTED'` | ||
— when an export is not defined in an export map (example: `'tape/index.js'`, | ||
which is not in its export map) | ||
* `'ERR_UNSUPPORTED_DIR_IMPORT'` | ||
— when attempting to import a directory (example: `'./lib/'`) | ||
* `'ERR_UNKNOWN_FILE_EXTENSION'` | ||
— when somehow reading a file that has an unexpected extensions (`'./readme.md'`) | ||
* `'ERR_INVALID_ARG_VALUE'` | ||
— when `conditions` is incorrect | ||
@@ -202,22 +202,22 @@ ## Algorithm | ||
* `parent` defaulting to `import.meta.url` cannot be ponyfilled: you have to | ||
explicitly pass it | ||
* no support for loaders (that would mean implementing all of loaders) | ||
* no support for CLI flags: | ||
`--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 | ||
* no attempt is made to add a suggestion based on how things used to work in | ||
CJS before to not-found errors | ||
* prototypal methods are not guarded: Node protects for example `String#slice` | ||
or so from being tampered with, whereas this doesn’t | ||
* `parent` defaulting to `import.meta.url` cannot be ponyfilled: you have to | ||
explicitly pass it | ||
* no support for loaders (that would mean implementing all of loaders) | ||
* no support for CLI flags: | ||
`--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 | ||
* no attempt is made to add a suggestion based on how things used to work in | ||
CJS before to not-found errors | ||
* prototypal methods are not guarded: Node protects for example `String#slice` | ||
or so from being tampered with, whereas this doesn’t | ||
@@ -224,0 +224,0 @@ ## Types |
Sorry, the diff of this file is not supported yet
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
125577
12
16
3494