| 
 | 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 | +export 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.tsconfig !== undefined) {  | 
 | 96 | +      lib.source ||= {};  | 
 | 97 | +      lib.source.tsconfigPath = options.tsconfig;  | 
 | 98 | +    }  | 
 | 99 | +    const entry = parseEntryOption(options.entry);  | 
 | 100 | +    if (entry !== undefined) {  | 
 | 101 | +      lib.source ||= {};  | 
 | 102 | +      lib.source.entry = entry as RsbuildEntry;  | 
 | 103 | +    }  | 
 | 104 | +    const syntax = parseSyntaxOption(options.syntax);  | 
 | 105 | +    if (syntax !== undefined) lib.syntax = syntax;  | 
 | 106 | +    if (options.dts !== undefined) lib.dts = options.dts;  | 
 | 107 | +    if (options.autoExtension !== undefined)  | 
 | 108 | +      lib.autoExtension = options.autoExtension;  | 
 | 109 | +    if (options.autoExternal !== undefined)  | 
 | 110 | +      lib.autoExternal = options.autoExternal;  | 
 | 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.externals?.filter(Boolean) ?? [];  | 
 | 117 | +    if (externals.length > 0) output.externals = externals;  | 
 | 118 | +    if (options.distPath) {  | 
 | 119 | +      output.distPath = {  | 
 | 120 | +        ...(output.distPath ?? {}),  | 
 | 121 | +        root: options.distPath,  | 
 | 122 | +      };  | 
 | 123 | +    }  | 
 | 124 | +  }  | 
 | 125 | +};  | 
 | 126 | + | 
 | 127 | +export async function initConfig(options: CommonOptions): Promise<{  | 
 | 128 | +  config: RslibConfig;  | 
 | 129 | +  configFilePath: string;  | 
 | 130 | +  watchFiles: string[];  | 
 | 131 | +}> {  | 
 | 132 | +  const cwd = process.cwd();  | 
 | 133 | +  const root = options.root ? getAbsolutePath(cwd, options.root) : cwd;  | 
 | 134 | +  const envs = loadEnv({  | 
 | 135 | +    cwd: getEnvDir(root, options.envDir),  | 
 | 136 | +    mode: options.envMode,  | 
 | 137 | +  });  | 
 | 138 | + | 
 | 139 | +  onBeforeRestart(envs.cleanup);  | 
 | 140 | + | 
 | 141 | +  const { content: config, filePath: configFilePath } = await loadConfig({  | 
 | 142 | +    cwd: root,  | 
 | 143 | +    path: options.config,  | 
 | 144 | +    envMode: options.envMode,  | 
 | 145 | +    loader: options.configLoader,  | 
 | 146 | +  });  | 
 | 147 | + | 
 | 148 | +  config.source ||= {};  | 
 | 149 | +  config.source.define = {  | 
 | 150 | +    ...envs.publicVars,  | 
 | 151 | +    ...config.source.define,  | 
 | 152 | +  };  | 
 | 153 | + | 
 | 154 | +  applyCliOptions(config, options, root);  | 
 | 155 | + | 
 | 156 | +  logger.debug('Rslib config used to generate Rsbuild environments:');  | 
 | 157 | +  logger.debug(`\n${util.inspect(config, { depth: null, colors: true })}`);  | 
 | 158 | + | 
 | 159 | +  return {  | 
 | 160 | +    config,  | 
 | 161 | +    configFilePath,  | 
 | 162 | +    watchFiles: [configFilePath, ...envs.filePaths],  | 
 | 163 | +  };  | 
 | 164 | +}  | 
0 commit comments