|
| 1 | +import { BuildTask, BundleUserConfig, Context, DeclarationUserConfig } from '../types.js'; |
| 2 | +import { formatEntry, getTransformDefaultOutputDir } from '../helpers/getTaskIO.js'; |
| 3 | +import getDefaultDefineValues from '../helpers/getDefaultDefineValues.js'; |
| 4 | +import { stringifyObject } from '../utils.js'; |
| 5 | +import { merge, mergeWith, omit } from 'es-toolkit/object'; |
| 6 | +import path from 'node:path'; |
| 7 | +import { groupBy } from 'es-toolkit'; |
| 8 | + |
| 9 | +const mergeDefaults: typeof merge = (target, source) => { |
| 10 | + return mergeWith(target, source, (targetValue, sourceValue) => { |
| 11 | + return targetValue ?? sourceValue; |
| 12 | + }); |
| 13 | +}; |
| 14 | + |
| 15 | +const defaultMinifyFunction = (mode: string, command: string) => { |
| 16 | + return mode === 'production' && command === 'build'; |
| 17 | +}; |
| 18 | + |
| 19 | +const defaultBundleUserConfig: BundleUserConfig = { |
| 20 | + outputDir: 'dist', |
| 21 | + minify: { |
| 22 | + js: defaultMinifyFunction, |
| 23 | + css: defaultMinifyFunction, |
| 24 | + }, |
| 25 | + polyfill: 'usage', |
| 26 | + compileDependencies: false, |
| 27 | +}; |
| 28 | + |
| 29 | +const defaultDeclarationUserConfig = { |
| 30 | + outputMode: 'multi', |
| 31 | +} satisfies DeclarationUserConfig; |
| 32 | + |
| 33 | +export function initContextTasks(ctx: Context) { |
| 34 | + const tasks = ctx.getTaskConfig() as BuildTask[]; |
| 35 | + |
| 36 | + const { declaration: declarationTasks, buildable: buildableTasks } = groupBy(tasks, (task) => |
| 37 | + task.config.type === 'declaration' ? 'declaration' : 'buildable', |
| 38 | + ); |
| 39 | + |
| 40 | + // 1. init all tasks except declaration |
| 41 | + for (const buildTask of buildableTasks) { |
| 42 | + initTask(buildTask, ctx); |
| 43 | + } |
| 44 | + |
| 45 | + // 2. init declaration based on transform tasks |
| 46 | + for (const buildTask of declarationTasks ?? []) { |
| 47 | + initDeclarationTask(buildTask, ctx, buildableTasks); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +type InitTaskOptions = Pick<Context, 'userConfig' | 'rootDir' | 'command'>; |
| 52 | + |
| 53 | +function initSharedTask(buildTask: BuildTask, options: InitTaskOptions) { |
| 54 | + const { userConfig, command } = options; |
| 55 | + const { config } = buildTask; |
| 56 | + const { pkg } = config; |
| 57 | + |
| 58 | + config.entry = formatEntry(config.entry ?? pkg?.entry ?? userConfig.entry); |
| 59 | + config.alias ??= mergeDefaults({ ...pkg?.alias }, userConfig.alias ?? {}); |
| 60 | + // Configure define |
| 61 | + config.define = Object.assign( |
| 62 | + // Note: The define values in bundle mode will be defined (according to the `modes` value) |
| 63 | + // in generating rollup options. But when the command is test, we don't need to get the rollup options. |
| 64 | + // So in test, we assume the mode is 'development'. |
| 65 | + command === 'test' ? getDefaultDefineValues('development') : {}, |
| 66 | + stringifyObject(userConfig.define ?? {}), |
| 67 | + stringifyObject(pkg?.define ?? {}), |
| 68 | + stringifyObject(config.define ?? {}), |
| 69 | + ); |
| 70 | + |
| 71 | + config.sourcemap ??= pkg?.sourceMaps ?? userConfig.sourceMaps ?? command === 'start'; |
| 72 | + config.jsxRuntime ??= pkg?.jsxRuntime ?? userConfig.jsxRuntime; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * @internal export for test |
| 77 | + */ |
| 78 | +export function initTask(buildTask: BuildTask, options: InitTaskOptions) { |
| 79 | + const { userConfig, rootDir, command } = options; |
| 80 | + const { config, name: taskName } = buildTask; |
| 81 | + const { pkg } = config; |
| 82 | + |
| 83 | + initSharedTask(buildTask, options); |
| 84 | + |
| 85 | + const expectedMode = command === 'build' ? 'production' : 'development'; |
| 86 | + |
| 87 | + if (config.type === 'bundle') { |
| 88 | + const bundleConfig = userConfig.bundle ?? {}; |
| 89 | + config.modes ??= bundleConfig.modes ?? [expectedMode]; |
| 90 | + // TODO: 判断下这个东西是否真的有用 |
| 91 | + // Set outputDir to process.env for CI |
| 92 | + process.env.ICE_PKG_BUNDLE_OUTPUT_DIR = config.outputDir; |
| 93 | + const originMinifyConfig = pkg?.minify ?? bundleConfig.minify ?? defaultBundleUserConfig.minify ?? {}; |
| 94 | + let { jsMinify, cssMinify } = config; |
| 95 | + if (typeof originMinifyConfig === 'object') { |
| 96 | + jsMinify ??= getMinifyFunction(originMinifyConfig.js); |
| 97 | + cssMinify ??= getMinifyFunction(originMinifyConfig.css); |
| 98 | + } else { |
| 99 | + jsMinify ??= getMinifyFunction(originMinifyConfig); |
| 100 | + cssMinify ??= getMinifyFunction(originMinifyConfig); |
| 101 | + } |
| 102 | + |
| 103 | + config.jsMinify = jsMinify; |
| 104 | + config.cssMinify = cssMinify; |
| 105 | + |
| 106 | + config.outputDir ??= pkg?.outputDir ?? bundleConfig.outputDir ?? defaultBundleUserConfig.outputDir; |
| 107 | + |
| 108 | + if (pkg) { |
| 109 | + mergeDefaults(config, omit(pkg, ['id', 'pluginInfos', 'id', 'target', 'module', 'declaration', 'outputDir'])); |
| 110 | + } |
| 111 | + mergeDefaults(config, bundleConfig); |
| 112 | + mergeDefaults(config, defaultBundleUserConfig); |
| 113 | + } else if (config.type === 'transform') { |
| 114 | + config.modes ??= [expectedMode]; |
| 115 | + config.outputDir ??= pkg?.outputDir ?? getTransformDefaultOutputDir(rootDir, taskName, config); |
| 116 | + } else if (config.type === 'declaration') { |
| 117 | + // should run in initDeclarationTask |
| 118 | + } else { |
| 119 | + throw new Error('Invalid task type.'); |
| 120 | + } |
| 121 | + |
| 122 | + return buildTask; |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * 需要判断 entry 是否一致,对于不一致的 entry,理论上要生成多个 declaration task |
| 127 | + * @internal export for test |
| 128 | + */ |
| 129 | +export function initDeclarationTask(buildTask: BuildTask, options: InitTaskOptions, allTasks: BuildTask[]) { |
| 130 | + const { userConfig, rootDir } = options; |
| 131 | + const { config } = buildTask; |
| 132 | + const { declaration: declarationConfig } = userConfig; |
| 133 | + if (config.type !== 'declaration') { |
| 134 | + throw new Error('initDeclarationTask only allow declaration task'); |
| 135 | + } |
| 136 | + if (declarationConfig === false) { |
| 137 | + throw new Error('Cannot disable declaration when transform formats is not empty.'); |
| 138 | + } |
| 139 | + initSharedTask(buildTask, options); |
| 140 | + config.outputMode ??= |
| 141 | + declarationConfig === true |
| 142 | + ? defaultDeclarationUserConfig.outputMode |
| 143 | + : (declarationConfig.outputMode ?? defaultDeclarationUserConfig.outputMode); |
| 144 | + const allOutputDirs = allTasks |
| 145 | + .map((v) => { |
| 146 | + return v.config.type === 'transform' ? v.config.outputDir! : ''; |
| 147 | + }) |
| 148 | + .filter(Boolean); |
| 149 | + // 这个 output 仅仅用于生成正确的 .d.ts 的 alias,不做实际输出目录 |
| 150 | + config.outputDir = allOutputDirs[0]; |
| 151 | + if (config.outputMode === 'unique') { |
| 152 | + config.declarationOutputDirs = [path.resolve(rootDir, 'typings')]; |
| 153 | + } else { |
| 154 | + config.declarationOutputDirs = allOutputDirs; |
| 155 | + } |
| 156 | + |
| 157 | + return buildTask; |
| 158 | +} |
| 159 | + |
| 160 | +function getMinifyFunction(minify: boolean | ((mode: string, command: string) => unknown)) { |
| 161 | + switch (typeof minify) { |
| 162 | + case 'boolean': |
| 163 | + return () => minify; |
| 164 | + case 'function': |
| 165 | + return minify; |
| 166 | + } |
| 167 | + return defaultMinifyFunction; |
| 168 | +} |
0 commit comments