From 85eb1066a3698ea810af61bd68e510148b381a92 Mon Sep 17 00:00:00 2001 From: tangjinzhou <415800467@qq.com> Date: Tue, 9 Nov 2021 14:36:15 +0800 Subject: [PATCH] fix: type miss, close #4863 --- antd-tools/gulpfile.js | 125 +++++++++++---------------- components/typography/Typography.tsx | 2 +- 2 files changed, 52 insertions(+), 75 deletions(-) diff --git a/antd-tools/gulpfile.js b/antd-tools/gulpfile.js index 71e2c42830..6a3243cec7 100644 --- a/antd-tools/gulpfile.js +++ b/antd-tools/gulpfile.js @@ -17,18 +17,18 @@ const chalk = require('chalk'); const getNpmArgs = require('./utils/get-npm-args'); const getChangelog = require('./utils/getChangelog'); const path = require('path'); +// const watch = require('gulp-watch') +const ts = require('gulp-typescript'); const gulp = require('gulp'); -const fg = require('fast-glob'); const fs = require('fs'); const rimraf = require('rimraf'); -const { createCompilerHost, createProgram } = require('typescript'); const stripCode = require('gulp-strip-code'); const compareVersions = require('compare-versions'); -// const getTSCommonConfig = require('./getTSCommonConfig'); -const replaceLib = require('./replaceLib'); const getTSCommonConfig = require('./getTSCommonConfig'); +const replaceLib = require('./replaceLib'); const packageJson = require(getProjectPath('package.json')); +const tsDefaultReporter = ts.reporter.defaultReporter(); const cwd = process.cwd(); const libDir = getProjectPath('lib'); const esDir = getProjectPath('es'); @@ -72,52 +72,29 @@ function dist(done) { }); } -async function compileTs(modules = false, cb) { - const options = { - emitDeclarationOnly: true, - ...tsConfig, - moduleResolution: 2, - }; - - const createdFiles = {}; - const host = createCompilerHost(options); - host.writeFile = (fileName, contents) => { - createdFiles[path.isAbsolute(fileName) ? path.relative(cwd, fileName) : fileName] = contents; - }; - - const files = await fg( - [ - 'components/**/*.js', - 'components/**/*.jsx', - 'components/**/*.tsx', - 'components/**/*.ts', - 'typings/**/*.d.ts', - '!components/*/__tests__/*', - '!components/*/style/*', - '!components/styles.ts', - ], - { cwd }, - ); - - const program = createProgram(files, options, host); - program.emit(); +const tsFiles = ['**/*.ts', '**/*.tsx', '!node_modules/**/*.*', 'typings/**/*.d.ts']; - Object.keys(createdFiles).forEach(fileName => { - const contents = createdFiles[fileName]; - const filePath = path.join( - cwd, - fileName.replace(/^components/, modules === false ? 'es' : 'lib'), - ); - const dir = path.dirname(filePath); - if (!fs.existsSync(dir)) { - fs.mkdirSync(dir, { recursive: true }); - } - fs.writeFileSync(filePath, contents); - }); - cb(0); +function compileTs(stream) { + return stream + .pipe(ts(tsConfig)) + .js.pipe( + through2.obj(function (file, encoding, next) { + // console.log(file.path, file.base); + file.path = file.path.replace(/\.[jt]sx$/, '.js'); + this.push(file); + next(); + }), + ) + .pipe(gulp.dest(process.cwd())); } -gulp.task('tsc', () => compileTs()); +gulp.task('tsc', () => + compileTs( + gulp.src(tsFiles, { + base: cwd, + }), + ), +); function babelify(js, modules) { const babelConfig = getBabelCommonConfig(modules); @@ -192,7 +169,7 @@ function compile(modules) { const assets = gulp .src(['components/**/*.@(png|svg)']) .pipe(gulp.dest(modules === false ? esDir : libDir)); - // let error = 0; + let error = 0; const source = [ 'components/**/*.js', 'components/**/*.jsx', @@ -202,8 +179,27 @@ function compile(modules) { '!components/*/__tests__/*', ]; - const jsFilesStream = babelify(gulp.src(source), modules); - return merge2([less, jsFilesStream, assets]); + const tsResult = gulp.src(source).pipe( + ts(tsConfig, { + error(e) { + tsDefaultReporter.error(e); + error = 1; + }, + finish: tsDefaultReporter.finish, + }), + ); + + function check() { + if (error && !argv['ignore-error']) { + process.exit(1); + } + } + + tsResult.on('finish', check); + tsResult.on('end', check); + const tsFilesStream = babelify(tsResult.js, modules); + const tsd = tsResult.dts.pipe(gulp.dest(modules === false ? esDir : libDir)); + return merge2([less, tsFilesStream, tsd, assets]); } function tag() { @@ -332,7 +328,6 @@ function pub(done) { let startTime = new Date(); gulp.task('compile-with-es', done => { - startTime = new Date(); console.log('start compile at ', startTime); console.log('[Parallel] Compile to es...'); compile(false).on('finish', done); @@ -343,31 +338,13 @@ gulp.task('compile-with-lib', done => { compile().on('finish', done); }); -gulp.task('compile-with-es-ts-type', async done => { - console.log('[Parallel] Compile to es ts type...'); - await compileTs(false, done); -}); - -gulp.task('compile-with-lib-ts-type', async done => { - console.log('[Parallel] Compile to lib ts type...'); - await compileTs(true, done); -}); - gulp.task( 'compile', - gulp.series( - gulp.parallel( - 'compile-with-es', - 'compile-with-lib', - 'compile-with-es-ts-type', - 'compile-with-lib-ts-type', - ), - done => { - console.log('end compile at ', new Date()); - console.log('compile time ', (new Date() - startTime) / 1000, 's'); - done(); - }, - ), + gulp.series(gulp.parallel('compile-with-es', 'compile-with-lib'), done => { + console.log('end compile at ', new Date()); + console.log('compile time ', (new Date() - startTime) / 1000, 's'); + done(); + }), ); gulp.task( diff --git a/components/typography/Typography.tsx b/components/typography/Typography.tsx index c54254345a..1fdbda1ed2 100644 --- a/components/typography/Typography.tsx +++ b/components/typography/Typography.tsx @@ -8,7 +8,7 @@ export interface TypographyProps extends HTMLAttributes { prefixCls?: string; } -interface InternalTypographyProps extends TypographyProps { +export interface InternalTypographyProps extends TypographyProps { component?: string; }