From c2c7ccbc760c18ac301f187654bec22ffe0fef25 Mon Sep 17 00:00:00 2001 From: XGHeaven Date: Mon, 9 Mar 2026 12:21:39 +0800 Subject: [PATCH] fix: dts error and compact bundle format --- .changeset/tender-mails-guess.md | 5 ++++ packages/pkg/src/core/init.ts | 18 +++++++++++-- packages/pkg/src/core/pkg.ts | 18 +++++-------- packages/pkg/src/helpers/dts.ts | 3 ++- packages/pkg/tests/core/init.test.ts | 39 ++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 15 deletions(-) create mode 100644 .changeset/tender-mails-guess.md diff --git a/.changeset/tender-mails-guess.md b/.changeset/tender-mails-guess.md new file mode 100644 index 00000000..cbb28182 --- /dev/null +++ b/.changeset/tender-mails-guess.md @@ -0,0 +1,5 @@ +--- +'@ice/pkg': patch +--- + +fix: dts error and compact bundle format diff --git a/packages/pkg/src/core/init.ts b/packages/pkg/src/core/init.ts index 318fccdc..08b29ee3 100644 --- a/packages/pkg/src/core/init.ts +++ b/packages/pkg/src/core/init.ts @@ -1,10 +1,11 @@ -import { BuildTask, BundleUserConfig, Context, DeclarationUserConfig } from '../types.js'; +import { AliasBundleFormatString, BuildTask, BundleUserConfig, Context, DeclarationUserConfig } from '../types.js'; import { formatEntry, getTransformDefaultOutputDir } from '../helpers/getTaskIO.js'; import getDefaultDefineValues from '../helpers/getDefaultDefineValues.js'; import { stringifyObject } from '../utils.js'; import { merge, mergeWith, omit } from 'es-toolkit/object'; import path, { resolve } from 'node:path'; import { groupBy } from 'es-toolkit'; +import { createFormat } from '../helpers/formats.js'; const mergeDefaults: typeof merge = (target, source) => { return mergeWith(target, source, (targetValue, sourceValue) => { @@ -107,10 +108,23 @@ export function initTask(buildTask: BuildTask, options: InitTaskOptions) { // resolve to absolute config.outputDir = resolve(rootDir, config.outputDir!); + if (!config.formats) { + if (!pkg) { + // compact mode,以前的旧版本在注册任务的时候,可能不会添加 formats,则降级使用旧模式 + const legacyFormats = bundleConfig.formats ?? ['esm', 'es2017']; + const aliasedFormatsGroup = groupBy(legacyFormats, (format) => (format === 'es2017' ? 'es2017' : 'es5')); + const es5Formats = aliasedFormatsGroup.es5 as Array> | undefined; + config.formats = [...(es5Formats?.map((module) => createFormat(module, 'es5')) ?? [])]; + } else { + // 理论上 Pkg 模式不会出现这个情况,但为了健壮性还是尝试补上这部分 + config.formats = [{ module: 'esm', target: 'es5' }]; + } + } + if (pkg) { mergeDefaults(config, omit(pkg, ['id', 'pluginInfos', 'id', 'target', 'module', 'declaration', 'outputDir'])); } - mergeDefaults(config, bundleConfig); + mergeDefaults(config, omit(bundleConfig, ['formats'])); mergeDefaults(config, defaultBundleUserConfig); } else if (config.type === 'transform') { config.modes ??= [expectedMode]; diff --git a/packages/pkg/src/core/pkg.ts b/packages/pkg/src/core/pkg.ts index 65113dd2..0d9bdd8a 100644 --- a/packages/pkg/src/core/pkg.ts +++ b/packages/pkg/src/core/pkg.ts @@ -283,18 +283,12 @@ export async function runPkgPlugins(ctx: Context, pkgs: PkgResolvedConfig[]) { }; const onGetConfig = ((nameOrFn: string | ((config: unknown) => unknown), fn?: (config: unknown) => unknown) => { if (typeof nameOrFn === 'string') { - const oldFn = fn!; - fn = (config: unknown) => { - if ( - typeof config === 'object' && - config && - 'pkg' in config && - getPkgTaskName((config as any).pkg) !== taskName - ) { - return; - } - return oldFn(config); - }; + if (nameOrFn !== taskName) { + ctx.logger.warn( + `Pkg plugin "${pluginName}" is not allowed to use onGetConfig with task name "${nameOrFn}". Only allow "${taskName}" or omitted`, + ); + return; + } } else { fn = nameOrFn; nameOrFn = taskName; diff --git a/packages/pkg/src/helpers/dts.ts b/packages/pkg/src/helpers/dts.ts index 051a2b07..399f7194 100644 --- a/packages/pkg/src/helpers/dts.ts +++ b/packages/pkg/src/helpers/dts.ts @@ -160,7 +160,8 @@ async function compileFromTsc( return [...needCompileFileNames, ...dtsFilenames]; } - const ts = await import('typescript'); + const importTs = await import('typescript'); + const ts = importTs.default ?? importTs; const parsedTsConfig: ts.ParsedCommandLine = configPath ? ts.parseJsonConfigFileContent(tsConfig, ts.sys, configPath) diff --git a/packages/pkg/tests/core/init.test.ts b/packages/pkg/tests/core/init.test.ts index 28d3b749..d23db51e 100644 --- a/packages/pkg/tests/core/init.test.ts +++ b/packages/pkg/tests/core/init.test.ts @@ -406,6 +406,45 @@ describe('initTask', () => { }); }); + describe('formats', () => { + it('without pkg and without formats, using default bundleConfig.formats', () => { + const task = initTask(bt('esm', [], { formats: undefined }), c({})); + // Default bundleConfig.formats is ['esm', 'es2017'], but now only module formats are kept. + expect((task.config as BundleTaskConfig).formats).toEqual([{ module: 'esm', target: 'es5' }]); + }); + + it('without pkg and without formats, using custom bundleConfig.formats', () => { + const task = initTask( + bt('esm', [], { formats: undefined }), + c({ + bundle: { + formats: ['umd', 'cjs'], + }, + }), + ); + expect((task.config as BundleTaskConfig).formats).toEqual([ + { module: 'umd', target: 'es5' }, + { module: 'cjs', target: 'es5' }, + ]); + }); + + it('without pkg and without formats, ignore es2017 and only keep cjs/umd/esm modules', () => { + const task = initTask( + bt('esm', [], { formats: undefined }), + c({ + bundle: { + formats: ['esm', 'es2017', 'cjs', 'umd'], + }, + }), + ); + expect((task.config as BundleTaskConfig).formats).toEqual([ + { module: 'esm', target: 'es5' }, + { module: 'cjs', target: 'es5' }, + { module: 'umd', target: 'es5' }, + ]); + }); + }); + describe.each< [ key: Exclude>,