|
| 1 | +import path from 'node:path'; |
| 2 | +import util from 'node:util'; |
| 3 | +import { loadEnv, type RsbuildEntry } from '@rsbuild/core'; |
| 4 | +import { loadConfig } from '../config'; |
| 5 | +import type { |
| 6 | + EcmaScriptVersion, |
| 7 | + RsbuildConfigOutputTarget, |
| 8 | + RslibConfig, |
| 9 | + Syntax, |
| 10 | +} from '../types'; |
| 11 | +import { getAbsolutePath } from '../utils/helper'; |
| 12 | +import { logger } from '../utils/logger'; |
| 13 | +import type { CommonOptions } from './commands'; |
| 14 | +import { onBeforeRestart } from './restart'; |
| 15 | + |
| 16 | +const getEnvDir = (cwd: string, envDir?: string) => { |
| 17 | + if (envDir) { |
| 18 | + return path.isAbsolute(envDir) ? envDir : path.resolve(cwd, envDir); |
| 19 | + } |
| 20 | + return cwd; |
| 21 | +}; |
| 22 | + |
| 23 | +export const parseEntryOption = ( |
| 24 | + entries?: string[], |
| 25 | +): Record<string, string> | undefined => { |
| 26 | + if (!entries || entries.length === 0) { |
| 27 | + return undefined; |
| 28 | + } |
| 29 | + |
| 30 | + const parsed: Record<string, string> = {}; |
| 31 | + let unnamedIndex = 0; |
| 32 | + |
| 33 | + for (const rawEntry of entries) { |
| 34 | + const value = rawEntry?.trim(); |
| 35 | + if (!value) { |
| 36 | + continue; |
| 37 | + } |
| 38 | + |
| 39 | + const equalIndex = value.indexOf('='); |
| 40 | + if (equalIndex > -1) { |
| 41 | + const name = value.slice(0, equalIndex).trim(); |
| 42 | + const entryPath = value.slice(equalIndex + 1).trim(); |
| 43 | + if (name && entryPath) { |
| 44 | + parsed[name] = entryPath; |
| 45 | + continue; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + unnamedIndex += 1; |
| 50 | + const key = unnamedIndex === 1 ? 'index' : `entry${unnamedIndex}`; |
| 51 | + parsed[key] = value; |
| 52 | + } |
| 53 | + |
| 54 | + return Object.keys(parsed).length === 0 ? undefined : parsed; |
| 55 | +}; |
| 56 | + |
| 57 | +export const parseSyntaxOption = (syntax?: string): Syntax | undefined => { |
| 58 | + if (!syntax) { |
| 59 | + return undefined; |
| 60 | + } |
| 61 | + |
| 62 | + const trimmed = syntax.trim(); |
| 63 | + if (!trimmed) { |
| 64 | + return undefined; |
| 65 | + } |
| 66 | + |
| 67 | + if (trimmed.startsWith('[')) { |
| 68 | + try { |
| 69 | + const parsed = JSON.parse(trimmed); |
| 70 | + if (Array.isArray(parsed)) { |
| 71 | + return parsed; |
| 72 | + } |
| 73 | + } catch (e) { |
| 74 | + const reason = e instanceof Error ? e.message : String(e); |
| 75 | + throw new Error( |
| 76 | + `Failed to parse --syntax option "${trimmed}" as JSON array: ${reason}`, |
| 77 | + ); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return trimmed as EcmaScriptVersion; |
| 82 | +}; |
| 83 | + |
| 84 | +const applyCliOptions = ( |
| 85 | + config: RslibConfig, |
| 86 | + options: CommonOptions, |
| 87 | + root: string, |
| 88 | +): void => { |
| 89 | + if (options.root) config.root = root; |
| 90 | + if (options.logLevel) config.logLevel = options.logLevel; |
| 91 | + |
| 92 | + for (const lib of config.lib) { |
| 93 | + if (options.format !== undefined) lib.format = options.format; |
| 94 | + if (options.bundle !== undefined) lib.bundle = options.bundle; |
| 95 | + if (options.dts !== undefined) lib.dts = options.dts; |
| 96 | + if (options.autoExtension !== undefined) |
| 97 | + lib.autoExtension = options.autoExtension; |
| 98 | + if (options.autoExternal !== undefined) |
| 99 | + lib.autoExternal = options.autoExternal; |
| 100 | + if (options.tsconfig !== undefined) { |
| 101 | + lib.source ||= {}; |
| 102 | + lib.source.tsconfigPath = options.tsconfig; |
| 103 | + } |
| 104 | + const entry = parseEntryOption(options.entry); |
| 105 | + if (entry !== undefined) { |
| 106 | + lib.source ||= {}; |
| 107 | + lib.source.entry = entry as RsbuildEntry; |
| 108 | + } |
| 109 | + const syntax = parseSyntaxOption(options.syntax); |
| 110 | + if (syntax !== undefined) lib.syntax = syntax; |
| 111 | + const output = lib.output ?? {}; |
| 112 | + if (options.target !== undefined) |
| 113 | + output.target = options.target as RsbuildConfigOutputTarget; |
| 114 | + if (options.minify !== undefined) output.minify = options.minify; |
| 115 | + if (options.clean !== undefined) output.cleanDistPath = options.clean; |
| 116 | + const externals = options.external?.filter(Boolean) ?? []; |
| 117 | + if (externals.length > 0) output.externals = externals; |
| 118 | + if (options.distPath) { |
| 119 | + output.distPath ??= {}; |
| 120 | + output.distPath.root = options.distPath; |
| 121 | + } |
| 122 | + } |
| 123 | +}; |
| 124 | + |
| 125 | +export async function initConfig(options: CommonOptions): Promise<{ |
| 126 | + config: RslibConfig; |
| 127 | + configFilePath: string; |
| 128 | + watchFiles: string[]; |
| 129 | +}> { |
| 130 | + const cwd = process.cwd(); |
| 131 | + const root = options.root ? getAbsolutePath(cwd, options.root) : cwd; |
| 132 | + const envs = loadEnv({ |
| 133 | + cwd: getEnvDir(root, options.envDir), |
| 134 | + mode: options.envMode, |
| 135 | + }); |
| 136 | + |
| 137 | + onBeforeRestart(envs.cleanup); |
| 138 | + |
| 139 | + const { content: config, filePath: configFilePath } = await loadConfig({ |
| 140 | + cwd: root, |
| 141 | + path: options.config, |
| 142 | + envMode: options.envMode, |
| 143 | + loader: options.configLoader, |
| 144 | + }); |
| 145 | + |
| 146 | + config.source ||= {}; |
| 147 | + config.source.define = { |
| 148 | + ...envs.publicVars, |
| 149 | + ...config.source.define, |
| 150 | + }; |
| 151 | + |
| 152 | + applyCliOptions(config, options, root); |
| 153 | + |
| 154 | + logger.debug('Rslib config used to generate Rsbuild environments:'); |
| 155 | + logger.debug(`\n${util.inspect(config, { depth: null, colors: true })}`); |
| 156 | + |
| 157 | + return { |
| 158 | + config, |
| 159 | + configFilePath, |
| 160 | + watchFiles: [configFilePath, ...envs.filePaths], |
| 161 | + }; |
| 162 | +} |
0 commit comments