@@ -17,23 +17,22 @@ 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' ) ;
22
20
const gulp = require ( 'gulp' ) ;
21
+ const fg = require ( 'fast-glob' ) ;
23
22
const fs = require ( 'fs' ) ;
24
23
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' ) ;
27
+ // const getTSCommonConfig = require('./getTSCommonConfig');
28
28
const replaceLib = require ( './replaceLib' ) ;
29
29
30
30
const packageJson = require ( getProjectPath ( 'package.json' ) ) ;
31
- const tsDefaultReporter = ts . reporter . defaultReporter ( ) ;
32
31
const cwd = process . cwd ( ) ;
33
32
const libDir = getProjectPath ( 'lib' ) ;
34
33
const esDir = getProjectPath ( 'es' ) ;
35
34
36
- const tsConfig = getTSCommonConfig ( ) ;
35
+ // const tsConfig = getTSCommonConfig();
37
36
38
37
function dist ( done ) {
39
38
rimraf . sync ( path . join ( cwd , 'dist' ) ) ;
@@ -72,29 +71,51 @@ function dist(done) {
72
71
} ) ;
73
72
}
74
73
75
- const tsFiles = [ '**/*.ts' , '**/*.tsx' , '!node_modules/**/*.*' , 'typings/**/*.d.ts' ] ;
74
+ async function compileTs ( modules = false , cb ) {
75
+ const options = {
76
+ allowJs : true ,
77
+ declaration : true ,
78
+ emitDeclarationOnly : true ,
79
+ } ;
76
80
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 ( ) ) ) ;
81
+ const createdFiles = { } ;
82
+ const host = createCompilerHost ( options ) ;
83
+ host . writeFile = ( fileName , contents ) => {
84
+ createdFiles [ path . isAbsolute ( fileName ) ? path . relative ( cwd , fileName ) : fileName ] = contents ;
85
+ } ;
86
+
87
+ const files = await fg (
88
+ [
89
+ 'components/**/*.js' ,
90
+ 'components/**/*.jsx' ,
91
+ 'components/**/*.tsx' ,
92
+ 'components/**/*.ts' ,
93
+ '!components/*/__tests__/*' ,
94
+ '!components/*/style/*' ,
95
+ '!components/styles.ts' ,
96
+ ] ,
97
+ { cwd } ,
98
+ ) ;
99
+
100
+ const program = createProgram ( files , options , host ) ;
101
+ program . emit ( ) ;
102
+
103
+ Object . keys ( createdFiles ) . forEach ( fileName => {
104
+ const contents = createdFiles [ fileName ] ;
105
+ const filePath = path . join (
106
+ cwd ,
107
+ fileName . replace ( / ^ c o m p o n e n t s / , modules === false ? 'es' : 'lib' ) ,
108
+ ) ;
109
+ const dir = path . dirname ( filePath ) ;
110
+ if ( ! fs . existsSync ( dir ) ) {
111
+ fs . mkdirSync ( dir , { recursive : true } ) ;
112
+ }
113
+ fs . writeFileSync ( filePath , contents ) ;
114
+ } ) ;
115
+ cb ( 0 ) ;
89
116
}
90
117
91
- gulp . task ( 'tsc' , ( ) =>
92
- compileTs (
93
- gulp . src ( tsFiles , {
94
- base : cwd ,
95
- } ) ,
96
- ) ,
97
- ) ;
118
+ gulp . task ( 'tsc' , ( ) => compileTs ( ) ) ;
98
119
99
120
function babelify ( js , modules ) {
100
121
const babelConfig = getBabelCommonConfig ( modules ) ;
@@ -169,7 +190,7 @@ function compile(modules) {
169
190
const assets = gulp
170
191
. src ( [ 'components/**/*.@(png|svg)' ] )
171
192
. pipe ( gulp . dest ( modules === false ? esDir : libDir ) ) ;
172
- let error = 0 ;
193
+ // let error = 0;
173
194
const source = [
174
195
'components/**/*.js' ,
175
196
'components/**/*.jsx' ,
@@ -179,27 +200,8 @@ function compile(modules) {
179
200
'!components/*/__tests__/*' ,
180
201
] ;
181
202
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 ] ) ;
203
+ const jsFilesStream = babelify ( gulp . src ( source ) , modules ) ;
204
+ return merge2 ( [ less , jsFilesStream , assets ] ) ;
203
205
}
204
206
205
207
function tag ( ) {
@@ -329,11 +331,13 @@ function pub(done) {
329
331
gulp . task ( 'compile-with-es' , done => {
330
332
console . log ( '[Parallel] Compile to es...' ) ;
331
333
compile ( false ) . on ( 'finish' , done ) ;
334
+ compileTs ( false , done ) ;
332
335
} ) ;
333
336
334
337
gulp . task ( 'compile-with-lib' , done => {
335
338
console . log ( '[Parallel] Compile to js...' ) ;
336
339
compile ( ) . on ( 'finish' , done ) ;
340
+ compileTs ( true , done ) ;
337
341
} ) ;
338
342
339
343
gulp . task (
0 commit comments