UNPKG

knip

Version:

Find unused files, dependencies and exports in your TypeScript and JavaScript projects

67 lines (66 loc) 2.94 kB
import parseArgs from 'minimist'; import { pluginArgsMap } from '../plugins.js'; import { compact } from '../util/array.js'; import { toBinary, toConfig, toDeferResolve, toDeferResolveEntry, toEntry } from '../util/input.js'; import { extractBinary } from '../util/modules.js'; import { resolve as fallbackResolve } from './fallback.js'; const isGlobLikeMatch = /(^!|[*+\\(|{^$])/; const isGlobLike = (value) => isGlobLikeMatch.test(value); const nodeLoadersArgs = { import: ['r', 'experimental-loader', 'require', 'loader', 'test-reporter'] }; export const resolve = (binary, _args, options) => { const { fromArgs, containingFilePath } = options; const [pluginName, pluginArgs] = pluginArgsMap.get(binary) ?? []; if (!pluginArgs) return fallbackResolve(binary, _args, options); const opts = pluginArgs; const args = typeof opts.args === 'function' ? opts.args(_args) : _args; const parsed = parseArgs(args, { string: [ ...(opts.nodeImportArgs ? ['import'] : []), ...(opts.config === true ? ['config'] : []), ...(opts.string ?? []), ], boolean: ['quiet', 'verbose', 'watch', ...(opts.boolean ?? [])], alias: { ...(opts.nodeImportArgs ? nodeLoadersArgs : {}), ...(opts.config === true ? { config: ['c'] } : {}), ...opts.alias, }, }); const positionals = []; if (opts.positional && parsed._[0]) { const id = parsed._[0]; if (isGlobLike(id)) positionals.push(toEntry(id)); else { if (id.includes('node_modules/.bin/')) positionals.push(toBinary(extractBinary(id))); else positionals.push(toDeferResolveEntry(id)); } } const mapToParsedKey = (id) => parsed[id]; const resolved = compact(opts.resolve ? opts.resolve.flatMap(mapToParsedKey) : []); const resolvedImports = opts.nodeImportArgs && parsed.import ? [parsed.import].flat() : []; const resolvedFromArgs = typeof opts.fromArgs === 'function' ? fromArgs(opts.fromArgs(parsed, args)) : Array.isArray(opts.fromArgs) ? fromArgs(opts.fromArgs.flatMap(mapToParsedKey)) : []; const config = opts.config === true ? ['config'] : opts.config || []; const mapToConfigPattern = (value) => { if (typeof value === 'string') return parsed[value] && pluginName ? [toConfig(pluginName, parsed[value], containingFilePath)] : []; const [id, fn] = value; return parsed[id] && pluginName ? [toConfig(pluginName, fn(parsed[id]), containingFilePath)] : []; }; const configFilePaths = config.flatMap(mapToConfigPattern); return [ toBinary(binary), ...positionals, ...resolved.map(toDeferResolve), ...resolvedImports.map(toDeferResolve), ...resolvedFromArgs, ...configFilePaths, ]; };