From 2c0a147641fb78f2455378caf88e48b9565e2477 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 25 Mar 2022 13:35:33 +0100 Subject: [PATCH] fix: inline import-meta-resolve --- README.md | 2 +- lib/import-meta-resolve/errors.js | 352 ++++++ lib/import-meta-resolve/get-format.js | 54 + lib/import-meta-resolve/index.js | 34 + .../package-json-reader.js | 42 + lib/import-meta-resolve/resolve.js | 1064 +++++++++++++++++ package.json | 1 - src/resolve.ts | 2 +- 8 files changed, 1548 insertions(+), 3 deletions(-) create mode 100644 lib/import-meta-resolve/errors.js create mode 100644 lib/import-meta-resolve/get-format.js create mode 100644 lib/import-meta-resolve/index.js create mode 100644 lib/import-meta-resolve/package-json-reader.js create mode 100644 lib/import-meta-resolve/resolve.js diff --git a/README.md b/README.md index beaaa4e..b3522cd 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ Several utilities to make ESM resolution easier: ### `resolve` Resolve a module by respecting [ECMAScript Resolver algorithm](https://nodejs.org/dist/latest-v14.x/docs/api/esm.html#esm_resolver_algorithm) -(internally using [wooorm/import-meta-resolve](https://github.com/wooorm/import-meta-resolve) that exposes Node.js implementation). +(based on experimental Node.js implementation extracted from [wooorm/import-meta-resolve](https://github.com/wooorm/import-meta-resolve)). Additionally supports resolving without extension and `/index` similar to CommonJS. diff --git a/lib/import-meta-resolve/errors.js b/lib/import-meta-resolve/errors.js new file mode 100644 index 0000000..cd581ed --- /dev/null +++ b/lib/import-meta-resolve/errors.js @@ -0,0 +1,352 @@ +// Manually “tree shaken” from: +// +import assert from 'assert' +// Needed for types. +// eslint-disable-next-line no-unused-vars +import { format, inspect } from 'util' + +const isWindows = process.platform === 'win32' + +const own = {}.hasOwnProperty + +export const codes = {} + +/** + * @typedef {(...args: unknown[]) => string} MessageFunction + */ + +/** @type {Map} */ +const messages = new Map() +const nodeInternalPrefix = '__node_internal_' +/** @type {number} */ +let userStackTraceLimit + +codes.ERR_INVALID_MODULE_SPECIFIER = createError( + 'ERR_INVALID_MODULE_SPECIFIER', + /** + * @param {string} request + * @param {string} reason + * @param {string} [base] + */ + (request, reason, base = undefined) => { + return `Invalid module "${request}" ${reason}${ + base ? ` imported from ${base}` : '' + }` + }, + TypeError +) + +codes.ERR_INVALID_PACKAGE_CONFIG = createError( + 'ERR_INVALID_PACKAGE_CONFIG', + /** + * @param {string} path + * @param {string} [base] + * @param {string} [message] + */ + (path, base, message) => { + return `Invalid package config ${path}${ + base ? ` while importing ${base}` : '' + }${message ? `. ${message}` : ''}` + }, + Error +) + +codes.ERR_INVALID_PACKAGE_TARGET = createError( + 'ERR_INVALID_PACKAGE_TARGET', + /** + * @param {string} pkgPath + * @param {string} key + * @param {unknown} target + * @param {boolean} [isImport=false] + * @param {string} [base] + */ + (pkgPath, key, target, isImport = false, base = undefined) => { + const relError = + typeof target === 'string' && + !isImport && + target.length > 0 && + !target.startsWith('./') + if (key === '.') { + assert(isImport === false) + return ( + `Invalid "exports" main target ${JSON.stringify(target)} defined ` + + `in the package config ${pkgPath}package.json${ + base ? ` imported from ${base}` : '' + }${relError ? '; targets must start with "./"' : ''}` + ) + } + + return `Invalid "${ + isImport ? 'imports' : 'exports' + }" target ${JSON.stringify( + target + )} defined for '${key}' in the package config ${pkgPath}package.json${ + base ? ` imported from ${base}` : '' + }${relError ? '; targets must start with "./"' : ''}` + }, + Error +) + +codes.ERR_MODULE_NOT_FOUND = createError( + 'ERR_MODULE_NOT_FOUND', + /** + * @param {string} path + * @param {string} base + * @param {string} [type] + */ + (path, base, type = 'package') => { + return `Cannot find ${type} '${path}' imported from ${base}` + }, + Error +) + +codes.ERR_PACKAGE_IMPORT_NOT_DEFINED = createError( + 'ERR_PACKAGE_IMPORT_NOT_DEFINED', + /** + * @param {string} specifier + * @param {string} packagePath + * @param {string} base + */ + (specifier, packagePath, base) => { + return `Package import specifier "${specifier}" is not defined${ + packagePath ? ` in package ${packagePath}package.json` : '' + } imported from ${base}` + }, + TypeError +) + +codes.ERR_PACKAGE_PATH_NOT_EXPORTED = createError( + 'ERR_PACKAGE_PATH_NOT_EXPORTED', + /** + * @param {string} pkgPath + * @param {string} subpath + * @param {string} [base] + */ + (pkgPath, subpath, base = undefined) => { + if (subpath === '.') { + return `No "exports" main defined in ${pkgPath}package.json${ + base ? ` imported from ${base}` : '' + }` + } + return `Package subpath '${subpath}' is not defined by "exports" in ${pkgPath}package.json${ + base ? ` imported from ${base}` : '' + }` + }, + Error +) + +codes.ERR_UNSUPPORTED_DIR_IMPORT = createError( + 'ERR_UNSUPPORTED_DIR_IMPORT', + "Directory import '%s' is not supported " + + 'resolving ES modules imported from %s', + Error +) + +codes.ERR_UNKNOWN_FILE_EXTENSION = createError( + 'ERR_UNKNOWN_FILE_EXTENSION', + 'Unknown file extension "%s" for %s', + TypeError +) + +codes.ERR_INVALID_ARG_VALUE = createError( + 'ERR_INVALID_ARG_VALUE', + /** + * @param {string} name + * @param {unknown} value + * @param {string} [reason='is invalid'] + */ + (name, value, reason = 'is invalid') => { + let inspected = inspect(value) + + if (inspected.length > 128) { + inspected = `${inspected.slice(0, 128)}...` + } + + const type = name.includes('.') ? 'property' : 'argument' + + return `The ${type} '${name}' ${reason}. Received ${inspected}` + }, + TypeError + // Note: extra classes have been shaken out. + // , RangeError +) + +codes.ERR_UNSUPPORTED_ESM_URL_SCHEME = createError( + 'ERR_UNSUPPORTED_ESM_URL_SCHEME', + /** + * @param {URL} url + */ + (url) => { + let message = + 'Only file and data URLs 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 +) + +/** + * Utility function for registering the error codes. Only used here. Exported + * *only* to allow for testing. + * @param {string} sym + * @param {MessageFunction|string} value + * @param {ErrorConstructor} def + * @returns {new (...args: unknown[]) => Error} + */ +function createError (sym, value, def) { + // Special case for SystemError that formats the error message differently + // The SystemErrors only have SystemError as their base classes. + messages.set(sym, value) + + return makeNodeErrorWithCode(def, sym) +} + +/** + * @param {ErrorConstructor} Base + * @param {string} key + * @returns {ErrorConstructor} + */ +function makeNodeErrorWithCode (Base, key) { + // @ts-expect-error It’s a Node error. + return NodeError + /** + * @param {unknown[]} args + */ + function NodeError (...args) { + const limit = Error.stackTraceLimit + if (isErrorStackTraceLimitWritable()) { Error.stackTraceLimit = 0 } + const error = new Base() + // Reset the limit and setting the name property. + if (isErrorStackTraceLimitWritable()) { Error.stackTraceLimit = limit } + const message = getMessage(key, args, error) + Object.defineProperty(error, 'message', { + value: message, + enumerable: false, + writable: true, + configurable: true + }) + Object.defineProperty(error, 'toString', { + /** @this {Error} */ + value () { + return `${this.name} [${key}]: ${this.message}` + }, + enumerable: false, + writable: true, + configurable: true + }) + addCodeToName(error, Base.name, key) + // @ts-expect-error It’s a Node error. + error.code = key + return error + } +} + +const addCodeToName = hideStackFrames( + /** + * @param {Error} error + * @param {string} name + * @param {string} code + * @returns {void} + */ + function (error, name, code) { + // Set the stack + error = captureLargerStackTrace(error) + // Add the error code to the name to include it in the stack trace. + error.name = `${name} [${code}]` + // Access the stack to generate the error message including the error code + // from the name. + error.stack // eslint-disable-line no-unused-expressions + // Reset the name to the actual name. + if (name === 'SystemError') { + Object.defineProperty(error, 'name', { + value: name, + enumerable: false, + writable: true, + configurable: true + }) + } else { + delete error.name + } + } +) + +/** + * @returns {boolean} + */ +function isErrorStackTraceLimitWritable () { + const desc = Object.getOwnPropertyDescriptor(Error, 'stackTraceLimit') + if (desc === undefined) { + return Object.isExtensible(Error) + } + + return own.call(desc, 'writable') ? desc.writable : desc.set !== undefined +} + +/** + * This function removes unnecessary frames from Node.js core errors. + * @template {(...args: unknown[]) => unknown} T + * @type {(fn: T) => T} + */ +function hideStackFrames (fn) { + // 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 captureLargerStackTrace = hideStackFrames( + /** + * @param {Error} error + * @returns {Error} + */ + function (error) { + const stackTraceLimitIsWritable = isErrorStackTraceLimitWritable() + if (stackTraceLimitIsWritable) { + userStackTraceLimit = Error.stackTraceLimit + Error.stackTraceLimit = Number.POSITIVE_INFINITY + } + + Error.captureStackTrace(error) + + // Reset the limit + if (stackTraceLimitIsWritable) { Error.stackTraceLimit = userStackTraceLimit } + + return error + } +) + +/** + * @param {string} key + * @param {unknown[]} args + * @param {Error} self + * @returns {string} + */ +function getMessage (key, args, self) { + const message = messages.get(key) + + if (typeof message === 'function') { + assert( + message.length <= args.length, // Default options do not count. + `Code: ${key}; The provided arguments length (${args.length}) does not ` + + `match the required ones (${message.length}).` + ) + return Reflect.apply(message, self, args) + } + + const expectedLength = (message.match(/%[dfijoOs]/g) || []).length + assert( + expectedLength === args.length, + `Code: ${key}; The provided arguments length (${args.length}) does not ` + + `match the required ones (${expectedLength}).` + ) + if (args.length === 0) { return message } + + args.unshift(message) + return Reflect.apply(format, null, args) +} diff --git a/lib/import-meta-resolve/get-format.js b/lib/import-meta-resolve/get-format.js new file mode 100644 index 0000000..8628d1d --- /dev/null +++ b/lib/import-meta-resolve/get-format.js @@ -0,0 +1,54 @@ +// Manually “tree shaken” from: +// +import path from 'path' +import { URL, fileURLToPath } from 'url' +import { getPackageType } from './resolve.js' +import { codes } from './errors.js' + +const { ERR_UNKNOWN_FILE_EXTENSION } = codes + +const extensionFormatMap = { + __proto__: null, + '.cjs': 'commonjs', + '.js': 'module', + '.mjs': 'module' +} + +/** + * @param {string} url + * @returns {{format: string|null}} + */ +export function defaultGetFormat (url) { + if (url.startsWith('node:')) { + return { format: 'builtin' } + } + + const parsed = new URL(url) + + if (parsed.protocol === 'data:') { + const { 1: mime } = /^([^/]+\/[^;,]+)[^,]*?(;base64)?,/.exec( + parsed.pathname + ) || [null, null] + const format = mime === 'text/javascript' ? 'module' : null + return { format } + } + + if (parsed.protocol === 'file:') { + const ext = path.extname(parsed.pathname) + /** @type {string} */ + let format + if (ext === '.js') { + format = getPackageType(parsed.href) === 'module' ? 'module' : 'commonjs' + } else { + format = extensionFormatMap[ext] + } + + if (!format) { + throw new ERR_UNKNOWN_FILE_EXTENSION(ext, fileURLToPath(url)) + } + + return { format: format || null } + } + + return { format: null } +} diff --git a/lib/import-meta-resolve/index.js b/lib/import-meta-resolve/index.js new file mode 100644 index 0000000..163754e --- /dev/null +++ b/lib/import-meta-resolve/index.js @@ -0,0 +1,34 @@ +import { moduleResolve, defaultResolve } from './resolve' + +export { moduleResolve } + +/** + * Provides a module-relative resolution function scoped to each module, + * returning the URL string. + * `import.meta.resolve` also accepts a second argument which is the parent + * module from which to resolve from. + * + * This function is asynchronous because the ES module resolver in Node.js is + * allowed to be asynchronous. + * + * @param {string} specifier The module specifier to resolve relative to parent. + * @param {string} parent The absolute parent module URL to resolve from. + * You should pass `import.meta.url` or something else + * @returns {Promise} + */ +// eslint-disable-next-line require-await +export async function resolve (specifier, parent) { + if (!parent) { + throw new Error( + 'Please pass `parent`: `import-meta-resolve` cannot ponyfill that' + ) + } + + try { + return defaultResolve(specifier, { parentURL: parent }).url + } catch (error) { + return error.code === 'ERR_UNSUPPORTED_DIR_IMPORT' + ? error.url + : Promise.reject(error) + } +} diff --git a/lib/import-meta-resolve/package-json-reader.js b/lib/import-meta-resolve/package-json-reader.js new file mode 100644 index 0000000..8709e54 --- /dev/null +++ b/lib/import-meta-resolve/package-json-reader.js @@ -0,0 +1,42 @@ +// Manually “tree shaken” from: +// +// Removed the native dependency. +// Also: no need to cache, we do that in resolve already. + +import fs from 'fs' +import path from 'path' + +const reader = { read } +export default reader + +/** + * @param {string} jsonPath + * @returns {{string: string}} + */ +function read (jsonPath) { + return find(path.dirname(jsonPath)) +} + +/** + * @param {string} dir + * @returns {{string: string}} + */ +function find (dir) { + try { + const string = fs.readFileSync( + path.toNamespacedPath(path.join(dir, 'package.json')), + 'utf8' + ) + return { string } + } catch (error) { + if (error.code === 'ENOENT') { + const parent = path.dirname(dir) + if (dir !== parent) { return find(parent) } + return { string: undefined } + // Throw all other errors. + /* c8 ignore next 4 */ + } + + throw error + } +} diff --git a/lib/import-meta-resolve/resolve.js b/lib/import-meta-resolve/resolve.js new file mode 100644 index 0000000..44734fc --- /dev/null +++ b/lib/import-meta-resolve/resolve.js @@ -0,0 +1,1064 @@ +// Manually “tree shaken” from: +// + +/** + * @typedef {'module'|'commonjs'|'none'} PackageType + * + * @typedef PackageConfig + * @property {string} pjsonPath + * @property {boolean} exists + * @property {string} main + * @property {string} name + * @property {PackageType} type + * @property {Object.} exports + * @property {Object.} imports + * + * @typedef ResolveObject + * @property {URL} resolved + * @property {boolean} exact + */ + +import { fileURLToPath, pathToFileURL } from 'url' +import { Stats, statSync, realpathSync } from 'fs' +import path from 'path' +import { builtinModules } from 'module' +import packageJsonReader from './package-json-reader.js' +import { defaultGetFormat } from './get-format.js' +import { codes } from './errors.js' + +const { + ERR_INVALID_MODULE_SPECIFIER, + ERR_INVALID_PACKAGE_CONFIG, + ERR_INVALID_PACKAGE_TARGET, + ERR_MODULE_NOT_FOUND, + ERR_PACKAGE_IMPORT_NOT_DEFINED, + ERR_PACKAGE_PATH_NOT_EXPORTED, + ERR_UNSUPPORTED_DIR_IMPORT, + ERR_UNSUPPORTED_ESM_URL_SCHEME, + ERR_INVALID_ARG_VALUE +} = codes + +const own = {}.hasOwnProperty + +const DEFAULT_CONDITIONS = Object.freeze(['node', 'import']) +const DEFAULT_CONDITIONS_SET = new Set(DEFAULT_CONDITIONS) + +const invalidSegmentRegEx = /(^|\\|\/)(\.\.?|node_modules)(\\|\/|$)/ +const patternRegEx = /\*/g +const encodedSepRegEx = /%2f|%2c/i +/** @type {Set} */ +const emittedPackageWarnings = new Set() +/** @type {Map} */ +const packageJsonCache = new Map() + +/** + * @param {string} match + * @param {URL} pjsonUrl + * @param {boolean} isExports + * @param {URL} base + * @returns {void} + */ +function emitFolderMapDeprecation (match, pjsonUrl, isExports, base) { + const pjsonPath = fileURLToPath(pjsonUrl) + + if (emittedPackageWarnings.has(pjsonPath + '|' + match)) { return } + emittedPackageWarnings.add(pjsonPath + '|' + match) + process.emitWarning( + `Use of deprecated folder mapping "${match}" in the ${ + isExports ? '"exports"' : '"imports"' + } field module resolution of the package at ${pjsonPath}${ + base ? ` imported from ${fileURLToPath(base)}` : '' + }.\n` + + `Update this package.json to use a subpath pattern like "${match}*".`, + 'DeprecationWarning', + 'DEP0148' + ) +} + +/** + * @param {URL} url + * @param {URL} packageJsonUrl + * @param {URL} base + * @param {unknown} [main] + * @returns {void} + */ +function emitLegacyIndexDeprecation (url, packageJsonUrl, base, main) { + const { format } = defaultGetFormat(url.href) + if (format !== 'module') { return } + const path = fileURLToPath(url.href) + const pkgPath = fileURLToPath(new URL('.', packageJsonUrl)) + const basePath = fileURLToPath(base) + 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.', + 'DeprecationWarning', + 'DEP0151' + ) + } else { + 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.`, + 'DeprecationWarning', + 'DEP0151' + ) + } +} + +/** + * @param {string[]} [conditions] + * @returns {Set} + */ +function getConditionsSet (conditions) { + if (conditions !== undefined && conditions !== DEFAULT_CONDITIONS) { + if (!Array.isArray(conditions)) { + throw new ERR_INVALID_ARG_VALUE( + 'conditions', + conditions, + 'expected an array' + ) + } + + return new Set(conditions) + } + + return DEFAULT_CONDITIONS_SET +} + +/** + * @param {string} path + * @returns {Stats} + */ +function tryStatSync (path) { + // Note: from Node 15 onwards we can use `throwIfNoEntry: false` instead. + try { + return statSync(path) + } catch { + return new Stats() + } +} + +/** + * @param {string} path + * @param {string|URL} specifier Note: `specifier` is actually optional, not base. + * @param {URL} [base] + * @returns {PackageConfig} + */ +function getPackageConfig (path, specifier, base) { + const existing = packageJsonCache.get(path) + if (existing !== undefined) { + return existing + } + + 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 + } + packageJsonCache.set(path, packageConfig) + return packageConfig + } + + /** @type {Object.} */ + let packageJson + try { + packageJson = JSON.parse(source) + } catch (error) { + throw new ERR_INVALID_PACKAGE_CONFIG( + path, + (base ? `"${specifier}" from ` : '') + fileURLToPath(base || specifier), + error.message + ) + } + + const { exports, imports, main, name, type } = packageJson + + /** @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 `Object.`. + exports, + // @ts-expect-error Assume `Object.`. + imports: imports && typeof imports === 'object' ? imports : undefined + } + packageJsonCache.set(path, packageConfig) + return packageConfig +} + +/** + * @param {URL|string} resolved + * @returns {PackageConfig} + */ +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 } + } + + const packageJsonPath = fileURLToPath(packageJsonUrl) + /** @type {PackageConfig} */ + const packageConfig = { + pjsonPath: packageJsonPath, + exists: false, + main: undefined, + name: undefined, + type: 'none', + exports: undefined, + imports: undefined + } + packageJsonCache.set(packageJsonPath, packageConfig) + return packageConfig +} + +/** + * Legacy CommonJS main resolution: + * 1. let M = pkg_url + (json main field) + * 2. TRY(M, M.js, M.json, M.node) + * 3. TRY(M/index.js, M/index.json, M/index.node) + * 4. TRY(pkg_url/index.js, pkg_url/index.json, pkg_url/index.node) + * 5. NOT_FOUND + * + * @param {URL} url + * @returns {boolean} + */ +function fileExists (url) { + return tryStatSync(fileURLToPath(url)).isFile() +} + +/** + * @param {URL} packageJsonUrl + * @param {PackageConfig} packageConfig + * @param {URL} base + * @returns {URL} + */ +function legacyMainResolve (packageJsonUrl, packageConfig, base) { + /** @type {URL} */ + let guess + if (packageConfig.main !== undefined) { + guess = new URL(`./${packageConfig.main}`, packageJsonUrl) + // Note: fs check redundances will be handled by Descriptor cache here. + if (fileExists(guess)) { return guess } + + const tries = [ + `./${packageConfig.main}.js`, + `./${packageConfig.main}.json`, + `./${packageConfig.main}.node`, + `./${packageConfig.main}/index.js`, + `./${packageConfig.main}/index.json`, + `./${packageConfig.main}/index.node` + ] + let i = -1 + + while (++i < tries.length) { + guess = new URL(tries[i], packageJsonUrl) + if (fileExists(guess)) { break } + guess = undefined + } + + if (guess) { + emitLegacyIndexDeprecation( + guess, + packageJsonUrl, + base, + packageConfig.main + ) + return guess + } + // Fallthrough. + } + + const tries = ['./index.js', './index.json', './index.node'] + let i = -1 + + while (++i < tries.length) { + guess = new URL(tries[i], packageJsonUrl) + if (fileExists(guess)) { break } + guess = undefined + } + + if (guess) { + emitLegacyIndexDeprecation(guess, packageJsonUrl, base, packageConfig.main) + return guess + } + + // Not found. + throw new ERR_MODULE_NOT_FOUND( + fileURLToPath(new URL('.', packageJsonUrl)), + fileURLToPath(base) + ) +} + +/** + * @param {URL} resolved + * @param {URL} base + * @returns {URL} + */ +function finalizeResolution (resolved, base) { + if (encodedSepRegEx.test(resolved.pathname)) { + throw new ERR_INVALID_MODULE_SPECIFIER( + resolved.pathname, + 'must not include encoded "/" or "\\" characters', + fileURLToPath(base) + ) + } + + const path = fileURLToPath(resolved) + + const stats = tryStatSync(path.endsWith('/') ? path.slice(-1) : path) + + if (stats.isDirectory()) { + const error = new ERR_UNSUPPORTED_DIR_IMPORT(path, fileURLToPath(base)) + // @ts-expect-error Add this for `import.meta.resolve`. + error.url = String(resolved) + throw error + } + + if (!stats.isFile()) { + throw new ERR_MODULE_NOT_FOUND( + path || resolved.pathname, + base && fileURLToPath(base), + 'module' + ) + } + + return resolved +} + +/** + * @param {string} specifier + * @param {URL?} packageJsonUrl + * @param {URL} base + * @returns {never} + */ +function throwImportNotDefined (specifier, packageJsonUrl, base) { + throw new ERR_PACKAGE_IMPORT_NOT_DEFINED( + specifier, + packageJsonUrl && fileURLToPath(new URL('.', packageJsonUrl)), + fileURLToPath(base) + ) +} + +/** + * @param {string} subpath + * @param {URL} packageJsonUrl + * @param {URL} base + * @returns {never} + */ +function throwExportsNotFound (subpath, packageJsonUrl, base) { + throw new ERR_PACKAGE_PATH_NOT_EXPORTED( + fileURLToPath(new URL('.', packageJsonUrl)), + subpath, + base && fileURLToPath(base) + ) +} + +/** + * @param {string} subpath + * @param {URL} packageJsonUrl + * @param {boolean} internal + * @param {URL} [base] + * @returns {never} + */ +function throwInvalidSubpath (subpath, packageJsonUrl, internal, base) { + const reason = `request is not a valid subpath for the "${ + internal ? 'imports' : 'exports' + }" resolution of ${fileURLToPath(packageJsonUrl)}` + + throw new ERR_INVALID_MODULE_SPECIFIER( + subpath, + reason, + base && fileURLToPath(base) + ) +} + +/** + * @param {string} subpath + * @param {unknown} target + * @param {URL} packageJsonUrl + * @param {boolean} internal + * @param {URL} [base] + * @returns {never} + */ +function throwInvalidPackageTarget ( + subpath, + target, + packageJsonUrl, + internal, + base +) { + target = + typeof target === 'object' && target !== null + ? JSON.stringify(target, null, '') + : `${target}` + + throw new ERR_INVALID_PACKAGE_TARGET( + fileURLToPath(new URL('.', packageJsonUrl)), + subpath, + target, + internal, + base && fileURLToPath(base) + ) +} + +/** + * @param {string} target + * @param {string} subpath + * @param {string} match + * @param {URL} packageJsonUrl + * @param {URL} base + * @param {boolean} pattern + * @param {boolean} internal + * @param {Set} conditions + * @returns {URL} + */ +function resolvePackageTargetString ( + target, + subpath, + match, + packageJsonUrl, + base, + pattern, + internal, + conditions +) { + if (subpath !== '' && !pattern && target[target.length - 1] !== '/') { throwInvalidPackageTarget(match, target, packageJsonUrl, internal, base) } + + if (!target.startsWith('./')) { + if (internal && !target.startsWith('../') && !target.startsWith('/')) { + let isURL = false + + try { + // eslint-disable-next-line no-new + new URL(target) + isURL = true + } catch {} + + if (!isURL) { + const exportTarget = pattern + ? target.replace(patternRegEx, subpath) + : target + subpath + + return packageResolve(exportTarget, packageJsonUrl, conditions) + } + } + + throwInvalidPackageTarget(match, target, packageJsonUrl, internal, base) + } + + if (invalidSegmentRegEx.test(target.slice(2))) { throwInvalidPackageTarget(match, target, packageJsonUrl, internal, base) } + + const resolved = new URL(target, packageJsonUrl) + const resolvedPath = resolved.pathname + const packagePath = new URL('.', packageJsonUrl).pathname + + if (!resolvedPath.startsWith(packagePath)) { throwInvalidPackageTarget(match, target, packageJsonUrl, internal, base) } + + if (subpath === '') { return resolved } + + if (invalidSegmentRegEx.test(subpath)) { throwInvalidSubpath(match + subpath, packageJsonUrl, internal, base) } + + if (pattern) { return new URL(resolved.href.replace(patternRegEx, subpath)) } + return new URL(subpath, resolved) +} + +/** + * @param {string} key + * @returns {boolean} + */ +function isArrayIndex (key) { + const keyNumber = Number(key) + if (`${keyNumber}` !== key) { return false } + return keyNumber >= 0 && keyNumber < 0xFFFF_FFFF +} + +/** + * @param {URL} packageJsonUrl + * @param {unknown} target + * @param {string} subpath + * @param {string} packageSubpath + * @param {URL} base + * @param {boolean} pattern + * @param {boolean} internal + * @param {Set} conditions + * @returns {URL} + */ +function resolvePackageTarget ( + packageJsonUrl, + target, + subpath, + packageSubpath, + base, + pattern, + internal, + conditions +) { + if (typeof target === 'string') { + return resolvePackageTargetString( + target, + subpath, + packageSubpath, + packageJsonUrl, + base, + pattern, + internal, + conditions + ) + } + + if (Array.isArray(target)) { + /** @type {unknown[]} */ + const targetList = target + if (targetList.length === 0) { return null } + + /** @type {Error} */ + let lastException + let i = -1 + + while (++i < targetList.length) { + const targetItem = targetList[i] + /** @type {URL} */ + let resolved + try { + resolved = resolvePackageTarget( + packageJsonUrl, + targetItem, + subpath, + packageSubpath, + base, + pattern, + internal, + conditions + ) + } catch (error) { + lastException = error + if (error.code === 'ERR_INVALID_PACKAGE_TARGET') { continue } + throw error + } + + if (resolved === undefined) { continue } + + if (resolved === null) { + lastException = null + continue + } + + return resolved + } + + if (lastException === undefined || lastException === null) { + // @ts-expect-error The diff between `undefined` and `null` seems to be + // intentional + return lastException + } + + throw lastException + } + + if (typeof target === 'object' && target !== null) { + const keys = Object.getOwnPropertyNames(target) + let i = -1 + + while (++i < keys.length) { + const key = keys[i] + if (isArrayIndex(key)) { + throw new ERR_INVALID_PACKAGE_CONFIG( + fileURLToPath(packageJsonUrl), + base, + '"exports" cannot contain numeric property keys.' + ) + } + } + + i = -1 + + while (++i < keys.length) { + const key = keys[i] + if (key === 'default' || (conditions && conditions.has(key))) { + /** @type {unknown} */ + const conditionalTarget = target[key] + const resolved = resolvePackageTarget( + packageJsonUrl, + conditionalTarget, + subpath, + packageSubpath, + base, + pattern, + internal, + conditions + ) + if (resolved === undefined) { continue } + return resolved + } + } + + return undefined + } + + if (target === null) { + return null + } + + throwInvalidPackageTarget( + packageSubpath, + target, + packageJsonUrl, + internal, + base + ) +} + +/** + * @param {unknown} exports + * @param {URL} packageJsonUrl + * @param {URL} base + * @returns {boolean} + */ +function isConditionalExportsMainSugar (exports, packageJsonUrl, base) { + if (typeof exports === 'string' || Array.isArray(exports)) { return true } + if (typeof exports !== 'object' || exports === null) { return false } + + const keys = Object.getOwnPropertyNames(exports) + let isConditionalSugar = false + let i = 0 + let j = -1 + while (++j < keys.length) { + const key = keys[j] + const curIsConditionalSugar = key === '' || key[0] !== '.' + if (i++ === 0) { + isConditionalSugar = curIsConditionalSugar + } else if (isConditionalSugar !== curIsConditionalSugar) { + throw new ERR_INVALID_PACKAGE_CONFIG( + fileURLToPath(packageJsonUrl), + base, + '"exports" cannot contain some keys starting with \'.\' and some not.' + + ' The exports object must either be an object of package subpath keys' + + ' or an object of main entry condition name keys only.' + ) + } + } + + return isConditionalSugar +} + +/** + * @param {URL} packageJsonUrl + * @param {string} packageSubpath + * @param {Object.} packageConfig + * @param {URL} base + * @param {Set} conditions + * @returns {ResolveObject} + */ +function packageExportsResolve ( + packageJsonUrl, + packageSubpath, + packageConfig, + base, + conditions +) { + let exports = packageConfig.exports + if (isConditionalExportsMainSugar(exports, packageJsonUrl, base)) { exports = { '.': exports } } + + if (own.call(exports, packageSubpath)) { + const target = exports[packageSubpath] + const resolved = resolvePackageTarget( + packageJsonUrl, + target, + '', + packageSubpath, + base, + false, + false, + conditions + ) + if (resolved === null || resolved === undefined) { throwExportsNotFound(packageSubpath, packageJsonUrl, base) } + return { resolved, exact: true } + } + + let bestMatch = '' + const keys = Object.getOwnPropertyNames(exports) + let i = -1 + + while (++i < keys.length) { + const key = keys[i] + if ( + key[key.length - 1] === '*' && + packageSubpath.startsWith(key.slice(0, -1)) && + packageSubpath.length >= key.length && + key.length > bestMatch.length + ) { + bestMatch = key + } else if ( + key[key.length - 1] === '/' && + packageSubpath.startsWith(key) && + key.length > bestMatch.length + ) { + bestMatch = key + } + } + + if (bestMatch) { + const target = exports[bestMatch] + const pattern = bestMatch[bestMatch.length - 1] === '*' + const subpath = packageSubpath.slice(bestMatch.length - (pattern ? 1 : 0)) + const resolved = resolvePackageTarget( + packageJsonUrl, + target, + subpath, + bestMatch, + base, + pattern, + false, + conditions + ) + if (resolved === null || resolved === undefined) { throwExportsNotFound(packageSubpath, packageJsonUrl, base) } + if (!pattern) { emitFolderMapDeprecation(bestMatch, packageJsonUrl, true, base) } + return { resolved, exact: pattern } + } + + throwExportsNotFound(packageSubpath, packageJsonUrl, base) +} + +/** + * @param {string} name + * @param {URL} base + * @param {Set} [conditions] + * @returns {ResolveObject} + */ +function packageImportsResolve (name, base, conditions) { + if (name === '#' || name.startsWith('#/')) { + const reason = 'is not a valid internal imports specifier name' + throw new ERR_INVALID_MODULE_SPECIFIER(name, reason, fileURLToPath(base)) + } + + /** @type {URL} */ + let packageJsonUrl + + const packageConfig = getPackageScopeConfig(base) + + if (packageConfig.exists) { + packageJsonUrl = pathToFileURL(packageConfig.pjsonPath) + const imports = packageConfig.imports + if (imports) { + if (own.call(imports, name)) { + const resolved = resolvePackageTarget( + packageJsonUrl, + imports[name], + '', + name, + base, + false, + true, + conditions + ) + if (resolved !== null) { return { resolved, exact: true } } + } else { + let bestMatch = '' + const keys = Object.getOwnPropertyNames(imports) + let i = -1 + + while (++i < keys.length) { + const key = keys[i] + + if ( + key[key.length - 1] === '*' && + name.startsWith(key.slice(0, -1)) && + name.length >= key.length && + key.length > bestMatch.length + ) { + bestMatch = key + } else if ( + key[key.length - 1] === '/' && + name.startsWith(key) && + key.length > bestMatch.length + ) { + bestMatch = key + } + } + + if (bestMatch) { + const target = imports[bestMatch] + const pattern = bestMatch[bestMatch.length - 1] === '*' + const subpath = name.slice(bestMatch.length - (pattern ? 1 : 0)) + const resolved = resolvePackageTarget( + packageJsonUrl, + target, + subpath, + bestMatch, + base, + pattern, + true, + conditions + ) + if (resolved !== null) { + if (!pattern) { emitFolderMapDeprecation(bestMatch, packageJsonUrl, false, base) } + return { resolved, exact: pattern } + } + } + } + } + } + + throwImportNotDefined(name, packageJsonUrl, base) +} + +/** + * @param {string} url + * @returns {PackageType} + */ +export function getPackageType (url) { + const packageConfig = getPackageScopeConfig(url) + return packageConfig.type +} + +/** + * @param {string} specifier + * @param {URL} base + */ +function parsePackageName (specifier, base) { + let separatorIndex = specifier.indexOf('/') + let validPackageName = true + let isScoped = false + if (specifier[0] === '@') { + isScoped = true + if (separatorIndex === -1 || specifier.length === 0) { + validPackageName = false + } else { + separatorIndex = specifier.indexOf('/', separatorIndex + 1) + } + } + + const packageName = + separatorIndex === -1 ? specifier : specifier.slice(0, separatorIndex) + + // Package name cannot have leading . and cannot have percent-encoding or + // separators. + let i = -1 + while (++i < packageName.length) { + if (packageName[i] === '%' || packageName[i] === '\\') { + validPackageName = false + break + } + } + + if (!validPackageName) { + throw new ERR_INVALID_MODULE_SPECIFIER( + specifier, + 'is not a valid package name', + fileURLToPath(base) + ) + } + + const packageSubpath = + '.' + (separatorIndex === -1 ? '' : specifier.slice(separatorIndex)) + + return { packageName, packageSubpath, isScoped } +} + +/** + * @param {string} specifier + * @param {URL} base + * @param {Set} conditions + * @returns {URL} + */ +function packageResolve (specifier, base, conditions) { + const { packageName, packageSubpath, isScoped } = parsePackageName( + specifier, + base + ) + + // ResolveSelf + const packageConfig = getPackageScopeConfig(base) + + // Can’t test. + /* c8 ignore next 16 */ + if (packageConfig.exists) { + const packageJsonUrl = pathToFileURL(packageConfig.pjsonPath) + if ( + packageConfig.name === packageName && + packageConfig.exports !== undefined && + packageConfig.exports !== null + ) { + return packageExportsResolve( + packageJsonUrl, + packageSubpath, + packageConfig, + base, + conditions + ).resolved + } + } + + let packageJsonUrl = new URL( + './node_modules/' + packageName + '/package.json', + base + ) + let packageJsonPath = fileURLToPath(packageJsonUrl) + /** @type {string} */ + let lastPath + do { + const stat = tryStatSync(packageJsonPath.slice(0, -13)) + if (!stat.isDirectory()) { + lastPath = packageJsonPath + packageJsonUrl = new URL( + (isScoped ? '../../../../node_modules/' : '../../../node_modules/') + + packageName + + '/package.json', + packageJsonUrl + ) + packageJsonPath = fileURLToPath(packageJsonUrl) + continue + } + + // Package match. + const packageConfig = getPackageConfig(packageJsonPath, specifier, base) + if (packageConfig.exports !== undefined && packageConfig.exports !== null) { + return packageExportsResolve( + packageJsonUrl, + packageSubpath, + packageConfig, + base, + conditions + ).resolved + } + if (packageSubpath === '.') { return legacyMainResolve(packageJsonUrl, packageConfig, base) } + return new URL(packageSubpath, packageJsonUrl) + // Cross-platform root check. + } while (packageJsonPath.length !== lastPath.length) + + throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base)) +} + +/** + * @param {string} specifier + * @returns {boolean} + */ +function isRelativeSpecifier (specifier) { + if (specifier[0] === '.') { + if (specifier.length === 1 || specifier[1] === '/') { return true } + if ( + specifier[1] === '.' && + (specifier.length === 2 || specifier[2] === '/') + ) { + return true + } + } + + return false +} + +/** + * @param {string} specifier + * @returns {boolean} + */ +function shouldBeTreatedAsRelativeOrAbsolutePath (specifier) { + if (specifier === '') { return false } + if (specifier[0] === '/') { return true } + return isRelativeSpecifier(specifier) +} + +/** + * The “Resolver Algorithm Specification” as detailed in the Node docs (which is + * sync and slightly lower-level than `resolve`). + * + * + * + * @param {string} specifier + * @param {URL} base + * @param {Set} [conditions] + * @returns {URL} + */ +export function moduleResolve (specifier, base, conditions) { + // Order swapped from spec for minor perf gain. + // Ok since relative URLs cannot parse as URLs. + /** @type {URL} */ + let resolved + + if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) { + resolved = new URL(specifier, base) + } else if (specifier[0] === '#') { + ;({ resolved } = packageImportsResolve(specifier, base, conditions)) + } else { + try { + resolved = new URL(specifier) + } catch { + resolved = packageResolve(specifier, base, conditions) + } + } + + return finalizeResolution(resolved, base) +} + +/** + * @param {string} specifier + * @param {{parentURL?: string, conditions?: string[]}} context + * @returns {{url: string}} + */ +export function defaultResolve (specifier, context = {}) { + const { parentURL } = context + /** @type {URL} */ + let parsed + + try { + parsed = new URL(specifier) + if (parsed.protocol === 'data:') { + return { url: specifier } + } + } catch {} + + if (parsed && parsed.protocol === 'node:') { return { url: specifier } } + if (parsed && parsed.protocol !== 'file:' && parsed.protocol !== 'data:') { throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(parsed) } + + if (builtinModules.includes(specifier)) { + return { url: 'node:' + specifier } + } + + if (parentURL.startsWith('data:')) { + // This is gonna blow up, we want the error + // eslint-disable-next-line no-new + new URL(specifier, parentURL) + } + + const conditions = getConditionsSet(context.conditions) + let url = moduleResolve(specifier, new URL(parentURL), conditions) + + const urlPath = fileURLToPath(url) + const real = realpathSync(urlPath) + const old = url + url = pathToFileURL(real + (urlPath.endsWith(path.sep) ? '/' : '')) + url.search = old.search + url.hash = old.hash + + return { url: `${url}` } +} diff --git a/package.json b/package.json index 6ac16cc..49492a1 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,6 @@ "test": "pnpm lint && vitest run" }, "dependencies": { - "import-meta-resolve": "^1.1.1", "pathe": "^0.2.0", "pkg-types": "^0.3.2" }, diff --git a/src/resolve.ts b/src/resolve.ts index ba8d878..52d662b 100644 --- a/src/resolve.ts +++ b/src/resolve.ts @@ -1,7 +1,7 @@ import { existsSync, realpathSync } from 'fs' import { pathToFileURL } from 'url' import { isAbsolute } from 'pathe' -import { moduleResolve } from 'import-meta-resolve' +import { moduleResolve } from '../lib/import-meta-resolve' import { fileURLToPath, normalizeid } from './utils' import { pcall, BUILTIN_MODULES } from './_utils'