|
| 1 | +import { builtinModules } from 'node:module' |
| 2 | + |
| 3 | +import type { ReadClosestTsConfigByPathValue } from './read-closest-ts-config-by-path' |
| 4 | +import type { RegexOption } from '../../types/common-options' |
| 5 | + |
| 6 | +import { getTypescriptImport } from './get-typescript-import' |
| 7 | +import { matches } from '../../utils/matches' |
| 8 | + |
| 9 | +export let computeCommonPredefinedGroups = ({ |
| 10 | + tsConfigOutput, |
| 11 | + filename, |
| 12 | + options, |
| 13 | + name, |
| 14 | +}: { |
| 15 | + options: { |
| 16 | + internalPattern: RegexOption[] |
| 17 | + environment: 'node' | 'bun' |
| 18 | + } |
| 19 | + tsConfigOutput: ReadClosestTsConfigByPathValue | null |
| 20 | + filename: string |
| 21 | + name: string |
| 22 | +}): string[] => { |
| 23 | + let matchesInternalPattern = (value: string): boolean | number => |
| 24 | + options.internalPattern.some(pattern => matches(value, pattern)) |
| 25 | + |
| 26 | + let internalExternalGroup = matchesInternalPattern(name) |
| 27 | + ? 'internal' |
| 28 | + : getInternalOrExternalGroup({ |
| 29 | + tsConfigOutput, |
| 30 | + filename, |
| 31 | + name, |
| 32 | + }) |
| 33 | + |
| 34 | + let predefinedGroups: string[] = [] |
| 35 | + |
| 36 | + if (isIndex(name)) { |
| 37 | + predefinedGroups.push('index') |
| 38 | + } |
| 39 | + |
| 40 | + if (isSibling(name)) { |
| 41 | + predefinedGroups.push('sibling') |
| 42 | + } |
| 43 | + |
| 44 | + if (isParent(name)) { |
| 45 | + predefinedGroups.push('parent') |
| 46 | + } |
| 47 | + |
| 48 | + if (internalExternalGroup === 'internal') { |
| 49 | + predefinedGroups.push('internal') |
| 50 | + } |
| 51 | + |
| 52 | + if (isCoreModule(name, options.environment)) { |
| 53 | + predefinedGroups.push('builtin') |
| 54 | + } |
| 55 | + |
| 56 | + if (internalExternalGroup === 'external') { |
| 57 | + predefinedGroups.push('external') |
| 58 | + } |
| 59 | + |
| 60 | + return predefinedGroups |
| 61 | +} |
| 62 | + |
| 63 | +let bunModules = new Set([ |
| 64 | + 'detect-libc', |
| 65 | + 'bun:sqlite', |
| 66 | + 'bun:test', |
| 67 | + 'bun:wrap', |
| 68 | + 'bun:ffi', |
| 69 | + 'bun:jsc', |
| 70 | + 'undici', |
| 71 | + 'bun', |
| 72 | + 'ws', |
| 73 | +]) |
| 74 | +let nodeBuiltinModules = new Set(builtinModules) |
| 75 | +let builtinPrefixOnlyModules = new Set(['node:sqlite', 'node:test', 'node:sea']) |
| 76 | +let isCoreModule = (value: string, environment: 'node' | 'bun'): boolean => { |
| 77 | + let valueToCheck = value.startsWith('node:') ? value.split('node:')[1] : value |
| 78 | + return ( |
| 79 | + (!!valueToCheck && nodeBuiltinModules.has(valueToCheck)) || |
| 80 | + builtinPrefixOnlyModules.has(value) || |
| 81 | + (environment === 'bun' ? bunModules.has(value) : false) |
| 82 | + ) |
| 83 | +} |
| 84 | + |
| 85 | +let isParent = (value: string): boolean => value.startsWith('..') |
| 86 | + |
| 87 | +let isSibling = (value: string): boolean => value.startsWith('./') |
| 88 | + |
| 89 | +let isIndex = (value: string): boolean => |
| 90 | + [ |
| 91 | + './index.d.js', |
| 92 | + './index.d.ts', |
| 93 | + './index.js', |
| 94 | + './index.ts', |
| 95 | + './index', |
| 96 | + './', |
| 97 | + '.', |
| 98 | + ].includes(value) |
| 99 | + |
| 100 | +let getInternalOrExternalGroup = ({ |
| 101 | + tsConfigOutput, |
| 102 | + filename, |
| 103 | + name, |
| 104 | +}: { |
| 105 | + tsConfigOutput: ReadClosestTsConfigByPathValue | null |
| 106 | + filename: string |
| 107 | + name: string |
| 108 | +}): 'internal' | 'external' | null => { |
| 109 | + let typescriptImport = getTypescriptImport() |
| 110 | + if (!typescriptImport) { |
| 111 | + return !name.startsWith('.') && !name.startsWith('/') ? 'external' : null |
| 112 | + } |
| 113 | + |
| 114 | + let isRelativeImport = typescriptImport.isExternalModuleNameRelative(name) |
| 115 | + if (isRelativeImport) { |
| 116 | + return null |
| 117 | + } |
| 118 | + if (!tsConfigOutput) { |
| 119 | + return 'external' |
| 120 | + } |
| 121 | + |
| 122 | + let resolution = typescriptImport.resolveModuleName( |
| 123 | + name, |
| 124 | + filename, |
| 125 | + tsConfigOutput.compilerOptions, |
| 126 | + typescriptImport.sys, |
| 127 | + tsConfigOutput.cache, |
| 128 | + ) |
| 129 | + // If the module can't be resolved, assume it is external. |
| 130 | + if (typeof resolution.resolvedModule?.isExternalLibraryImport !== 'boolean') { |
| 131 | + return 'external' |
| 132 | + } |
| 133 | + |
| 134 | + return resolution.resolvedModule.isExternalLibraryImport |
| 135 | + ? 'external' |
| 136 | + : 'internal' |
| 137 | +} |
0 commit comments