@modern-js/module-tools 1.1.5 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +4 -1
- package/CHANGELOG.md +85 -0
- package/dist/js/modern/cli/build.js +1 -1
- package/dist/js/modern/cli/dev.js +2 -2
- package/dist/js/modern/commands/dev.js +13 -1
- package/dist/js/modern/features/dev/index.js +11 -0
- package/dist/js/node/cli/build.js +1 -1
- package/dist/js/node/cli/dev.js +2 -2
- package/dist/js/node/commands/dev.js +12 -1
- package/dist/js/node/features/dev/index.js +16 -2
- package/dist/types/commands/build.d.ts +1 -1
- package/dist/types/commands/dev.d.ts +1 -1
- package/dist/types/features/dev/index.d.ts +2 -1
- package/dist/types/index.d.ts +3 -1
- package/jest.config.js +8 -0
- package/lib/types.d.ts +0 -11
- package/package.json +30 -22
- package/tests/dev-cli.test.ts +25 -0
- package/tests/dev-command.test.ts +44 -0
- package/tests/dev-feature.test.ts +30 -0
- package/tests/fixtures/tspaths/a.ts +1 -0
- package/tests/fixtures/tspaths/b.ts +1 -0
- package/tests/index.test.ts +7 -0
- package/tests/tsconfig.json +11 -0
- package/tests/tspaths-transform.test.ts +21 -0
- package/tsconfig.json +2 -3
- package/src/.eslintrc.json +0 -5
- package/src/cli/build.ts +0 -39
- package/src/cli/dev.ts +0 -24
- package/src/cli/index.ts +0 -3
- package/src/cli/new.ts +0 -32
- package/src/commands/build.ts +0 -74
- package/src/commands/dev.ts +0 -43
- package/src/commands/index.ts +0 -2
- package/src/features/build/build-platform.ts +0 -83
- package/src/features/build/build-watch.ts +0 -99
- package/src/features/build/build.ts +0 -111
- package/src/features/build/constants.ts +0 -52
- package/src/features/build/index.ts +0 -54
- package/src/features/build/logger/index.ts +0 -2
- package/src/features/build/logger/logText.ts +0 -80
- package/src/features/build/logger/loggerManager.ts +0 -132
- package/src/features/build/utils.ts +0 -235
- package/src/features/dev/index.ts +0 -62
- package/src/index.ts +0 -55
- package/src/locale/en.ts +0 -21
- package/src/locale/index.ts +0 -15
- package/src/locale/zh.ts +0 -21
- package/src/schema/index.ts +0 -4
- package/src/schema/output.ts +0 -41
- package/src/schema/schema.d.ts +0 -13
- package/src/schema/source.ts +0 -16
- package/src/tasks/build-source-code.ts +0 -234
- package/src/tasks/build-style.ts +0 -194
- package/src/tasks/build-watch-source-code.ts +0 -186
- package/src/tasks/build-watch-style.ts +0 -271
- package/src/tasks/constants.ts +0 -1
- package/src/tasks/copy-assets.ts +0 -123
- package/src/tasks/generator-dts.ts +0 -277
- package/src/type.d.ts +0 -1
- package/src/types.ts +0 -65
- package/src/utils/babel.ts +0 -104
- package/src/utils/color.ts +0 -3
- package/src/utils/copy.ts +0 -71
- package/src/utils/init-env.ts +0 -31
- package/src/utils/json.ts +0 -13
- package/src/utils/language.ts +0 -9
- package/src/utils/logger.ts +0 -141
- package/src/utils/readline.ts +0 -28
- package/src/utils/tsconfig.ts +0 -37
- package/src/utils/tspaths-transform/constants.ts +0 -19
- package/src/utils/tspaths-transform/index.ts +0 -139
- package/src/utils/tspaths-transform/utils.ts +0 -25
- package/src/utils/valide.ts +0 -37
package/src/types.ts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import type { OutputConfig, SourceConfig } from '@modern-js/core/config';
|
|
2
|
-
import type { ImportStyleType } from '@modern-js/babel-preset-module';
|
|
3
|
-
import type { NormalizedConfig } from '@modern-js/core';
|
|
4
|
-
import type { LoggerText } from './features/build/logger/logText';
|
|
5
|
-
import type { Platform } from './features/build/build-platform';
|
|
6
|
-
|
|
7
|
-
export type { Platform } from './features/build/build-platform';
|
|
8
|
-
export type { ITsconfig } from './utils/tsconfig';
|
|
9
|
-
|
|
10
|
-
export interface ITaskMapper {
|
|
11
|
-
logger: LoggerText | null;
|
|
12
|
-
taskPath: string;
|
|
13
|
-
params?: string[];
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export type PackageModeType =
|
|
17
|
-
| 'universal-js'
|
|
18
|
-
| 'universal-js-lite'
|
|
19
|
-
| 'browser-js'
|
|
20
|
-
| 'browser-js-lite'
|
|
21
|
-
| 'node-js';
|
|
22
|
-
|
|
23
|
-
export type JsSyntaxType = 'CJS+ES6' | 'ESM+ES5' | 'ESM+ES6';
|
|
24
|
-
|
|
25
|
-
export interface IPackageFields {
|
|
26
|
-
main?: JsSyntaxType;
|
|
27
|
-
modern?: JsSyntaxType;
|
|
28
|
-
module?: JsSyntaxType;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface IBuildConfig {
|
|
32
|
-
appDirectory: string;
|
|
33
|
-
platform: boolean | Exclude<Platform, 'all'>;
|
|
34
|
-
enableTscCompiler: boolean;
|
|
35
|
-
enableWatchMode?: boolean;
|
|
36
|
-
isTsProject: boolean;
|
|
37
|
-
sourceDir: string;
|
|
38
|
-
tsconfigName?: string;
|
|
39
|
-
clear?: boolean;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export interface IPackageModeValue {
|
|
43
|
-
type: 'module' | 'commonjs';
|
|
44
|
-
syntax: 'es5' | 'es6+';
|
|
45
|
-
outDir: 'node' | 'treeshaking' | 'modern';
|
|
46
|
-
copyDirs?: ('node' | 'treeshaking' | 'modern')[];
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export interface ModuleToolsOutput extends OutputConfig {
|
|
50
|
-
assetsPath: string;
|
|
51
|
-
disableTsChecker: boolean;
|
|
52
|
-
enableSourceMap: boolean;
|
|
53
|
-
packageMode: PackageModeType;
|
|
54
|
-
packageFields: IPackageFields;
|
|
55
|
-
importStyle: ImportStyleType;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export interface ModuleToolsSource extends SourceConfig {
|
|
59
|
-
jsxTransformRuntime: 'automatic' | 'classic';
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export type ModuleToolsConfig = NormalizedConfig & {
|
|
63
|
-
output: OutputConfig & ModuleToolsOutput;
|
|
64
|
-
source: NormalizedConfig['source'] & ModuleToolsSource;
|
|
65
|
-
};
|
package/src/utils/babel.ts
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
getBabelConfig,
|
|
3
|
-
getModuleBabelChain,
|
|
4
|
-
} from '@modern-js/babel-preset-module';
|
|
5
|
-
import { TransformOptions } from '@babel/core';
|
|
6
|
-
import { applyOptionsChain, getAlias } from '@modern-js/utils';
|
|
7
|
-
import { NormalizedConfig } from '@modern-js/core';
|
|
8
|
-
import type { BabelChain } from '@modern-js/babel-chain';
|
|
9
|
-
import { IPackageModeValue, ModuleToolsConfig } from '../types';
|
|
10
|
-
|
|
11
|
-
export const getFinalAlias: any = (
|
|
12
|
-
modernConfig: NormalizedConfig,
|
|
13
|
-
option: { appDirectory: string; tsconfigPath: string; sourceAbsDir: string },
|
|
14
|
-
) => {
|
|
15
|
-
const aliasConfig = getAlias(modernConfig.source.alias, option);
|
|
16
|
-
// 排除内部别名,因为不需要处理
|
|
17
|
-
const finalPaths: Record<string, string | string[]> = {};
|
|
18
|
-
const internalAliasPrefix = '@modern-js/runtime';
|
|
19
|
-
if (aliasConfig.paths && typeof aliasConfig.paths === 'object') {
|
|
20
|
-
const pathsKey = Object.keys(aliasConfig.paths);
|
|
21
|
-
for (const key of pathsKey) {
|
|
22
|
-
if (!key.includes(internalAliasPrefix)) {
|
|
23
|
-
finalPaths[key] = aliasConfig.paths[key];
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
aliasConfig.paths = finalPaths;
|
|
29
|
-
return aliasConfig;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export const resolveBabelConfig = (
|
|
33
|
-
appDirectory: string,
|
|
34
|
-
modernConfig: NormalizedConfig,
|
|
35
|
-
option: Pick<IPackageModeValue, 'syntax' | 'type'> & {
|
|
36
|
-
sourceAbsDir: string;
|
|
37
|
-
tsconfigPath: string;
|
|
38
|
-
},
|
|
39
|
-
) => {
|
|
40
|
-
const {
|
|
41
|
-
source: { envVars, globalVars, jsxTransformRuntime = 'automatic' },
|
|
42
|
-
output: { importStyle },
|
|
43
|
-
tools: { lodash: userLodashOption },
|
|
44
|
-
} = modernConfig as ModuleToolsConfig;
|
|
45
|
-
|
|
46
|
-
// alias config
|
|
47
|
-
const aliasConfig = getFinalAlias(modernConfig, {
|
|
48
|
-
appDirectory,
|
|
49
|
-
...option,
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
// lodash config
|
|
53
|
-
const lodashOptions = applyOptionsChain(
|
|
54
|
-
{ id: ['lodash', 'ramda'] },
|
|
55
|
-
// TODO: 需要处理类型问题
|
|
56
|
-
userLodashOption as any,
|
|
57
|
-
);
|
|
58
|
-
// babel config
|
|
59
|
-
const internalBabelConfig = getBabelConfig(
|
|
60
|
-
{
|
|
61
|
-
appDirectory,
|
|
62
|
-
enableReactPreset: true,
|
|
63
|
-
enableTypescriptPreset: true,
|
|
64
|
-
alias: aliasConfig,
|
|
65
|
-
envVars,
|
|
66
|
-
globalVars,
|
|
67
|
-
lodashOptions,
|
|
68
|
-
jsxTransformRuntime,
|
|
69
|
-
importStyle,
|
|
70
|
-
},
|
|
71
|
-
{
|
|
72
|
-
type: option.type,
|
|
73
|
-
syntax: option.syntax,
|
|
74
|
-
},
|
|
75
|
-
);
|
|
76
|
-
|
|
77
|
-
// Preventing warning when files are too large
|
|
78
|
-
internalBabelConfig.compact = false;
|
|
79
|
-
|
|
80
|
-
const babelChain = getModuleBabelChain(
|
|
81
|
-
{
|
|
82
|
-
appDirectory,
|
|
83
|
-
enableReactPreset: true,
|
|
84
|
-
enableTypescriptPreset: true,
|
|
85
|
-
alias: aliasConfig,
|
|
86
|
-
envVars,
|
|
87
|
-
globalVars,
|
|
88
|
-
lodashOptions,
|
|
89
|
-
jsxTransformRuntime,
|
|
90
|
-
importStyle,
|
|
91
|
-
},
|
|
92
|
-
{
|
|
93
|
-
type: option.type,
|
|
94
|
-
syntax: option.syntax,
|
|
95
|
-
},
|
|
96
|
-
);
|
|
97
|
-
const userBabelConfig = modernConfig.tools.babel;
|
|
98
|
-
return applyOptionsChain<TransformOptions, { chain: BabelChain }>(
|
|
99
|
-
internalBabelConfig,
|
|
100
|
-
// TODO: 感觉 userBabelConfig 的类型应该是TransformOptions
|
|
101
|
-
userBabelConfig as any,
|
|
102
|
-
{ chain: babelChain },
|
|
103
|
-
);
|
|
104
|
-
};
|
package/src/utils/color.ts
DELETED
package/src/utils/copy.ts
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import * as path from 'path';
|
|
2
|
-
import { fs, Import } from '@modern-js/utils';
|
|
3
|
-
import type { NormalizedConfig, IAppContext } from '@modern-js/core';
|
|
4
|
-
|
|
5
|
-
const globby: typeof import('globby') = Import.lazy('globby', require);
|
|
6
|
-
const fastGlob: typeof import('fast-glob') = Import.lazy('fast-glob', require);
|
|
7
|
-
const normalizePath: typeof import('normalize-path') = Import.lazy(
|
|
8
|
-
'normalize-path',
|
|
9
|
-
require,
|
|
10
|
-
);
|
|
11
|
-
|
|
12
|
-
// eslint-disable-next-line max-statements
|
|
13
|
-
export const copyTask = async (option: {
|
|
14
|
-
modernConfig: NormalizedConfig;
|
|
15
|
-
appContext: IAppContext;
|
|
16
|
-
}) => {
|
|
17
|
-
const { modernConfig, appContext } = option;
|
|
18
|
-
const { appDirectory } = appContext;
|
|
19
|
-
const {
|
|
20
|
-
output: { copy, path: outputPath = 'dist', jsPath = 'js' },
|
|
21
|
-
} = modernConfig;
|
|
22
|
-
const copyDistDir = path.join(outputPath, jsPath);
|
|
23
|
-
|
|
24
|
-
if (!copy) {
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
try {
|
|
28
|
-
// 类型暂时这样处理,待之后优化copy的逻辑
|
|
29
|
-
for (const copyOption of copy as any) {
|
|
30
|
-
// 在原来的基础上,引入了类似于 copy-webpck-plugin 的 context 属性,可以设置项目根路径
|
|
31
|
-
const {
|
|
32
|
-
context = appDirectory,
|
|
33
|
-
from,
|
|
34
|
-
globOptions,
|
|
35
|
-
to: toOrigin,
|
|
36
|
-
} = copyOption;
|
|
37
|
-
let fromOrigin = from;
|
|
38
|
-
// 获取 glob 规则
|
|
39
|
-
let glob = '';
|
|
40
|
-
let options = {};
|
|
41
|
-
if (fromOrigin !== null && typeof fromOrigin === 'object') {
|
|
42
|
-
({ glob, ...options } = fromOrigin);
|
|
43
|
-
fromOrigin = glob;
|
|
44
|
-
} else if (globOptions && typeof globOptions === 'object') {
|
|
45
|
-
options = globOptions;
|
|
46
|
-
}
|
|
47
|
-
glob = path.isAbsolute(fromOrigin)
|
|
48
|
-
? fromOrigin
|
|
49
|
-
: path.posix.join(
|
|
50
|
-
fastGlob.escapePath(normalizePath(path.resolve(context))),
|
|
51
|
-
fromOrigin,
|
|
52
|
-
);
|
|
53
|
-
// 计算 glob,获取目标文件
|
|
54
|
-
const paths = await globby(glob, options);
|
|
55
|
-
if (!paths.length) {
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
const to = path.normalize(toOrigin ? toOrigin : '');
|
|
59
|
-
const isToDirectory = path.extname(to) === '';
|
|
60
|
-
paths.forEach(item => {
|
|
61
|
-
const relativeFrom = path.relative(context, item);
|
|
62
|
-
// 如果 to 是目录,通过相对路径计算完整的产物路径;如果 to 是文件,直接作为最终产物路径
|
|
63
|
-
const fileName = isToDirectory ? path.join(to, relativeFrom) : to;
|
|
64
|
-
const finalToPath = path.resolve(copyDistDir, fileName);
|
|
65
|
-
fs.copySync(item, finalToPath);
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
} catch (e: any) {
|
|
69
|
-
console.error(`copy error: ${e.message}`);
|
|
70
|
-
}
|
|
71
|
-
};
|
package/src/utils/init-env.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
interface ITaskConfig {
|
|
2
|
-
srcRootDir: string; // 源码的根目录
|
|
3
|
-
willCompilerDirOrFile: string; // 用于编译的源码文件或者源码目录
|
|
4
|
-
distDir: string;
|
|
5
|
-
appDirectory: string;
|
|
6
|
-
sourceMaps: boolean;
|
|
7
|
-
syntax: 'es5' | 'es6+';
|
|
8
|
-
type: 'module' | 'commonjs';
|
|
9
|
-
tsconfigPath: string;
|
|
10
|
-
copyDirs?: string;
|
|
11
|
-
compiler?: 'babel' | 'esbuild' | 'swc';
|
|
12
|
-
watch: boolean;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const initEnv = ({
|
|
16
|
-
syntax,
|
|
17
|
-
type,
|
|
18
|
-
}: {
|
|
19
|
-
syntax: ITaskConfig['syntax'];
|
|
20
|
-
type: ITaskConfig['type'];
|
|
21
|
-
}) => {
|
|
22
|
-
if (syntax === 'es6+' && type === 'commonjs') {
|
|
23
|
-
return 'CJS_ES6';
|
|
24
|
-
} else if (syntax === 'es6+' && type === 'module') {
|
|
25
|
-
return 'ESM_ES6';
|
|
26
|
-
} else if (syntax === 'es5' && type === 'module') {
|
|
27
|
-
return 'ESM_ES5';
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
return '';
|
|
31
|
-
};
|
package/src/utils/json.ts
DELETED
package/src/utils/language.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { Import } from '@modern-js/utils';
|
|
2
|
-
|
|
3
|
-
const i18n: typeof import('@modern-js/i18n-cli-language-detector') =
|
|
4
|
-
Import.lazy('@modern-js/i18n-cli-language-detector', require);
|
|
5
|
-
|
|
6
|
-
export function getLocaleLanguage() {
|
|
7
|
-
const detector = new i18n.I18CLILanguageDetector();
|
|
8
|
-
return detector.detect();
|
|
9
|
-
}
|
package/src/utils/logger.ts
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import type { ChildProcess } from 'child_process';
|
|
2
|
-
import EventEmitter from 'events';
|
|
3
|
-
import { Import } from '@modern-js/utils';
|
|
4
|
-
|
|
5
|
-
const chalk: typeof import('chalk') = Import.lazy('chalk', require);
|
|
6
|
-
|
|
7
|
-
export const clearFlag = '\x1Bc';
|
|
8
|
-
|
|
9
|
-
export const logTemplate = (
|
|
10
|
-
title: string,
|
|
11
|
-
messageStack: string[],
|
|
12
|
-
maxLength: number,
|
|
13
|
-
{
|
|
14
|
-
noBottomBorder = false,
|
|
15
|
-
bottomBorderText = '',
|
|
16
|
-
noLeftBorder = false,
|
|
17
|
-
leftBorder = '│',
|
|
18
|
-
contentColor = (s: string) => s,
|
|
19
|
-
} = {},
|
|
20
|
-
) => {
|
|
21
|
-
const leftBorderFlag = noLeftBorder ? '' : leftBorder;
|
|
22
|
-
const messageFragments = messageStack
|
|
23
|
-
.map(p => {
|
|
24
|
-
p.trim();
|
|
25
|
-
|
|
26
|
-
return `${leftBorderFlag}${p.replace(clearFlag, '')}`;
|
|
27
|
-
}) // 移除 clearFlag
|
|
28
|
-
.filter(s => s !== leftBorderFlag) // 过滤空字符串
|
|
29
|
-
.slice(0, maxLength) // 控制长度
|
|
30
|
-
.reverse(); // 调换顺序,最新的消息在最后面
|
|
31
|
-
messageFragments[messageFragments.length - 1] =
|
|
32
|
-
messageFragments[messageFragments.length - 1].trim();
|
|
33
|
-
const template = `${title}:
|
|
34
|
-
${contentColor(messageFragments.join(''))}${
|
|
35
|
-
noBottomBorder ? '' : `\n${bottomBorderText}`
|
|
36
|
-
}`;
|
|
37
|
-
console.info('template', messageFragments);
|
|
38
|
-
return template;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
export type STDOUT = ChildProcess['stdout'] | NodeJS.ReadStream;
|
|
42
|
-
export type STDERR = ChildProcess['stdout'] | NodeJS.ReadStream;
|
|
43
|
-
|
|
44
|
-
export interface LoggerTextOption {
|
|
45
|
-
title: string;
|
|
46
|
-
maxLength: number;
|
|
47
|
-
contentConfig?: {
|
|
48
|
-
noBottomBorder?: boolean;
|
|
49
|
-
bottomBorderText?: string;
|
|
50
|
-
noLeftBorder?: boolean;
|
|
51
|
-
leftBorder?: string;
|
|
52
|
-
contentColor?: (s: string) => string;
|
|
53
|
-
replace?: string[];
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export class LoggerText {
|
|
58
|
-
messages: string[];
|
|
59
|
-
|
|
60
|
-
option: LoggerTextOption;
|
|
61
|
-
|
|
62
|
-
constructor(option: LoggerTextOption) {
|
|
63
|
-
this.messages = [];
|
|
64
|
-
this.option = option;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
append(message: string) {
|
|
68
|
-
const replace = this.option.contentConfig?.replace || [];
|
|
69
|
-
let content = message;
|
|
70
|
-
for (const r of replace) {
|
|
71
|
-
content = content.replace(new RegExp(r, 'g'), '');
|
|
72
|
-
}
|
|
73
|
-
this.messages.push(content);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
get value() {
|
|
77
|
-
const { title, maxLength, contentConfig } = this.option;
|
|
78
|
-
const messages = [...new Set(this.messages)];
|
|
79
|
-
return logTemplate(title, messages, maxLength, contentConfig);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
interface IAddStdoutConfig {
|
|
84
|
-
event?: { data?: boolean; error?: boolean };
|
|
85
|
-
colors?: {
|
|
86
|
-
data?: (s: string) => string;
|
|
87
|
-
error?: (s: string) => string;
|
|
88
|
-
warning?: (s: string) => string;
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export class LoggerManager extends EventEmitter {
|
|
93
|
-
// constructor() {}
|
|
94
|
-
|
|
95
|
-
createLoggerText(option: LoggerTextOption) {
|
|
96
|
-
return new LoggerText(option);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
addStdout(
|
|
100
|
-
loggerText: LoggerText,
|
|
101
|
-
stdout: STDOUT,
|
|
102
|
-
config: IAddStdoutConfig = {},
|
|
103
|
-
) {
|
|
104
|
-
const {
|
|
105
|
-
event = { data: true, error: true },
|
|
106
|
-
colors = {
|
|
107
|
-
data: chalk.green,
|
|
108
|
-
error: chalk.red,
|
|
109
|
-
warning: chalk.yellow,
|
|
110
|
-
},
|
|
111
|
-
} = config;
|
|
112
|
-
if (event.data) {
|
|
113
|
-
stdout?.on('data', chunk => {
|
|
114
|
-
const data = chunk.toString();
|
|
115
|
-
const content = colors.data ? colors.data(data) : chalk.green(data);
|
|
116
|
-
loggerText.append(content);
|
|
117
|
-
this.emit('data');
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
if (event.error) {
|
|
122
|
-
stdout?.on('error', error => {
|
|
123
|
-
const data = error.message;
|
|
124
|
-
const content = colors.error ? colors.error(data) : chalk.red(data);
|
|
125
|
-
loggerText.append(content);
|
|
126
|
-
this.emit('data');
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
addStderr(loggerText: LoggerText, stderr: STDERR) {
|
|
132
|
-
stderr?.on('data', chunk => {
|
|
133
|
-
const data = chunk.toString();
|
|
134
|
-
loggerText.append(data);
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
show(loggerText: LoggerText) {
|
|
139
|
-
console.info(loggerText.value);
|
|
140
|
-
}
|
|
141
|
-
}
|
package/src/utils/readline.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import * as readline from 'readline';
|
|
2
|
-
|
|
3
|
-
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
|
|
4
|
-
export class ReadlineUtils {
|
|
5
|
-
static clearPrevLine(
|
|
6
|
-
output: NodeJS.WriteStream & {
|
|
7
|
-
fd: 1;
|
|
8
|
-
},
|
|
9
|
-
) {
|
|
10
|
-
ReadlineUtils.clearLine(output, 1);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
static clearLine(
|
|
14
|
-
output: NodeJS.WriteStream & {
|
|
15
|
-
fd: 1;
|
|
16
|
-
},
|
|
17
|
-
n = 1,
|
|
18
|
-
dir: 1 | -1 = -1,
|
|
19
|
-
) {
|
|
20
|
-
// -1 向上,1 向下
|
|
21
|
-
const dx = dir === 1 ? 1 : -1;
|
|
22
|
-
for (let i = 0; i < n; i++) {
|
|
23
|
-
readline.moveCursor(output, 0, dx);
|
|
24
|
-
readline.clearLine(output, 0);
|
|
25
|
-
readline.cursorTo(output, 0);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
package/src/utils/tsconfig.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { fs, Import } from '@modern-js/utils';
|
|
2
|
-
|
|
3
|
-
const json5: typeof import('json5') = Import.lazy('json5', require);
|
|
4
|
-
|
|
5
|
-
export interface ITsconfig {
|
|
6
|
-
compilerOptions?:
|
|
7
|
-
| {
|
|
8
|
-
rootDir?: string;
|
|
9
|
-
baseUrl?: string;
|
|
10
|
-
declaration?: boolean;
|
|
11
|
-
emitDeclarationOnly?: boolean;
|
|
12
|
-
isolatedModules?: boolean;
|
|
13
|
-
allowJs?: boolean;
|
|
14
|
-
outDir?: string;
|
|
15
|
-
paths?: Record<string, string[]>;
|
|
16
|
-
}
|
|
17
|
-
| undefined;
|
|
18
|
-
include?: string[];
|
|
19
|
-
exclude?: string[];
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const readTsConfig = <T extends null | ITsconfig>(
|
|
23
|
-
tsconfigPath: string,
|
|
24
|
-
noExistReturn: T = null as T,
|
|
25
|
-
): ITsconfig | T => {
|
|
26
|
-
// 如果不存在,则返回 noExistReturn
|
|
27
|
-
if (!fs.existsSync(tsconfigPath)) {
|
|
28
|
-
return noExistReturn;
|
|
29
|
-
}
|
|
30
|
-
const content = fs.readFileSync(tsconfigPath, 'utf-8');
|
|
31
|
-
return json5.parse(content);
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
export const existTsConfigFile = (tsconfigAbsolutePath: string) => {
|
|
35
|
-
const tsconfig = readTsConfig(tsconfigAbsolutePath);
|
|
36
|
-
return Boolean(tsconfig);
|
|
37
|
-
};
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
export const defaultTransformedFunctions = [
|
|
2
|
-
'require',
|
|
3
|
-
'require.resolve',
|
|
4
|
-
'System.import',
|
|
5
|
-
|
|
6
|
-
// Jest methods
|
|
7
|
-
'jest.genMockFromModule',
|
|
8
|
-
'jest.mock',
|
|
9
|
-
'jest.unmock',
|
|
10
|
-
'jest.doMock',
|
|
11
|
-
'jest.dontMock',
|
|
12
|
-
'jest.setMock',
|
|
13
|
-
'jest.requireActual',
|
|
14
|
-
'jest.requireMock',
|
|
15
|
-
|
|
16
|
-
// Older Jest methods
|
|
17
|
-
'require.requireActual',
|
|
18
|
-
'require.requireMock',
|
|
19
|
-
];
|
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
import * as path from 'path';
|
|
2
|
-
import * as parser from '@babel/parser';
|
|
3
|
-
import traverse, { NodePath } from '@babel/traverse';
|
|
4
|
-
import generator from '@babel/generator';
|
|
5
|
-
import * as t from '@babel/types';
|
|
6
|
-
import { createMatchPath } from 'tsconfig-paths';
|
|
7
|
-
import { fs } from '@modern-js/utils';
|
|
8
|
-
import { defaultTransformedFunctions } from './constants';
|
|
9
|
-
import { matchesPattern, isImportCall } from './utils';
|
|
10
|
-
|
|
11
|
-
export interface TransformOption {
|
|
12
|
-
filename: string;
|
|
13
|
-
baseUrl: string;
|
|
14
|
-
paths: Record<string, string[] | string>;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const getPathsMap = (
|
|
18
|
-
paths: Record<string, string | string[]>,
|
|
19
|
-
sourceDirName = 'src',
|
|
20
|
-
) => {
|
|
21
|
-
const pathKeys = Object.keys(paths);
|
|
22
|
-
const pathsMap: Record<string, string[]> = {};
|
|
23
|
-
const replaceSrcToTypes = (s: string) => s.replace(sourceDirName, 'types');
|
|
24
|
-
for (const key of pathKeys) {
|
|
25
|
-
const p = paths[key];
|
|
26
|
-
if (typeof p === 'string') {
|
|
27
|
-
pathsMap[key] = [replaceSrcToTypes(p)];
|
|
28
|
-
} else {
|
|
29
|
-
pathsMap[key] = (paths[key] as string[]).map(sp => replaceSrcToTypes(sp));
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
return pathsMap;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
function mapPathString(
|
|
36
|
-
nodePath: NodePath<t.StringLiteral>,
|
|
37
|
-
{ filename, baseUrl, paths }: TransformOption,
|
|
38
|
-
) {
|
|
39
|
-
if (!t.isStringLiteral(nodePath)) {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const sourcePath = nodePath.node.value;
|
|
44
|
-
const currentFile = filename;
|
|
45
|
-
const pathsMap = getPathsMap(paths);
|
|
46
|
-
const matchPath = createMatchPath(baseUrl, pathsMap, ['index']);
|
|
47
|
-
const result = matchPath(
|
|
48
|
-
sourcePath,
|
|
49
|
-
packageJsonPath => {
|
|
50
|
-
if (!fs.existsSync(packageJsonPath)) {
|
|
51
|
-
return undefined;
|
|
52
|
-
}
|
|
53
|
-
return fs.readJSONSync(packageJsonPath);
|
|
54
|
-
},
|
|
55
|
-
filePath => fs.existsSync(filePath),
|
|
56
|
-
['.d.ts'],
|
|
57
|
-
);
|
|
58
|
-
if (result) {
|
|
59
|
-
const relativePath = path.relative(
|
|
60
|
-
path.dirname(currentFile),
|
|
61
|
-
path.dirname(result),
|
|
62
|
-
);
|
|
63
|
-
const fileName = path.basename(result);
|
|
64
|
-
// 如果是同级文件,则返回的是 ''
|
|
65
|
-
const filePath = path.normalize(
|
|
66
|
-
`${relativePath.length === 0 ? '.' : relativePath}/${fileName}`,
|
|
67
|
-
);
|
|
68
|
-
const replaceString = filePath.startsWith('.') ? filePath : `./${filePath}`;
|
|
69
|
-
nodePath.replaceWith(t.stringLiteral(replaceString));
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const transformCall =
|
|
74
|
-
(option: TransformOption) => (nodePath: NodePath<t.CallExpression>) => {
|
|
75
|
-
const calleePath = nodePath.get('callee') as NodePath;
|
|
76
|
-
const isNormalCall = defaultTransformedFunctions.some(pattern =>
|
|
77
|
-
matchesPattern(calleePath, pattern),
|
|
78
|
-
);
|
|
79
|
-
if (isNormalCall || isImportCall(nodePath)) {
|
|
80
|
-
mapPathString(
|
|
81
|
-
nodePath.get('arguments.0') as NodePath<t.StringLiteral>,
|
|
82
|
-
option,
|
|
83
|
-
);
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
const transformImport =
|
|
88
|
-
(option: TransformOption) => (nodePath: NodePath<t.ImportDeclaration>) => {
|
|
89
|
-
mapPathString(nodePath.get('source'), option);
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
const transformExport =
|
|
93
|
-
(option: TransformOption) => (nodePath: NodePath<t.ExportDeclaration>) => {
|
|
94
|
-
mapPathString(nodePath.get('source') as NodePath<t.StringLiteral>, option);
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
const transfromSingleFileAlias = ({
|
|
98
|
-
filename,
|
|
99
|
-
baseUrl,
|
|
100
|
-
paths,
|
|
101
|
-
}: TransformOption) => {
|
|
102
|
-
const sourceCode = fs.readFileSync(filename, 'utf-8');
|
|
103
|
-
const ast = parser.parse(sourceCode, {
|
|
104
|
-
sourceType: 'module',
|
|
105
|
-
errorRecovery: true, // 防止typescript不支持的语法出现而报错
|
|
106
|
-
plugins: ['typescript'],
|
|
107
|
-
});
|
|
108
|
-
traverse(ast as any, {
|
|
109
|
-
CallExpression: transformCall({ filename, baseUrl, paths }) as any,
|
|
110
|
-
ImportDeclaration: transformImport({
|
|
111
|
-
filename,
|
|
112
|
-
baseUrl,
|
|
113
|
-
paths,
|
|
114
|
-
}) as any,
|
|
115
|
-
ExportDeclaration: transformExport({
|
|
116
|
-
filename,
|
|
117
|
-
baseUrl,
|
|
118
|
-
paths,
|
|
119
|
-
}) as any,
|
|
120
|
-
});
|
|
121
|
-
return generator(ast as any).code;
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
interface TransformDtsAlaisOption {
|
|
125
|
-
filenames?: string[];
|
|
126
|
-
baseUrl: string;
|
|
127
|
-
paths: Record<string, string[] | string>;
|
|
128
|
-
}
|
|
129
|
-
export const transformDtsAlias = (option: TransformDtsAlaisOption) => {
|
|
130
|
-
const { filenames = [], baseUrl, paths } = option;
|
|
131
|
-
const transformResult: { path: string; content: string }[] = [];
|
|
132
|
-
for (const filename of filenames) {
|
|
133
|
-
transformResult.push({
|
|
134
|
-
path: filename,
|
|
135
|
-
content: transfromSingleFileAlias({ filename, baseUrl, paths }),
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
return transformResult;
|
|
139
|
-
};
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { Import } from '@modern-js/utils';
|
|
2
|
-
import type * as tt from '@babel/types';
|
|
3
|
-
import type { NodePath } from '@babel/traverse';
|
|
4
|
-
|
|
5
|
-
const t: typeof import('@babel/types') = Import.lazy('@babel/types', require);
|
|
6
|
-
|
|
7
|
-
export function matchesPattern(calleePath: NodePath, pattern: string) {
|
|
8
|
-
const { node } = calleePath;
|
|
9
|
-
|
|
10
|
-
if (t.isMemberExpression(node)) {
|
|
11
|
-
return calleePath.matchesPattern(pattern);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
if (!t.isIdentifier(node) || pattern.includes('.')) {
|
|
15
|
-
return false;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const name = pattern.split('.')[0];
|
|
19
|
-
|
|
20
|
-
return node.name === name;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function isImportCall(calleePath: NodePath<tt.CallExpression>) {
|
|
24
|
-
return t.isImport(calleePath.node.callee);
|
|
25
|
-
}
|