1
- import { BuildTask , BundleUserConfig , Context , DeclarationUserConfig } from '../types.js' ; // 添加 .js 后缀
2
- import { formatEntry , getTransformDefaultOutputDir } from '../helpers/getTaskIO.js' ; // 添加 .js 后缀
3
- import getDefaultDefineValues from '../helpers/getDefaultDefineValues.js' ; // 添加 .js 后缀
4
- import { stringifyObject } from '../utils.js' ; // 添加 .js 后缀
5
- import { getDefaultBundleSwcConfig , getDefaultTransformSwcConfig } from '../helpers/defaultSwcConfig.js' ; // 添加 .js 后缀
6
- import { merge , mergeWith } from 'es-toolkit/object' ;
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' ;
7
6
import path from 'node:path' ;
7
+ import { groupBy } from 'es-toolkit' ;
8
8
9
9
const mergeDefaults : typeof merge = ( target , source ) => {
10
10
return mergeWith ( target , source , ( targetValue , sourceValue ) => {
@@ -33,49 +33,64 @@ const defaultDeclarationUserConfig = {
33
33
export function initContextTasks ( ctx : Context ) {
34
34
const tasks = ctx . getTaskConfig ( ) as BuildTask [ ] ;
35
35
36
- for ( const buildTask of tasks ) {
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 ) {
37
42
initTask ( buildTask , ctx ) ;
38
43
}
44
+
45
+ // 2. init declaration based on transform tasks
46
+ for ( const buildTask of declarationTasks ?? [ ] ) {
47
+ initDeclarationTask ( buildTask , ctx , buildableTasks ) ;
48
+ }
39
49
}
40
50
41
- /**
42
- * @internal export for test
43
- */
44
- export function initTask (
45
- buildTask : BuildTask ,
46
- { userConfig, rootDir, command } : Pick < Context , 'userConfig' | 'rootDir' | 'command' > ,
47
- ) {
48
- const { config, name : taskName } = buildTask ;
51
+ type InitTaskOptions = Pick < Context , 'userConfig' | 'rootDir' | 'command' > ;
49
52
50
- config . entry = formatEntry ( config . entry ?? userConfig . entry ) ;
51
- config . alias ??= userConfig . alias ;
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 ?? { } ) ;
52
60
// Configure define
53
61
config . define = Object . assign (
54
62
// Note: The define values in bundle mode will be defined (according to the `modes` value)
55
63
// in generating rollup options. But when the command is test, we don't need to get the rollup options.
56
64
// So in test, we assume the mode is 'development'.
57
65
command === 'test' ? getDefaultDefineValues ( 'development' ) : { } ,
58
- stringifyObject ( config . define ?? userConfig . define ?? { } ) ,
66
+ stringifyObject ( userConfig . define ?? { } ) ,
67
+ stringifyObject ( pkg ?. define ?? { } ) ,
68
+ stringifyObject ( config . define ?? { } ) ,
59
69
) ;
60
70
61
- config . sourcemap ??= userConfig . sourceMaps ?? command === 'start' ;
62
- config . jsxRuntime ??= userConfig . jsxRuntime ;
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 ) ;
63
84
64
85
const expectedMode = command === 'build' ? 'production' : 'development' ;
65
86
66
87
if ( config . type === 'bundle' ) {
67
88
const bundleConfig = userConfig . bundle ?? { } ;
68
89
config . modes ??= bundleConfig . modes ?? [ expectedMode ] ;
69
- config . engine ??= bundleConfig . engine ;
70
- const defaultBundleSwcConfig = getDefaultBundleSwcConfig ( config ) ;
71
- config . swcCompileOptions =
72
- typeof config . modifySwcCompileOptions === 'function'
73
- ? config . modifySwcCompileOptions ( defaultBundleSwcConfig )
74
- : merge ( defaultBundleSwcConfig , config . swcCompileOptions || { } ) ;
75
90
// TODO: 判断下这个东西是否真的有用
76
91
// Set outputDir to process.env for CI
77
92
process . env . ICE_PKG_BUNDLE_OUTPUT_DIR = config . outputDir ;
78
- const originMinifyConfig = bundleConfig . minify ?? defaultBundleUserConfig . minify ?? { } ;
93
+ const originMinifyConfig = pkg ?. minify ?? bundleConfig . minify ?? defaultBundleUserConfig . minify ?? { } ;
79
94
let { jsMinify, cssMinify } = config ;
80
95
if ( typeof originMinifyConfig === 'object' ) {
81
96
jsMinify ??= getMinifyFunction ( originMinifyConfig . js ) ;
@@ -88,39 +103,60 @@ export function initTask(
88
103
config . jsMinify = jsMinify ;
89
104
config . cssMinify = cssMinify ;
90
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
+ }
91
111
mergeDefaults ( config , bundleConfig ) ;
92
112
mergeDefaults ( config , defaultBundleUserConfig ) ;
93
113
} else if ( config . type === 'transform' ) {
94
114
config . modes ??= [ expectedMode ] ;
95
- config . outputDir ??= getTransformDefaultOutputDir ( rootDir , taskName ) ;
96
- const defaultTransformSwcConfig = getDefaultTransformSwcConfig ( config , expectedMode ) ;
97
- config . swcCompileOptions =
98
- typeof config . modifySwcCompileOptions === 'function'
99
- ? config . modifySwcCompileOptions ( defaultTransformSwcConfig )
100
- : merge ( defaultTransformSwcConfig , config . swcCompileOptions || { } ) ;
115
+ config . outputDir ??= pkg ?. outputDir ?? getTransformDefaultOutputDir ( rootDir , taskName , config ) ;
101
116
} else if ( config . type === 'declaration' ) {
102
- const { declaration : declarationConfig } = userConfig ;
103
- if ( declarationConfig === false ) {
104
- throw new Error ( 'Cannot disable declaration when transform formats is not empty.' ) ;
105
- }
106
- config . outputMode ??=
107
- declarationConfig === true
108
- ? defaultDeclarationUserConfig . outputMode
109
- : ( declarationConfig . outputMode ?? defaultDeclarationUserConfig . outputMode ) ;
110
- // 这个 output 仅仅用于生成正确的 .d.ts 的 alias,不做实际输出目录
111
- config . outputDir = path . resolve ( rootDir , config . transformFormats [ 0 ] ) ;
112
- if ( config . outputMode === 'unique' ) {
113
- config . declarationOutputDirs = [ path . resolve ( rootDir , 'typings' ) ] ;
114
- } else {
115
- config . declarationOutputDirs = config . transformFormats . map ( ( format ) => path . resolve ( rootDir , format ) ) ;
116
- }
117
+ // should run in initDeclarationTask
117
118
} else {
118
119
throw new Error ( 'Invalid task type.' ) ;
119
120
}
120
121
121
122
return buildTask ;
122
123
}
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
+
124
160
function getMinifyFunction ( minify : boolean | ( ( mode : string , command : string ) => unknown ) ) {
125
161
switch ( typeof minify ) {
126
162
case 'boolean' :
0 commit comments