1- import { AliasBundleFormatString , Context , PackageResolvedConfig , PackageUserConfig , UserConfig } from '../types' ;
1+ import { AliasBundleFormatString , Context , PkgResolvedConfig , PkgUserConfig , UserConfig } from '../types.js ' ;
22import { ApplyMethodAPI , Context as BuildScriptContext , OnGetConfig } from 'build-scripts' ;
33import { merge , pick , omit } from 'es-toolkit/object' ;
44import { 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
3737const 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
8989function 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
141138export 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}
0 commit comments