Skip to content

Commit

Permalink
fix: type miss, close #4863
Browse files Browse the repository at this point in the history
  • Loading branch information
tangjinzhou committed Nov 9, 2021
1 parent 87a4007 commit 85eb106
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 75 deletions.
125 changes: 51 additions & 74 deletions antd-tools/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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',
Expand All @@ -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() {
Expand Down Expand Up @@ -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);
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion components/typography/Typography.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface TypographyProps extends HTMLAttributes {
prefixCls?: string;
}

interface InternalTypographyProps extends TypographyProps {
export interface InternalTypographyProps extends TypographyProps {
component?: string;
}

Expand Down

0 comments on commit 85eb106

Please sign in to comment.