@@ -10,6 +10,7 @@ import { getCjsTransformOption, getESMTransformOption } from './config.js';
10
10
export * from './utils.js' ;
11
11
12
12
export interface BabelCompileOptions {
13
+ watch ?: boolean ;
13
14
/**
14
15
* The specified entry file, for example:
15
16
* @example
@@ -64,10 +65,15 @@ export interface BabelCompileOptions {
64
65
* @default `false`
65
66
*/
66
67
sourceMaps ?: TransformOptions [ 'sourceMaps' ] ;
68
+ /**
69
+ * Exit the compile as soon as the compile fails(default: true).
70
+ * @default `true`
71
+ */
72
+ bail ?: boolean ;
67
73
}
68
74
69
75
export default async function compile ( fileName : string , options : BabelCompileOptions = { } ) {
70
- const { cjs = 'lib' , esm = 'esm' , envName, useVue = false } = options ;
76
+ const { cjs = 'lib' , esm = 'esm' , envName, useVue = false , bail , watch } = options ;
71
77
const dt = getOutputPath ( fileName , options ) ;
72
78
const log = new Log ( ) ;
73
79
log . name ( ) ;
@@ -89,7 +95,16 @@ export default async function compile(fileName: string, options: BabelCompileOpt
89
95
esmBabelOptions . cwd = dt . projectDirectory ;
90
96
91
97
if ( typeof esm === 'string' ) {
92
- transformFile ( fileName , dt . esm . path , dt . folderFilePath , dt . projectDirectory , dt . esm . fileName , esmBabelOptions ) ;
98
+ transformFile (
99
+ fileName ,
100
+ dt . esm . path ,
101
+ dt . folderFilePath ,
102
+ dt . projectDirectory ,
103
+ dt . esm . fileName ,
104
+ esmBabelOptions ,
105
+ bail ,
106
+ watch ,
107
+ ) ;
93
108
}
94
109
95
110
let cjsBabelOptions = getCjsTransformOption ( ) ;
@@ -106,7 +121,16 @@ export default async function compile(fileName: string, options: BabelCompileOpt
106
121
cjsBabelOptions . cwd = dt . projectDirectory ;
107
122
108
123
if ( typeof cjs === 'string' ) {
109
- transformFile ( fileName , dt . cjs . path , dt . folderFilePath , dt . projectDirectory , dt . cjs . fileName , cjsBabelOptions ) ;
124
+ transformFile (
125
+ fileName ,
126
+ dt . cjs . path ,
127
+ dt . folderFilePath ,
128
+ dt . projectDirectory ,
129
+ dt . cjs . fileName ,
130
+ cjsBabelOptions ,
131
+ bail ,
132
+ watch ,
133
+ ) ;
110
134
}
111
135
}
112
136
@@ -117,6 +141,8 @@ function transformFile(
117
141
projectDirectory : string ,
118
142
outFileName : string ,
119
143
options : TransformOptions ,
144
+ bail ?: boolean ,
145
+ isWatch ?: boolean ,
120
146
) {
121
147
const log = new Log ( ) ;
122
148
log . name ( ) ;
@@ -149,5 +175,8 @@ function transformFile(
149
175
} else {
150
176
log . icon ( '\n🚨' ) . error ( `\x1b[33;1m ${ JSON . stringify ( error ) } \x1b[0m\n` ) ;
151
177
}
178
+ if ( bail && isWatch !== true ) {
179
+ process . exitCode = 1 ;
180
+ }
152
181
} ) ;
153
182
}
0 commit comments