Skip to content

Commit 86a6718

Browse files
committed
feat: support disable for pkg config
1 parent 4b41cfe commit 86a6718

File tree

6 files changed

+82
-31
lines changed

6 files changed

+82
-31
lines changed

.changeset/wet-rocks-dream.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ice/pkg': patch
3+
---
4+
5+
feat: support `disable` for pkg config

packages/pkg/src/core/pkg.ts

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AliasBundleFormatString, Context, PackageResolvedConfig, PackageUserConfig, UserConfig } from '../types';
1+
import { AliasBundleFormatString, Context, PkgResolvedConfig, PkgUserConfig, UserConfig } from '../types.js';
22
import { ApplyMethodAPI, Context as BuildScriptContext, OnGetConfig } from 'build-scripts';
33
import { merge, pick, omit } from 'es-toolkit/object';
44
import { groupBy } from 'es-toolkit/array';
@@ -25,7 +25,7 @@ async function resolvePlugins(ctx: Context, plugins: UserConfig['plugins']) {
2525
commandArgs: ctx.commandArgs,
2626
});
2727
mockContext.userConfig = {
28-
plugins,
28+
plugins: plugins as BuildScriptContext['userConfig']['plugins'],
2929
};
3030
return await mockContext.resolvePlugins();
3131
}
@@ -36,7 +36,7 @@ function isBundlePresetPkgString(pkg: string): boolean {
3636

3737
const LEGACY_PRESET_CONFIG_MAP: Record<
3838
string,
39-
Pick<PackageResolvedConfig, 'id' | 'module' | 'target' | 'bundle' | 'outputDir' | 'displayId'>
39+
Pick<PkgResolvedConfig, 'id' | 'module' | 'target' | 'bundle' | 'outputDir' | 'displayId'>
4040
> = {
4141
esm: {
4242
id: 'esm',
@@ -88,7 +88,7 @@ const LEGACY_PRESET_CONFIG_MAP: Record<
8888

8989
function parsePresetPkgString(
9090
preset?: string,
91-
): Pick<PackageResolvedConfig, 'id' | 'module' | 'target' | 'bundle' | 'outputDir' | 'displayId'> | null {
91+
): Pick<PkgResolvedConfig, 'id' | 'module' | 'target' | 'bundle' | 'outputDir' | 'displayId'> | null {
9292
if (!preset) {
9393
return null;
9494
}
@@ -110,14 +110,11 @@ function parsePresetPkgString(
110110
};
111111
}
112112

113-
function resolveExtends(
114-
extendsConfig: string[] = [],
115-
pkgsMap: Map<string, PackageResolvedConfig>,
116-
): Partial<PackageUserConfig> {
117-
let mergedConfig: Partial<PackageUserConfig> = {};
113+
function resolveExtends(extendsConfig: string[] = [], pkgsMap: Map<string, PkgResolvedConfig>): Partial<PkgUserConfig> {
114+
let mergedConfig: Partial<PkgUserConfig> = {};
118115

119116
for (const extend of extendsConfig) {
120-
let extendConfig: Partial<PackageUserConfig> | null = null;
117+
let extendConfig: Partial<PkgUserConfig> | null = null;
121118

122119
// 尝试解析为预设字符串
123120
const presetConfig = parsePresetPkgString(extend);
@@ -140,9 +137,10 @@ function resolveExtends(
140137

141138
export async function resolvePackage(ctx: Context) {
142139
const { userConfig } = ctx;
140+
// filter undefined or boolean out
143141
const pkgs = userConfig.pkgs ?? [];
144-
const resolvedPkgs: PackageResolvedConfig[] = [];
145-
const pkgsMap = new Map<string, PackageResolvedConfig>();
142+
const resolvedPkgs: PkgResolvedConfig[] = [];
143+
const pkgsMap = new Map<string, PkgResolvedConfig>();
146144

147145
function toValidId(id: string): string {
148146
if (!pkgsMap.has(id)) return id;
@@ -157,16 +155,22 @@ export async function resolvePackage(ctx: Context) {
157155
if (typeof pkg === 'string') {
158156
if (isBundlePresetPkgString(pkg)) {
159157
if (isAliasFormatString(pkg.slice(1), ALIAS_BUNDLE_FORMATS_MAP)) {
158+
// 旧版本的 Bundle 配置,例如 esm/es2017,它们之间关系比较特殊,属于正交的能力,所以需要单独处理
160159
return 'bundleLegacy';
161160
}
162161
}
163162
return 'preset';
164163
}
164+
if (typeof pkg !== 'object' || pkg.disable) {
165+
// invalid pkg or disabled pkg
166+
return 'ignore';
167+
}
165168
return 'pkg';
166169
}) as {
167170
bundleLegacy?: string[];
168171
preset?: string[];
169-
pkg?: PackageUserConfig[];
172+
pkg?: PkgUserConfig[];
173+
ignore?: unknown[];
170174
};
171175

172176
if (groupedPkgs.bundleLegacy?.length) {
@@ -175,7 +179,7 @@ export async function resolvePackage(ctx: Context) {
175179
const es5Formats = aliasedFormatsGroup.es5 as Array<Exclude<AliasBundleFormatString, 'es2017'>> | undefined;
176180

177181
if (es5Formats?.length) {
178-
const resolvedPkg: PackageResolvedConfig = {
182+
const resolvedPkg: PkgResolvedConfig = {
179183
id: toValidId('!es5'),
180184
module: 'esm', // will be ignored
181185
target: 'es5',
@@ -189,7 +193,7 @@ export async function resolvePackage(ctx: Context) {
189193
}
190194

191195
if (aliasedFormatsGroup.es2017?.length) {
192-
const resolvedPkg: PackageResolvedConfig = {
196+
const resolvedPkg: PkgResolvedConfig = {
193197
id: toValidId('!es2017'),
194198
module: 'esm', // will be ignored
195199
target: 'es2017',
@@ -209,7 +213,7 @@ export async function resolvePackage(ctx: Context) {
209213
throw new Error(`Unknown preset package "${pkg}"`);
210214
}
211215
const id = toValidId(presetConfig.id);
212-
const resolvedPkg: PackageResolvedConfig = {
216+
const resolvedPkg: PkgResolvedConfig = {
213217
...presetConfig,
214218
id,
215219
pluginInfos: [],
@@ -223,7 +227,7 @@ export async function resolvePackage(ctx: Context) {
223227
// 处理 extends 配置
224228
const extendedConfig = resolveExtends(extendsConfig, pkgsMap);
225229

226-
const resolvedPkg: PackageResolvedConfig = {
230+
const resolvedPkg: PkgResolvedConfig = {
227231
target,
228232
module,
229233
...extendedConfig,
@@ -252,7 +256,7 @@ export async function resolvePackage(ctx: Context) {
252256
return resolvedPkgs;
253257
}
254258

255-
export async function runPkgPlugins(ctx: Context, pkgs: PackageResolvedConfig[]) {
259+
export async function runPkgPlugins(ctx: Context, pkgs: PkgResolvedConfig[]) {
256260
for (const pkg of pkgs) {
257261
for (const pluginInfo of pkg.pluginInfos) {
258262
const { setup, options, name: pluginName } = pluginInfo;
@@ -317,6 +321,6 @@ export async function runPkgPlugins(ctx: Context, pkgs: PackageResolvedConfig[])
317321
}
318322
}
319323

320-
export function getPkgTaskName(pkg: PackageResolvedConfig) {
324+
export function getPkgTaskName(pkg: PkgResolvedConfig) {
321325
return `${pkg.bundle ? 'bundle' : 'transform'}-${pkg.displayId ?? pkg.id}`;
322326
}

packages/pkg/src/core/register.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
BundleFormat,
66
TransformFormat,
77
TaskName,
8-
PackageResolvedConfig,
8+
PkgResolvedConfig,
99
NodeModuleType,
1010
} from '../types.js';
1111
import { createFormat, isAliasFormatString, toFormat, tryToFormat } from '../helpers/formats.js';
@@ -107,7 +107,7 @@ export function registerTasks(ctx: Context, customFormats: Record<string, Custom
107107
}
108108
}
109109

110-
export function registerPkgTasks(ctx: Context, pkgs: PackageResolvedConfig[]) {
110+
export function registerPkgTasks(ctx: Context, pkgs: PkgResolvedConfig[]) {
111111
const { userConfig, registerTask } = ctx;
112112
let hasTransformTasks = false;
113113
for (const pkg of pkgs) {

packages/pkg/src/types.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export interface DeclarationUserConfig {
159159
outputMode?: 'multi' | 'unique';
160160
}
161161

162-
export interface PackageUserConfig
162+
export interface PkgUserConfig
163163
extends Pick<
164164
BundleUserConfig,
165165
'externals' | 'name' | 'compileDependencies' | 'polyfill' | 'engine' | 'minify' | 'codeSplitting'
@@ -191,7 +191,7 @@ export interface PackageUserConfig
191191
/**
192192
* Extends other packages, use preset package or other package id
193193
*/
194-
extends?: Array<PresetPackage | string>;
194+
extends?: Array<PresetPkg | string>;
195195

196196
/**
197197
* Plugins only for this package
@@ -202,12 +202,17 @@ export interface PackageUserConfig
202202
* Define output directory
203203
*/
204204
outputDir?: string;
205+
206+
/**
207+
* Disable this pkg to build
208+
*/
209+
disable?: boolean;
205210
}
206211

207-
type PackageResolvedRequiredConfigKeys = 'module' | 'target' | 'id';
208-
export interface PackageResolvedConfig
209-
extends Omit<PackageUserConfig, 'extends' | 'preset' | 'plugins' | PackageResolvedRequiredConfigKeys>,
210-
Required<Pick<PackageUserConfig, PackageResolvedRequiredConfigKeys>> {
212+
type PkgResolvedRequiredConfigKeys = 'module' | 'target' | 'id';
213+
export interface PkgResolvedConfig
214+
extends Omit<PkgUserConfig, 'extends' | 'preset' | 'plugins' | PkgResolvedRequiredConfigKeys>,
215+
Required<Pick<PkgUserConfig, PkgResolvedRequiredConfigKeys>> {
211216
pluginInfos: Array<_PluginInfo<any, any, any>>;
212217
/**
213218
* for compat old task config, used for build task name
@@ -221,10 +226,10 @@ export interface PackageResolvedConfig
221226
legacyModules?: ModuleType[];
222227
}
223228

224-
type PresetPackage = TransformUserFormat | `!${BundleUserFormat}`;
229+
export type PresetPkg = TransformUserFormat | `!${BundleUserFormat}`;
225230

226231
export interface UserConfig {
227-
pkgs?: Array<PresetPackage | PackageUserConfig>;
232+
pkgs?: Array<PresetPkg | PkgUserConfig | boolean | undefined>;
228233
/**
229234
* Entry for a task
230235
* @default `./src/index`
@@ -279,7 +284,7 @@ export interface UserConfig {
279284
bundle?: BundleUserConfig;
280285
}
281286

282-
export type PluginUserConfig = string | [string, Json] | Plugin;
287+
export type PluginUserConfig = string | [string, Json?] | Plugin;
283288

284289
interface _TaskConfig {
285290
/**
@@ -342,7 +347,7 @@ interface _TaskConfig {
342347
*/
343348
modifyRslibConfig?: Array<(rslibOptions: RslibConfig) => RslibConfig>;
344349

345-
pkg?: PackageResolvedConfig;
350+
pkg?: PkgResolvedConfig;
346351
}
347352

348353
export type EngineType = 'rollup' | 'rslib';

tests/integration/default/__snapshots__/index.test.ts.snap

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,26 @@ exports[`Run config pkg-config > esm structure 1`] = `
279279
}
280280
`;
281281

282+
exports[`Run config pkg-config-disabled > cjs structure 1`] = `null`;
283+
284+
exports[`Run config pkg-config-disabled > dist structure 1`] = `null`;
285+
286+
exports[`Run config pkg-config-disabled > es2017 structure 1`] = `null`;
287+
288+
exports[`Run config pkg-config-disabled > esm structure 1`] = `
289+
{
290+
"files": [
291+
{
292+
"name": "index.d.ts",
293+
},
294+
{
295+
"name": "index.js",
296+
},
297+
],
298+
"name": "esm",
299+
}
300+
`;
301+
282302
exports[`Run config sourcemap-enable > cjs structure 1`] = `null`;
283303

284304
exports[`Run config sourcemap-enable > dist structure 1`] = `null`;

tests/integration/default/index.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,21 @@ runProjectTest(import.meta.url, [
101101
],
102102
},
103103
},
104+
{
105+
name: 'pkg-config-disabled',
106+
snapshot: 'structure',
107+
config: {
108+
pkgs: [
109+
{
110+
module: 'esm',
111+
target: 'es2017',
112+
},
113+
{
114+
module: 'cjs',
115+
target: 'es2017',
116+
disable: true,
117+
},
118+
],
119+
},
120+
},
104121
]);

0 commit comments

Comments
 (0)