@@ -17,18 +17,18 @@ const chalk = require('chalk');
17
17
const getNpmArgs = require ( './utils/get-npm-args' ) ;
18
18
const getChangelog = require ( './utils/getChangelog' ) ;
19
19
const path = require ( 'path' ) ;
20
+ // const watch = require('gulp-watch')
21
+ const ts = require ( 'gulp-typescript' ) ;
20
22
const gulp = require ( 'gulp' ) ;
21
- const fg = require ( 'fast-glob' ) ;
22
23
const fs = require ( 'fs' ) ;
23
24
const rimraf = require ( 'rimraf' ) ;
24
- const { createCompilerHost, createProgram } = require ( 'typescript' ) ;
25
25
const stripCode = require ( 'gulp-strip-code' ) ;
26
26
const compareVersions = require ( 'compare-versions' ) ;
27
- // const getTSCommonConfig = require('./getTSCommonConfig');
28
- const replaceLib = require ( './replaceLib' ) ;
29
27
const getTSCommonConfig = require ( './getTSCommonConfig' ) ;
28
+ const replaceLib = require ( './replaceLib' ) ;
30
29
31
30
const packageJson = require ( getProjectPath ( 'package.json' ) ) ;
31
+ const tsDefaultReporter = ts . reporter . defaultReporter ( ) ;
32
32
const cwd = process . cwd ( ) ;
33
33
const libDir = getProjectPath ( 'lib' ) ;
34
34
const esDir = getProjectPath ( 'es' ) ;
@@ -72,52 +72,29 @@ function dist(done) {
72
72
} ) ;
73
73
}
74
74
75
- async function compileTs ( modules = false , cb ) {
76
- const options = {
77
- emitDeclarationOnly : true ,
78
- ...tsConfig ,
79
- moduleResolution : 2 ,
80
- } ;
81
-
82
- const createdFiles = { } ;
83
- const host = createCompilerHost ( options ) ;
84
- host . writeFile = ( fileName , contents ) => {
85
- createdFiles [ path . isAbsolute ( fileName ) ? path . relative ( cwd , fileName ) : fileName ] = contents ;
86
- } ;
87
-
88
- const files = await fg (
89
- [
90
- 'components/**/*.js' ,
91
- 'components/**/*.jsx' ,
92
- 'components/**/*.tsx' ,
93
- 'components/**/*.ts' ,
94
- 'typings/**/*.d.ts' ,
95
- '!components/*/__tests__/*' ,
96
- '!components/*/style/*' ,
97
- '!components/styles.ts' ,
98
- ] ,
99
- { cwd } ,
100
- ) ;
101
-
102
- const program = createProgram ( files , options , host ) ;
103
- program . emit ( ) ;
75
+ const tsFiles = [ '**/*.ts' , '**/*.tsx' , '!node_modules/**/*.*' , 'typings/**/*.d.ts' ] ;
104
76
105
- Object . keys ( createdFiles ) . forEach ( fileName => {
106
- const contents = createdFiles [ fileName ] ;
107
- const filePath = path . join (
108
- cwd ,
109
- fileName . replace ( / ^ c o m p o n e n t s / , modules === false ? 'es' : 'lib' ) ,
110
- ) ;
111
- const dir = path . dirname ( filePath ) ;
112
- if ( ! fs . existsSync ( dir ) ) {
113
- fs . mkdirSync ( dir , { recursive : true } ) ;
114
- }
115
- fs . writeFileSync ( filePath , contents ) ;
116
- } ) ;
117
- cb ( 0 ) ;
77
+ function compileTs ( stream ) {
78
+ return stream
79
+ . pipe ( ts ( tsConfig ) )
80
+ . js . pipe (
81
+ through2 . obj ( function ( file , encoding , next ) {
82
+ // console.log(file.path, file.base);
83
+ file . path = file . path . replace ( / \. [ j t ] s x $ / , '.js' ) ;
84
+ this . push ( file ) ;
85
+ next ( ) ;
86
+ } ) ,
87
+ )
88
+ . pipe ( gulp . dest ( process . cwd ( ) ) ) ;
118
89
}
119
90
120
- gulp . task ( 'tsc' , ( ) => compileTs ( ) ) ;
91
+ gulp . task ( 'tsc' , ( ) =>
92
+ compileTs (
93
+ gulp . src ( tsFiles , {
94
+ base : cwd ,
95
+ } ) ,
96
+ ) ,
97
+ ) ;
121
98
122
99
function babelify ( js , modules ) {
123
100
const babelConfig = getBabelCommonConfig ( modules ) ;
@@ -192,7 +169,7 @@ function compile(modules) {
192
169
const assets = gulp
193
170
. src ( [ 'components/**/*.@(png|svg)' ] )
194
171
. pipe ( gulp . dest ( modules === false ? esDir : libDir ) ) ;
195
- // let error = 0;
172
+ let error = 0 ;
196
173
const source = [
197
174
'components/**/*.js' ,
198
175
'components/**/*.jsx' ,
@@ -202,8 +179,27 @@ function compile(modules) {
202
179
'!components/*/__tests__/*' ,
203
180
] ;
204
181
205
- const jsFilesStream = babelify ( gulp . src ( source ) , modules ) ;
206
- return merge2 ( [ less , jsFilesStream , assets ] ) ;
182
+ const tsResult = gulp . src ( source ) . pipe (
183
+ ts ( tsConfig , {
184
+ error ( e ) {
185
+ tsDefaultReporter . error ( e ) ;
186
+ error = 1 ;
187
+ } ,
188
+ finish : tsDefaultReporter . finish ,
189
+ } ) ,
190
+ ) ;
191
+
192
+ function check ( ) {
193
+ if ( error && ! argv [ 'ignore-error' ] ) {
194
+ process . exit ( 1 ) ;
195
+ }
196
+ }
197
+
198
+ tsResult . on ( 'finish' , check ) ;
199
+ tsResult . on ( 'end' , check ) ;
200
+ const tsFilesStream = babelify ( tsResult . js , modules ) ;
201
+ const tsd = tsResult . dts . pipe ( gulp . dest ( modules === false ? esDir : libDir ) ) ;
202
+ return merge2 ( [ less , tsFilesStream , tsd , assets ] ) ;
207
203
}
208
204
209
205
function tag ( ) {
@@ -332,7 +328,6 @@ function pub(done) {
332
328
333
329
let startTime = new Date ( ) ;
334
330
gulp . task ( 'compile-with-es' , done => {
335
- startTime = new Date ( ) ;
336
331
console . log ( 'start compile at ' , startTime ) ;
337
332
console . log ( '[Parallel] Compile to es...' ) ;
338
333
compile ( false ) . on ( 'finish' , done ) ;
@@ -343,31 +338,13 @@ gulp.task('compile-with-lib', done => {
343
338
compile ( ) . on ( 'finish' , done ) ;
344
339
} ) ;
345
340
346
- gulp . task ( 'compile-with-es-ts-type' , async done => {
347
- console . log ( '[Parallel] Compile to es ts type...' ) ;
348
- await compileTs ( false , done ) ;
349
- } ) ;
350
-
351
- gulp . task ( 'compile-with-lib-ts-type' , async done => {
352
- console . log ( '[Parallel] Compile to lib ts type...' ) ;
353
- await compileTs ( true , done ) ;
354
- } ) ;
355
-
356
341
gulp . task (
357
342
'compile' ,
358
- gulp . series (
359
- gulp . parallel (
360
- 'compile-with-es' ,
361
- 'compile-with-lib' ,
362
- 'compile-with-es-ts-type' ,
363
- 'compile-with-lib-ts-type' ,
364
- ) ,
365
- done => {
366
- console . log ( 'end compile at ' , new Date ( ) ) ;
367
- console . log ( 'compile time ' , ( new Date ( ) - startTime ) / 1000 , 's' ) ;
368
- done ( ) ;
369
- } ,
370
- ) ,
343
+ gulp . series ( gulp . parallel ( 'compile-with-es' , 'compile-with-lib' ) , done => {
344
+ console . log ( 'end compile at ' , new Date ( ) ) ;
345
+ console . log ( 'compile time ' , ( new Date ( ) - startTime ) / 1000 , 's' ) ;
346
+ done ( ) ;
347
+ } ) ,
371
348
) ;
372
349
373
350
gulp . task (
0 commit comments