Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tender-mails-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ice/pkg': patch
---

fix: dts error and compact bundle format
18 changes: 16 additions & 2 deletions packages/pkg/src/core/init.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -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<Exclude<AliasBundleFormatString, 'es2017'>> | 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];
Expand Down
18 changes: 6 additions & 12 deletions packages/pkg/src/core/pkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion packages/pkg/src/helpers/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 39 additions & 0 deletions packages/pkg/tests/core/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<keyof BundleUserConfig, Exclude<keyof BundleUserConfig, keyof BundleTaskConfig>>,
Expand Down
Loading