|
1 | 1 | import chalk from 'chalk'; |
2 | 2 | import { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync } from 'fs'; |
| 3 | +import { getTsconfig } from 'get-tsconfig'; |
3 | 4 | import { sync as globSync } from 'glob'; |
4 | | -import { join, resolve } from 'path'; |
| 5 | +import { dirname, join, resolve } from 'path'; |
5 | 6 | import { snapshotValidator as mysqlSnapshotValidator } from 'src/dialects/mysql/snapshot'; |
6 | 7 | import { snapshotValidator as singlestoreSnapshotValidator } from 'src/dialects/singlestore/snapshot'; |
7 | 8 | import { parse, pathToFileURL } from 'url'; |
@@ -134,6 +135,62 @@ export const prepareOutFolder = (out: string) => { |
134 | 135 | return { snapshots }; |
135 | 136 | }; |
136 | 137 |
|
| 138 | +const tsconfigAliasCache = new Map<string, Record<string, string> | undefined>(); |
| 139 | + |
| 140 | +const getAliasesForTsconfig = (baseDir: string): Record<string, string> | undefined => { |
| 141 | + const cached = tsconfigAliasCache.get(baseDir); |
| 142 | + if (cached !== undefined || tsconfigAliasCache.has(baseDir)) { |
| 143 | + return cached; |
| 144 | + } |
| 145 | + |
| 146 | + const tsconfig = getTsconfig(baseDir); |
| 147 | + const tsconfigPaths = tsconfig?.config?.compilerOptions?.paths as Record<string, string[]> | undefined; |
| 148 | + if (!tsconfigPaths || !tsconfig?.path) { |
| 149 | + tsconfigAliasCache.set(baseDir, undefined); |
| 150 | + return undefined; |
| 151 | + } |
| 152 | + |
| 153 | + const tsconfigBaseUrl = tsconfig.config.compilerOptions?.baseUrl ?? '.'; |
| 154 | + const tsconfigDir = dirname(tsconfig.path); |
| 155 | + |
| 156 | + const aliases = Object.fromEntries( |
| 157 | + Object.entries(tsconfigPaths).flatMap(([key, values]) => { |
| 158 | + const targets = (values ?? []).filter((value): value is string => Boolean(value)); |
| 159 | + if (targets.length === 0) return []; |
| 160 | + |
| 161 | + const hasWildcard = key.includes('*') || targets.some((target) => target.includes('*')); |
| 162 | + const supportsTrailingWildcard = key.endsWith('/*') && targets.every((target) => target.endsWith('/*')); |
| 163 | + if (hasWildcard && !supportsTrailingWildcard) { |
| 164 | + console.warn( |
| 165 | + chalk.yellow( |
| 166 | + `[drizzle-kit] Unsupported tsconfig "paths" mapping "${key}": [${ |
| 167 | + targets |
| 168 | + .map((target) => `"${target}"`) |
| 169 | + .join(', ') |
| 170 | + }]. Only trailing "/*" patterns are supported; this mapping will be ignored.`, |
| 171 | + ), |
| 172 | + ); |
| 173 | + return []; |
| 174 | + } |
| 175 | + |
| 176 | + const aliasKey = key.endsWith('/*') ? key.slice(0, -1) : key; |
| 177 | + const resolvedTargets = targets.map((target) => { |
| 178 | + if (supportsTrailingWildcard) { |
| 179 | + const targetPrefix = target.slice(0, -1); |
| 180 | + return resolve(tsconfigDir, tsconfigBaseUrl, targetPrefix); |
| 181 | + } |
| 182 | + return resolve(tsconfigDir, tsconfigBaseUrl, target); |
| 183 | + }); |
| 184 | + |
| 185 | + const selectedTarget = resolvedTargets.find((candidate) => existsSync(candidate)) ?? resolvedTargets[0]!; |
| 186 | + return [[aliasKey, selectedTarget]]; |
| 187 | + }), |
| 188 | + ); |
| 189 | + |
| 190 | + tsconfigAliasCache.set(baseDir, aliases); |
| 191 | + return aliases; |
| 192 | +}; |
| 193 | + |
137 | 194 | /** |
138 | 195 | * Reads all snapshot files and returns the IDs of leaf nodes (nodes that are |
139 | 196 | * not referenced as a parent by any other node). When generating a new migration, |
@@ -419,13 +476,16 @@ export const loadModule = async <T = unknown>( |
419 | 476 | const isTS = ext === '.ts' || ext === '.mts' || ext === '.cts'; |
420 | 477 |
|
421 | 478 | if (isTS) { |
422 | | - const jiti = require('jiti')(path.dirname(absoluteModulePath), { |
| 479 | + const baseDir = path.dirname(absoluteModulePath); |
| 480 | + const aliases = getAliasesForTsconfig(baseDir); |
| 481 | + // oxlint-disable-next-line consistent-type-imports |
| 482 | + const { createJiti } = require('jiti') as typeof import('jiti'); |
| 483 | + const jiti = createJiti(baseDir, { |
423 | 484 | interopDefault: true, |
424 | | - esmResolve: true, |
| 485 | + alias: aliases, |
425 | 486 | requireCache: false, |
426 | 487 | }); |
427 | | - const mod = await jiti.import(absoluteModulePath); |
428 | | - return mod; |
| 488 | + return jiti.import(absoluteModulePath); |
429 | 489 | } |
430 | 490 |
|
431 | 491 | const fileUrl = pathToFileURL(absoluteModulePath).href; |
|
0 commit comments