@modern-js/module-tools 1.3.0 → 1.4.0
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/CHANGELOG.md +55 -0
- package/dist/js/modern/features/dev/index.js +1 -1
- package/dist/js/node/features/dev/index.js +1 -1
- package/dist/types/index.d.ts +2 -0
- package/lib/types.d.ts +0 -10
- package/package.json +16 -17
- package/tests/dev-feature.test.ts +17 -1
- 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 -52
- 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 -74
- 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/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
|
-
}
|
package/src/utils/valide.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { validAlias } from '@modern-js/utils';
|
|
2
|
-
import type { NormalizedConfig } from '@modern-js/core';
|
|
3
|
-
|
|
4
|
-
export interface IValideOption {
|
|
5
|
-
modernConfig: NormalizedConfig;
|
|
6
|
-
tsconfigPath: string;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export const valideBeforeTask = ({
|
|
10
|
-
modernConfig,
|
|
11
|
-
tsconfigPath,
|
|
12
|
-
}: IValideOption) => {
|
|
13
|
-
const modernConfigValidResult = modernConfigValid(modernConfig, {
|
|
14
|
-
tsconfigPath,
|
|
15
|
-
});
|
|
16
|
-
if (modernConfigValidResult) {
|
|
17
|
-
console.error(modernConfigValidResult);
|
|
18
|
-
// eslint-disable-next-line no-process-exit
|
|
19
|
-
process.exit(0);
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export const modernConfigValid = (
|
|
24
|
-
modernConfig: NormalizedConfig,
|
|
25
|
-
option: { tsconfigPath: string },
|
|
26
|
-
) => {
|
|
27
|
-
const valids = [validAlias];
|
|
28
|
-
|
|
29
|
-
for (const validFn of valids) {
|
|
30
|
-
const result = validFn(modernConfig, option);
|
|
31
|
-
if (result) {
|
|
32
|
-
return result;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return null;
|
|
37
|
-
};
|