Skip to content

Commit 5ff95c6

Browse files
committed
chore: fix process ts
1 parent c739e8e commit 5ff95c6

File tree

6 files changed

+220
-189
lines changed

6 files changed

+220
-189
lines changed

src/getBabelCommonConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { resolve, isThereHaveBrowserslistConfig } from './utils/projectHelper';
22
import fs from 'fs-extra';
33
import type { TransformOptions } from '@babel/core';
44

5-
export default function getBabelCommonConfig(modules: boolean): TransformOptions & {
5+
export default function getBabelCommonConfig(modules?: boolean): TransformOptions & {
66
cacheDirectory?: boolean;
77
} {
88
const plugins = [

src/jest/codePreprocessor.ts

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,45 @@
1-
const crypto = require('crypto');
2-
const { createTransformer } = require('babel-jest').default;
3-
const getBabelCommonConfig = require('../getBabelCommonConfig');
4-
const rewriteSource = require('./rewriteSource');
5-
const pkg = require('../../package.json');
1+
import * as crypto from 'crypto';
2+
import { createTransformer } from 'babel-jest';
3+
import getBabelCommonConfig from '../getBabelCommonConfig';
4+
import rewriteSource from './rewriteSource';
5+
import pkg from '../../package.json';
6+
import * as babel from '@babel/core';
67

7-
const libDir = process.env.LIB_DIR || 'components';
8+
const libDir: string = process.env.LIB_DIR || 'components';
89

9-
function processDemo({ types: t }) {
10+
function processDemo({ types: t }: { types: typeof babel.types }): babel.PluginObj {
1011
return {
1112
visitor: {
12-
ImportDeclaration(path) {
13+
ImportDeclaration(path: babel.NodePath<babel.types.ImportDeclaration>) {
1314
rewriteSource(t, path, libDir);
1415
},
1516
},
1617
};
1718
}
1819

19-
module.exports = {
20+
interface TransformOptions {
21+
instrument: boolean;
22+
}
23+
24+
interface Preprocessor {
25+
canInstrument: boolean;
26+
process(
27+
src: string,
28+
filePath: string,
29+
config: object,
30+
transformOptions: TransformOptions
31+
): string;
32+
getCacheKey(): string;
33+
}
34+
35+
const preprocessor: Preprocessor = {
2036
canInstrument: true,
21-
process(src, path, config, transformOptions) {
37+
process(src, filePath, config, transformOptions) {
2238
global.__clearBabelAntdPlugin && global.__clearBabelAntdPlugin(); // eslint-disable-line
2339
const babelConfig = getBabelCommonConfig();
24-
babelConfig.plugins = [...babelConfig.plugins];
40+
babelConfig.plugins = [...(babelConfig.plugins || [])];
2541

26-
if (/\/demo\//.test(path)) {
42+
if (/\/demo\//.test(filePath)) {
2743
babelConfig.plugins.push(processDemo);
2844
}
2945

@@ -35,15 +51,20 @@ module.exports = {
3551
},
3652
]);
3753

38-
const babelSupport =
39-
path.endsWith('.ts') ||
40-
path.endsWith('.tsx') ||
41-
path.endsWith('.js') ||
42-
path.endsWith('.jsx');
43-
54+
const babelSupport = /\.(t|j)sx?$/.test(filePath);
4455
const babelJest = createTransformer(babelConfig);
45-
const fileName = babelSupport ? path : 'file.js';
46-
return babelJest.process(src, fileName, config, transformOptions);
56+
const name = babelSupport ? filePath : 'file.js';
57+
58+
type ProcessParams = Parameters<typeof babelJest.process>;
59+
60+
return (
61+
babelJest.process as unknown as (
62+
src: ProcessParams[0],
63+
name: ProcessParams[1],
64+
config: object,
65+
transformOptions: TransformOptions
66+
) => string
67+
)(src, name, config, transformOptions);
4768
},
4869

4970
getCacheKey() {
@@ -56,3 +77,5 @@ module.exports = {
5677
.digest('hex');
5778
},
5879
};
80+
81+
module.exports = preprocessor;

src/jest/demoPreprocessor.js

Lines changed: 0 additions & 148 deletions
This file was deleted.

0 commit comments

Comments
 (0)