|
| 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 { RsbuildConfigOutputTarget, RslibConfig } from '../types'; |
| 6 | +import { getAbsolutePath } from '../utils/helper'; |
| 7 | +import { logger } from '../utils/logger'; |
| 8 | +import type { BuildOptions, CommonOptions } from './commands'; |
| 9 | +import { onBeforeRestart } from './restart'; |
| 10 | + |
| 11 | +const getEnvDir = (cwd: string, envDir?: string) => { |
| 12 | + if (envDir) { |
| 13 | + return path.isAbsolute(envDir) ? envDir : path.resolve(cwd, envDir); |
| 14 | + } |
| 15 | + return cwd; |
| 16 | +}; |
| 17 | + |
| 18 | +export const parseEntryOption = ( |
| 19 | + entries?: string[], |
| 20 | +): Record<string, string> | undefined => { |
| 21 | + if (!entries?.length) return undefined; |
| 22 | + |
| 23 | + const entryList: { key: string; value: string; explicit: boolean }[] = []; |
| 24 | + |
| 25 | + for (const rawEntry of entries) { |
| 26 | + const value = rawEntry?.trim(); |
| 27 | + if (!value) continue; |
| 28 | + |
| 29 | + const equalIndex = value.indexOf('='); |
| 30 | + if (equalIndex > -1) { |
| 31 | + const name = value.slice(0, equalIndex).trim(); |
| 32 | + const entryPath = value.slice(equalIndex + 1).trim(); |
| 33 | + if (name && entryPath) { |
| 34 | + entryList.push({ key: name, value: entryPath, explicit: true }); |
| 35 | + continue; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + const basename = path.basename(value, path.extname(value)); |
| 40 | + entryList.push({ key: basename, value, explicit: false }); |
| 41 | + } |
| 42 | + |
| 43 | + const keyCount: Record<string, number> = {}; |
| 44 | + for (const { key, explicit } of entryList) { |
| 45 | + if (!explicit) keyCount[key] = (keyCount[key] ?? 0) + 1; |
| 46 | + } |
| 47 | + |
| 48 | + const keyIndex: Record<string, number> = {}; |
| 49 | + const parsed: Record<string, string> = {}; |
| 50 | + |
| 51 | + for (const { key, value, explicit } of entryList) { |
| 52 | + const needsIndex = !explicit && (keyCount[key] ?? 0) > 1; |
| 53 | + const finalKey = needsIndex ? `${key}${keyIndex[key] ?? 0}` : key; |
| 54 | + if (needsIndex) keyIndex[key] = (keyIndex[key] ?? 0) + 1; |
| 55 | + parsed[finalKey] = value; |
| 56 | + } |
| 57 | + |
| 58 | + return Object.keys(parsed).length ? parsed : undefined; |
| 59 | +}; |
| 60 | + |
| 61 | +export const applyCliOptions = ( |
| 62 | + config: RslibConfig, |
| 63 | + options: BuildOptions, |
| 64 | + root: string, |
| 65 | +): void => { |
| 66 | + if (options.root) config.root = root; |
| 67 | + if (options.logLevel) config.logLevel = options.logLevel; |
| 68 | + |
| 69 | + for (const lib of config.lib) { |
| 70 | + if (options.format !== undefined) lib.format = options.format; |
| 71 | + if (options.bundle !== undefined) lib.bundle = options.bundle; |
| 72 | + if (options.tsconfig !== undefined) { |
| 73 | + lib.source ||= {}; |
| 74 | + lib.source.tsconfigPath = options.tsconfig; |
| 75 | + } |
| 76 | + const entry = parseEntryOption(options.entry); |
| 77 | + if (entry !== undefined) { |
| 78 | + lib.source ||= {}; |
| 79 | + lib.source.entry = entry as RsbuildEntry; |
| 80 | + } |
| 81 | + const syntax = options.syntax; |
| 82 | + if (syntax !== undefined) lib.syntax = syntax; |
| 83 | + if (options.dts !== undefined) lib.dts = options.dts; |
| 84 | + if (options.autoExtension !== undefined) |
| 85 | + lib.autoExtension = options.autoExtension; |
| 86 | + if (options.autoExternal !== undefined) |
| 87 | + lib.autoExternal = options.autoExternal; |
| 88 | + const output = lib.output ?? {}; |
| 89 | + if (options.target !== undefined) |
| 90 | + output.target = options.target as RsbuildConfigOutputTarget; |
| 91 | + if (options.minify !== undefined) output.minify = options.minify; |
| 92 | + if (options.clean !== undefined) output.cleanDistPath = options.clean; |
| 93 | + const externals = options.externals?.filter(Boolean) ?? []; |
| 94 | + if (externals.length > 0) output.externals = externals; |
| 95 | + if (options.distPath) { |
| 96 | + output.distPath = { |
| 97 | + ...(typeof output.distPath === 'object' ? output.distPath : {}), |
| 98 | + root: options.distPath, |
| 99 | + }; |
| 100 | + } |
| 101 | + } |
| 102 | +}; |
| 103 | + |
| 104 | +export async function initConfig(options: CommonOptions): Promise<{ |
| 105 | + config: RslibConfig; |
| 106 | + configFilePath: string; |
| 107 | + watchFiles: string[]; |
| 108 | +}> { |
| 109 | + const cwd = process.cwd(); |
| 110 | + const root = options.root ? getAbsolutePath(cwd, options.root) : cwd; |
| 111 | + const envs = loadEnv({ |
| 112 | + cwd: getEnvDir(root, options.envDir), |
| 113 | + mode: options.envMode, |
| 114 | + }); |
| 115 | + |
| 116 | + onBeforeRestart(envs.cleanup); |
| 117 | + |
| 118 | + const { content: config, filePath: configFilePath } = await loadConfig({ |
| 119 | + cwd: root, |
| 120 | + path: options.config, |
| 121 | + envMode: options.envMode, |
| 122 | + loader: options.configLoader, |
| 123 | + }); |
| 124 | + |
| 125 | + config.source ||= {}; |
| 126 | + config.source.define = { |
| 127 | + ...envs.publicVars, |
| 128 | + ...config.source.define, |
| 129 | + }; |
| 130 | + |
| 131 | + applyCliOptions(config, options, root); |
| 132 | + |
| 133 | + logger.debug('Rslib config used to generate Rsbuild environments:'); |
| 134 | + logger.debug(`\n${util.inspect(config, { depth: null, colors: true })}`); |
| 135 | + |
| 136 | + return { |
| 137 | + config, |
| 138 | + configFilePath, |
| 139 | + watchFiles: [configFilePath, ...envs.filePaths], |
| 140 | + }; |
| 141 | +} |
0 commit comments