@@ -17,23 +17,22 @@ const chalk = require('chalk');
1717const getNpmArgs = require ( './utils/get-npm-args' ) ;
1818const getChangelog = require ( './utils/getChangelog' ) ;
1919const path = require ( 'path' ) ;
20- // const watch = require('gulp-watch')
21- const ts = require ( 'gulp-typescript' ) ;
2220const gulp = require ( 'gulp' ) ;
21+ const fg = require ( 'fast-glob' ) ;
2322const fs = require ( 'fs' ) ;
2423const rimraf = require ( 'rimraf' ) ;
24+ const { createCompilerHost, createProgram } = require ( 'typescript' ) ;
2525const stripCode = require ( 'gulp-strip-code' ) ;
2626const compareVersions = require ( 'compare-versions' ) ;
27- const getTSCommonConfig = require ( './getTSCommonConfig' ) ;
27+ // const getTSCommonConfig = require('./getTSCommonConfig');
2828const replaceLib = require ( './replaceLib' ) ;
2929
3030const packageJson = require ( getProjectPath ( 'package.json' ) ) ;
31- const tsDefaultReporter = ts . reporter . defaultReporter ( ) ;
3231const cwd = process . cwd ( ) ;
3332const libDir = getProjectPath ( 'lib' ) ;
3433const esDir = getProjectPath ( 'es' ) ;
3534
36- const tsConfig = getTSCommonConfig ( ) ;
35+ // const tsConfig = getTSCommonConfig();
3736
3837function dist ( done ) {
3938 rimraf . sync ( path . join ( cwd , 'dist' ) ) ;
@@ -72,29 +71,51 @@ function dist(done) {
7271 } ) ;
7372}
7473
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+ } ;
7680
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 ) ;
89116}
90117
91- gulp . task ( 'tsc' , ( ) =>
92- compileTs (
93- gulp . src ( tsFiles , {
94- base : cwd ,
95- } ) ,
96- ) ,
97- ) ;
118+ gulp . task ( 'tsc' , ( ) => compileTs ( ) ) ;
98119
99120function babelify ( js , modules ) {
100121 const babelConfig = getBabelCommonConfig ( modules ) ;
@@ -169,7 +190,7 @@ function compile(modules) {
169190 const assets = gulp
170191 . src ( [ 'components/**/*.@(png|svg)' ] )
171192 . pipe ( gulp . dest ( modules === false ? esDir : libDir ) ) ;
172- let error = 0 ;
193+ // let error = 0;
173194 const source = [
174195 'components/**/*.js' ,
175196 'components/**/*.jsx' ,
@@ -179,27 +200,8 @@ function compile(modules) {
179200 '!components/*/__tests__/*' ,
180201 ] ;
181202
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 ] ) ;
203205}
204206
205207function tag ( ) {
@@ -329,11 +331,13 @@ function pub(done) {
329331gulp . task ( 'compile-with-es' , done => {
330332 console . log ( '[Parallel] Compile to es...' ) ;
331333 compile ( false ) . on ( 'finish' , done ) ;
334+ compileTs ( false , done ) ;
332335} ) ;
333336
334337gulp . task ( 'compile-with-lib' , done => {
335338 console . log ( '[Parallel] Compile to js...' ) ;
336339 compile ( ) . on ( 'finish' , done ) ;
340+ compileTs ( true , done ) ;
337341} ) ;
338342
339343gulp . task (
0 commit comments