|
| 1 | +import path from 'node:path' |
| 2 | +import fs from 'node:fs' |
| 3 | +import { ConfigExport } from '../types/config-export' |
| 4 | +import { pathToFileURL } from 'node:url' |
| 5 | +import { rolldown } from '../api/rolldown' |
| 6 | +import { RolldownOutputChunk } from '../types/rolldown-output' |
| 7 | + |
| 8 | +export async function loadTsConfig(configFile: string): Promise<ConfigExport> { |
| 9 | + const file = await bundleTsConfig(configFile) |
| 10 | + try { |
| 11 | + return (await import(pathToFileURL(file).href)).default |
| 12 | + } finally { |
| 13 | + fs.unlink(file, () => {}) // Ignore errors |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +async function bundleTsConfig(configFile: string): Promise<string> { |
| 18 | + const dirnameVarName = 'injected_original_dirname' |
| 19 | + const filenameVarName = 'injected_original_filename' |
| 20 | + const importMetaUrlVarName = 'injected_original_import_meta_url' |
| 21 | + |
| 22 | + const bundle = await rolldown({ |
| 23 | + input: configFile, |
| 24 | + platform: 'node', |
| 25 | + resolve: { |
| 26 | + mainFields: ['main'], |
| 27 | + }, |
| 28 | + define: { |
| 29 | + __dirname: dirnameVarName, |
| 30 | + __filename: filenameVarName, |
| 31 | + 'import.meta.url': importMetaUrlVarName, |
| 32 | + 'import.meta.dirname': dirnameVarName, |
| 33 | + 'import.meta.filename': filenameVarName, |
| 34 | + }, |
| 35 | + treeshake: false, |
| 36 | + external: [/^[\w@][^:]/], // external bare imports |
| 37 | + plugins: [ |
| 38 | + { |
| 39 | + name: 'inject-file-scope-variables', |
| 40 | + transform: { |
| 41 | + filter: { id: /\.[cm]?[jt]s$/ }, |
| 42 | + async handler(code, id) { |
| 43 | + const injectValues = |
| 44 | + `const ${dirnameVarName} = ${JSON.stringify(path.dirname(id))};` + |
| 45 | + `const ${filenameVarName} = ${JSON.stringify(id)};` + |
| 46 | + `const ${importMetaUrlVarName} = ${JSON.stringify( |
| 47 | + pathToFileURL(id).href, |
| 48 | + )};` |
| 49 | + return { code: injectValues + code, map: null } |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + ], |
| 54 | + }) |
| 55 | + const result = await bundle.write({ |
| 56 | + dir: path.dirname(configFile), |
| 57 | + format: 'esm', |
| 58 | + sourcemap: 'inline', |
| 59 | + entryFileNames: 'rolldown.config.[hash].js', |
| 60 | + }) |
| 61 | + |
| 62 | + return result.output.find( |
| 63 | + (chunk): chunk is RolldownOutputChunk => |
| 64 | + chunk.type === 'chunk' && chunk.isEntry, |
| 65 | + )!.fileName |
| 66 | +} |
| 67 | + |
| 68 | +const SUPPORTED_JS_CONFIG_FORMATS = ['.js', '.mjs', '.cjs'] |
| 69 | +const SUPPORTED_TS_CONFIG_FORMATS = ['.ts', '.mts', '.cts'] |
| 70 | +const SUPPORTED_CONFIG_FORMATS = [ |
| 71 | + ...SUPPORTED_JS_CONFIG_FORMATS, |
| 72 | + ...SUPPORTED_TS_CONFIG_FORMATS, |
| 73 | +] |
| 74 | + |
| 75 | +export async function loadConfig(configPath: string): Promise<ConfigExport> { |
| 76 | + const ext = path.extname(configPath) |
| 77 | + |
| 78 | + try { |
| 79 | + if ( |
| 80 | + SUPPORTED_JS_CONFIG_FORMATS.includes(ext) || |
| 81 | + (process.env.NODE_OPTIONS?.includes('--import=tsx') && |
| 82 | + SUPPORTED_TS_CONFIG_FORMATS.includes(ext)) |
| 83 | + ) { |
| 84 | + return (await import(pathToFileURL(configPath).href)).default |
| 85 | + } else if (SUPPORTED_TS_CONFIG_FORMATS.includes(ext)) { |
| 86 | + return await loadTsConfig(configPath) |
| 87 | + } else { |
| 88 | + throw new Error( |
| 89 | + `Unsupported config format. Expected: \`${SUPPORTED_CONFIG_FORMATS.join(',')}\` but got \`${ext}\``, |
| 90 | + ) |
| 91 | + } |
| 92 | + } catch (err) { |
| 93 | + throw new Error('Error happened while loading config.', { cause: err }) |
| 94 | + } |
| 95 | +} |
0 commit comments