@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
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
/* eslint-disable max-statements */
|
|
2
|
-
import * as path from 'path';
|
|
3
|
-
import * as os from 'os';
|
|
4
|
-
import { Import } from '@modern-js/utils';
|
|
5
|
-
import type { NormalizedConfig } from '@modern-js/core';
|
|
6
|
-
import type { IBuildConfig, ITaskMapper } from '../../types';
|
|
7
|
-
|
|
8
|
-
const pMap: typeof import('p-map') = Import.lazy('p-map', require);
|
|
9
|
-
const utils: typeof import('./utils') = Import.lazy('./utils', require);
|
|
10
|
-
const execa: typeof import('execa') = Import.lazy('execa', require);
|
|
11
|
-
const lg: typeof import('./logger') = Import.lazy('./logger', require);
|
|
12
|
-
const constants: typeof import('./constants') = Import.lazy(
|
|
13
|
-
'./constants',
|
|
14
|
-
require,
|
|
15
|
-
);
|
|
16
|
-
const core: typeof import('@modern-js/core') = Import.lazy(
|
|
17
|
-
'@modern-js/core',
|
|
18
|
-
require,
|
|
19
|
-
);
|
|
20
|
-
|
|
21
|
-
export const buildSourceCode = async (
|
|
22
|
-
config: IBuildConfig,
|
|
23
|
-
_: NormalizedConfig,
|
|
24
|
-
) => {
|
|
25
|
-
const { sourceDir, enableTscCompiler } = config;
|
|
26
|
-
const { appDirectory } = core.useAppContext();
|
|
27
|
-
const concurrency = os.cpus().length;
|
|
28
|
-
const srcRootDir = path.join(appDirectory, sourceDir);
|
|
29
|
-
const lm = new lg.LoggerManager();
|
|
30
|
-
const codeLog = lm.createLoggerText({
|
|
31
|
-
title: constants.runBabelCompilerTitle,
|
|
32
|
-
});
|
|
33
|
-
const dtsLog = lm.createLoggerText({ title: constants.runTscTitle });
|
|
34
|
-
const styleLog = lm.createLoggerText({
|
|
35
|
-
title: constants.runStyleCompilerTitle,
|
|
36
|
-
});
|
|
37
|
-
const copyLog = lm.createLoggerText({ title: 'Copy Log:' });
|
|
38
|
-
const initCodeMapper = utils.getCodeInitMapper(config);
|
|
39
|
-
const taskMapper: ITaskMapper[] = [
|
|
40
|
-
...utils.getCodeMapper({
|
|
41
|
-
logger: codeLog,
|
|
42
|
-
taskPath: require.resolve('../../tasks/build-source-code'),
|
|
43
|
-
config,
|
|
44
|
-
willCompilerDirOrFile: sourceDir,
|
|
45
|
-
initMapper: initCodeMapper,
|
|
46
|
-
srcRootDir,
|
|
47
|
-
}),
|
|
48
|
-
...(enableTscCompiler ? utils.getDtsMapper(config, dtsLog) : []),
|
|
49
|
-
{
|
|
50
|
-
logger: styleLog,
|
|
51
|
-
taskPath: require.resolve('../../tasks/build-style'),
|
|
52
|
-
},
|
|
53
|
-
{
|
|
54
|
-
logger: copyLog,
|
|
55
|
-
taskPath: require.resolve('../../tasks/copy-assets'),
|
|
56
|
-
},
|
|
57
|
-
];
|
|
58
|
-
|
|
59
|
-
lm.showCompiling();
|
|
60
|
-
await pMap(
|
|
61
|
-
taskMapper,
|
|
62
|
-
async ({ logger, taskPath, params }) => {
|
|
63
|
-
const childProcess = execa.node(taskPath, params, { stdio: 'pipe' });
|
|
64
|
-
|
|
65
|
-
if (logger === codeLog || logger === copyLog) {
|
|
66
|
-
lm.addStdout(logger, childProcess.stdout, {
|
|
67
|
-
event: { data: true, error: true },
|
|
68
|
-
});
|
|
69
|
-
lm.addStderr(logger, childProcess.stderr);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (logger === dtsLog) {
|
|
73
|
-
lm.addStdout(dtsLog, childProcess.stdout, {
|
|
74
|
-
event: { data: true, error: true },
|
|
75
|
-
});
|
|
76
|
-
lm.addStderr(dtsLog, childProcess.stderr);
|
|
77
|
-
}
|
|
78
|
-
if (logger === styleLog) {
|
|
79
|
-
lm.addStdout(logger, childProcess.stdout, {
|
|
80
|
-
event: { data: true, error: true },
|
|
81
|
-
});
|
|
82
|
-
lm.addStderr(logger, childProcess.stderr);
|
|
83
|
-
}
|
|
84
|
-
await childProcess;
|
|
85
|
-
},
|
|
86
|
-
{ concurrency },
|
|
87
|
-
);
|
|
88
|
-
|
|
89
|
-
lm.disappearCompiling();
|
|
90
|
-
enableTscCompiler && console.info(dtsLog.value);
|
|
91
|
-
console.info(codeLog.value);
|
|
92
|
-
if (styleLog.hasMessages()) {
|
|
93
|
-
console.info(styleLog.value);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (copyLog.hasMessages()) {
|
|
97
|
-
console.info(copyLog.value);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (
|
|
101
|
-
dtsLog.hasErrorMessage ||
|
|
102
|
-
codeLog.hasErrorMessage ||
|
|
103
|
-
styleLog.hasErrorMessage ||
|
|
104
|
-
copyLog.hasErrorMessage
|
|
105
|
-
) {
|
|
106
|
-
// eslint-disable-next-line no-process-exit
|
|
107
|
-
process.exit(1);
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
/* eslint-enable max-statements */
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import type { IPackageModeValue } from '../../types';
|
|
2
|
-
|
|
3
|
-
// Universal JS 的默认选择,三份构建产物,支持 Node.js,对现代浏览器有优化
|
|
4
|
-
const universalJs: IPackageModeValue[] = [
|
|
5
|
-
{ type: 'module', syntax: 'es5', outDir: 'treeshaking' },
|
|
6
|
-
{ type: 'commonjs', syntax: 'es6+', outDir: 'node' },
|
|
7
|
-
{ type: 'module', syntax: 'es6+', outDir: 'modern' },
|
|
8
|
-
];
|
|
9
|
-
|
|
10
|
-
// Universal JS 的优化选择,两份构建产物,对现代浏览器无优化
|
|
11
|
-
const universalJsLite: IPackageModeValue[] = [
|
|
12
|
-
{ type: 'module', syntax: 'es5', outDir: 'treeshaking' },
|
|
13
|
-
{ type: 'commonjs', syntax: 'es6+', outDir: 'node', copyDirs: ['modern'] },
|
|
14
|
-
];
|
|
15
|
-
|
|
16
|
-
// 纯前端代码的默认选择,两份构建产物
|
|
17
|
-
const browserJs: IPackageModeValue[] = [
|
|
18
|
-
{ type: 'module', syntax: 'es5', outDir: 'treeshaking', copyDirs: ['node'] },
|
|
19
|
-
{ type: 'module', syntax: 'es6+', outDir: 'modern' },
|
|
20
|
-
];
|
|
21
|
-
|
|
22
|
-
// 纯前端代码的优化选择,单份构建产物,对现代浏览器无优化
|
|
23
|
-
const browserJsLite: IPackageModeValue[] = [
|
|
24
|
-
{
|
|
25
|
-
type: 'module',
|
|
26
|
-
syntax: 'es5',
|
|
27
|
-
outDir: 'treeshaking',
|
|
28
|
-
copyDirs: ['modern', 'node'],
|
|
29
|
-
},
|
|
30
|
-
];
|
|
31
|
-
|
|
32
|
-
// 纯 Node.js 代码的默认选择,两份构建产物
|
|
33
|
-
const nodeJs: IPackageModeValue[] = [
|
|
34
|
-
{ type: 'commonjs', syntax: 'es6+', outDir: 'node' },
|
|
35
|
-
{ type: 'module', syntax: 'es6+', outDir: 'modern' },
|
|
36
|
-
];
|
|
37
|
-
|
|
38
|
-
export const DEFAULT_PACKAGE_MODE = 'universal-js';
|
|
39
|
-
|
|
40
|
-
export const PACKAGE_MODES = {
|
|
41
|
-
'universal-js': universalJs,
|
|
42
|
-
'universal-js-lite': universalJsLite,
|
|
43
|
-
'browser-js': browserJs,
|
|
44
|
-
'browser-js-lite': browserJsLite,
|
|
45
|
-
'node-js': nodeJs,
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
export const runBabelCompilerTitle = 'Run babel compiler code log';
|
|
49
|
-
export const runTscWatchTitle = 'Run `tsc -w` log';
|
|
50
|
-
export const runTscTitle = 'Run `tsc` log';
|
|
51
|
-
export const runStyleCompilerTitle = 'Run style compiler code log';
|
|
52
|
-
export const clearFlag = '\x1Bc';
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import { Import, fs } from '@modern-js/utils';
|
|
3
|
-
import type { NormalizedConfig } from '@modern-js/core';
|
|
4
|
-
import type { IBuildConfig } from '../../types';
|
|
5
|
-
|
|
6
|
-
const buildFeature: typeof import('./build') = Import.lazy('./build', require);
|
|
7
|
-
const buildWatchFeature: typeof import('./build-watch') = Import.lazy(
|
|
8
|
-
'./build-watch',
|
|
9
|
-
require,
|
|
10
|
-
);
|
|
11
|
-
const bp: typeof import('./build-platform') = Import.lazy(
|
|
12
|
-
'./build-platform',
|
|
13
|
-
require,
|
|
14
|
-
);
|
|
15
|
-
|
|
16
|
-
export const build = async (
|
|
17
|
-
config: IBuildConfig,
|
|
18
|
-
modernConfig: NormalizedConfig,
|
|
19
|
-
) => {
|
|
20
|
-
const {
|
|
21
|
-
appDirectory,
|
|
22
|
-
enableWatchMode,
|
|
23
|
-
platform,
|
|
24
|
-
clear = true,
|
|
25
|
-
isTsProject,
|
|
26
|
-
} = config;
|
|
27
|
-
const {
|
|
28
|
-
output: { path: outputPath = 'dist' },
|
|
29
|
-
} = modernConfig;
|
|
30
|
-
// TODO: maybe need watch mode in build platform
|
|
31
|
-
if (typeof platform === 'boolean' && platform) {
|
|
32
|
-
if (process.env.RUN_PLATFORM) {
|
|
33
|
-
await bp.buildPlatform({ platform: 'all', isTsProject });
|
|
34
|
-
}
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (typeof platform === 'string') {
|
|
39
|
-
if (process.env.RUN_PLATFORM) {
|
|
40
|
-
await bp.buildPlatform({ platform, isTsProject });
|
|
41
|
-
}
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (clear) {
|
|
46
|
-
fs.removeSync(path.join(appDirectory, outputPath));
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if (enableWatchMode) {
|
|
50
|
-
await buildWatchFeature.buildInWatchMode(config, modernConfig);
|
|
51
|
-
} else {
|
|
52
|
-
await buildFeature.buildSourceCode(config, modernConfig);
|
|
53
|
-
}
|
|
54
|
-
};
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { chalk } from '@modern-js/utils';
|
|
2
|
-
|
|
3
|
-
export const colors = { title: chalk.rgb(218, 152, 92) };
|
|
4
|
-
|
|
5
|
-
export const clearFlag = '\x1Bc';
|
|
6
|
-
|
|
7
|
-
export const logTemplate = (
|
|
8
|
-
title: string,
|
|
9
|
-
messageStack: string[],
|
|
10
|
-
{
|
|
11
|
-
noBottomBorder = false,
|
|
12
|
-
bottomBorderText = '',
|
|
13
|
-
noLeftBorder = false,
|
|
14
|
-
leftBorder = '',
|
|
15
|
-
contentColor = (s: string) => s,
|
|
16
|
-
} = {},
|
|
17
|
-
) => {
|
|
18
|
-
const maxLength = Infinity; // TODO: 考虑后面是否提供该参数
|
|
19
|
-
const leftBorderFlag = noLeftBorder ? '' : leftBorder;
|
|
20
|
-
const messageFragments = messageStack
|
|
21
|
-
.map(p => {
|
|
22
|
-
p.trim();
|
|
23
|
-
|
|
24
|
-
return `${leftBorderFlag}${p.replace(clearFlag, '')}`;
|
|
25
|
-
}) // 移除 clearFlag
|
|
26
|
-
.filter(s => s !== leftBorderFlag) // 过滤空字符串
|
|
27
|
-
.slice(0, maxLength); // 控制长度
|
|
28
|
-
const template = `${colors.title(title)}:
|
|
29
|
-
${contentColor(messageFragments.join(''))}${
|
|
30
|
-
noBottomBorder ? '' : `\n${bottomBorderText}`
|
|
31
|
-
}`;
|
|
32
|
-
return template;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
export interface LoggerTextOption {
|
|
36
|
-
title: string;
|
|
37
|
-
contentConfig?: {
|
|
38
|
-
noBottomBorder?: boolean;
|
|
39
|
-
bottomBorderText?: string;
|
|
40
|
-
noLeftBorder?: boolean;
|
|
41
|
-
leftBorder?: string;
|
|
42
|
-
contentColor?: (s: string) => string;
|
|
43
|
-
replace?: string[];
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
// 处理Log内容如何展示
|
|
47
|
-
export class LoggerText {
|
|
48
|
-
messages: string[];
|
|
49
|
-
|
|
50
|
-
hasErrorMessage: boolean;
|
|
51
|
-
|
|
52
|
-
option: LoggerTextOption;
|
|
53
|
-
|
|
54
|
-
constructor(option: LoggerTextOption) {
|
|
55
|
-
this.messages = [];
|
|
56
|
-
this.option = option;
|
|
57
|
-
this.hasErrorMessage = false;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
append(message: string) {
|
|
61
|
-
if (message.includes(clearFlag)) {
|
|
62
|
-
this.messages = [];
|
|
63
|
-
}
|
|
64
|
-
this.messages.push(message);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
errorHappen() {
|
|
68
|
-
this.hasErrorMessage = true;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
hasMessages() {
|
|
72
|
-
return this.messages.length > 0;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
get value() {
|
|
76
|
-
const { title, contentConfig } = this.option;
|
|
77
|
-
const messages = [...new Set(this.messages)];
|
|
78
|
-
return logTemplate(title, messages, contentConfig);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 1. 注册构建任务
|
|
3
|
-
* 2. 监听各个构建任务进程中的信息:process.stdout.on('data' | 'error')
|
|
4
|
-
* 3. 分别输出内容
|
|
5
|
-
*/
|
|
6
|
-
import type { ChildProcess } from 'child_process';
|
|
7
|
-
import EventEmitter from 'events';
|
|
8
|
-
import { chalk, Import } from '@modern-js/utils';
|
|
9
|
-
import type { LoggerTextOption, LoggerText } from './logText';
|
|
10
|
-
|
|
11
|
-
const logText: typeof import('./logText') = Import.lazy('./logText', require);
|
|
12
|
-
const readline: typeof import('../../../utils/readline') = Import.lazy(
|
|
13
|
-
'../../../utils/readline',
|
|
14
|
-
require,
|
|
15
|
-
);
|
|
16
|
-
|
|
17
|
-
export type STDOUT =
|
|
18
|
-
| ChildProcess['stdout']
|
|
19
|
-
| (NodeJS.WriteStream & {
|
|
20
|
-
fd: 1;
|
|
21
|
-
});
|
|
22
|
-
export type STDERR =
|
|
23
|
-
| ChildProcess['stderr']
|
|
24
|
-
| (NodeJS.WriteStream & {
|
|
25
|
-
fd: 2;
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
interface IAddStdoutConfig {
|
|
29
|
-
event?: { data?: boolean; error?: boolean };
|
|
30
|
-
colors?: {
|
|
31
|
-
data?: (s: string) => string;
|
|
32
|
-
error?: (s: string) => string;
|
|
33
|
-
warning?: (s: string) => string;
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export class LoggerManager extends EventEmitter {
|
|
38
|
-
private _compilering: boolean;
|
|
39
|
-
|
|
40
|
-
private readonly _listeners: STDOUT[];
|
|
41
|
-
|
|
42
|
-
constructor() {
|
|
43
|
-
super();
|
|
44
|
-
this._compilering = false;
|
|
45
|
-
this._listeners = [];
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
createLoggerText(option: LoggerTextOption) {
|
|
49
|
-
return new logText.LoggerText(option);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
addStdout(
|
|
53
|
-
loggerText: LoggerText,
|
|
54
|
-
stdout: STDOUT,
|
|
55
|
-
config: IAddStdoutConfig = {},
|
|
56
|
-
) {
|
|
57
|
-
const {
|
|
58
|
-
event = { data: true, error: true },
|
|
59
|
-
colors = {
|
|
60
|
-
data: chalk.green,
|
|
61
|
-
error: chalk.red,
|
|
62
|
-
warning: chalk.yellow,
|
|
63
|
-
},
|
|
64
|
-
} = config;
|
|
65
|
-
if (event.data) {
|
|
66
|
-
stdout?.on('data', chunk => {
|
|
67
|
-
const data = chunk.toString();
|
|
68
|
-
const content = colors.data ? colors.data(data) : chalk.green(data);
|
|
69
|
-
loggerText.append(content);
|
|
70
|
-
this.emit('data');
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (event.error) {
|
|
75
|
-
stdout?.on('error', error => {
|
|
76
|
-
console.info('error');
|
|
77
|
-
const data = error.message;
|
|
78
|
-
const content = colors.error ? colors.error(data) : chalk.red(data);
|
|
79
|
-
loggerText.append(content);
|
|
80
|
-
loggerText.errorHappen();
|
|
81
|
-
this.emit('data');
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
this._listeners.push(stdout);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
addStderr(
|
|
89
|
-
loggerText: LoggerText,
|
|
90
|
-
stderr: STDERR,
|
|
91
|
-
color: (s: string) => string = chalk.red,
|
|
92
|
-
) {
|
|
93
|
-
stderr?.on('data', chunk => {
|
|
94
|
-
const data = chunk.toString();
|
|
95
|
-
loggerText.append(color(data));
|
|
96
|
-
loggerText.errorHappen();
|
|
97
|
-
this.emit('data');
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
showCompiling() {
|
|
102
|
-
if (!this._compilering) {
|
|
103
|
-
this._compilering = true;
|
|
104
|
-
console.info(chalk.green`Compiling in progress...`);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
disappearCompiling() {
|
|
109
|
-
if (this._compilering) {
|
|
110
|
-
readline.ReadlineUtils.clearLine(process.stdout);
|
|
111
|
-
this._compilering = false;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
listenDateAndShow(
|
|
116
|
-
logTexts: LoggerText[],
|
|
117
|
-
// stdout: NodeJS.WriteStream & {
|
|
118
|
-
// fd: 1;
|
|
119
|
-
// } = process.stdout,
|
|
120
|
-
) {
|
|
121
|
-
this.on('data', () => {
|
|
122
|
-
this.disappearCompiling();
|
|
123
|
-
const content = logTexts.map(logtext => logtext.value).join('');
|
|
124
|
-
// 每次更新,使用新的内容覆盖旧的内容,有几率出现内容错乱问题
|
|
125
|
-
console.info(content);
|
|
126
|
-
});
|
|
127
|
-
return () => {
|
|
128
|
-
// eslint-disable-next-line no-process-exit
|
|
129
|
-
process.exit(0);
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
}
|
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
import * as path from 'path';
|
|
2
|
-
import * as os from 'os';
|
|
3
|
-
import { Import, chalk } from '@modern-js/utils';
|
|
4
|
-
import type {
|
|
5
|
-
IBuildConfig,
|
|
6
|
-
IPackageModeValue,
|
|
7
|
-
JsSyntaxType,
|
|
8
|
-
ITaskMapper,
|
|
9
|
-
ModuleToolsConfig,
|
|
10
|
-
} from '../../types';
|
|
11
|
-
import type { LoggerText } from './logger';
|
|
12
|
-
|
|
13
|
-
const constants: typeof import('./constants') = Import.lazy(
|
|
14
|
-
'./constants',
|
|
15
|
-
require,
|
|
16
|
-
);
|
|
17
|
-
const core: typeof import('@modern-js/core') = Import.lazy(
|
|
18
|
-
'@modern-js/core',
|
|
19
|
-
require,
|
|
20
|
-
);
|
|
21
|
-
|
|
22
|
-
// 硬解字符串返回相应格式的对象
|
|
23
|
-
const updateMapper = (
|
|
24
|
-
packageFieldValue: JsSyntaxType,
|
|
25
|
-
outDir: IPackageModeValue['outDir'],
|
|
26
|
-
mapper: IPackageModeValue[],
|
|
27
|
-
): IPackageModeValue[] => {
|
|
28
|
-
if (packageFieldValue === 'CJS+ES6') {
|
|
29
|
-
return [...mapper, { type: 'commonjs', syntax: 'es6+', outDir }];
|
|
30
|
-
} else if (packageFieldValue === 'ESM+ES5') {
|
|
31
|
-
return [...mapper, { type: 'module', syntax: 'es5', outDir }];
|
|
32
|
-
} else if (packageFieldValue === 'ESM+ES6') {
|
|
33
|
-
return [...mapper, { type: 'module', syntax: 'es6+', outDir }];
|
|
34
|
-
} else {
|
|
35
|
-
return [...mapper];
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export const getCodeInitMapper = (_: IBuildConfig) => {
|
|
40
|
-
const {
|
|
41
|
-
output: { packageFields, packageMode },
|
|
42
|
-
} = core.useResolvedConfigContext() as ModuleToolsConfig;
|
|
43
|
-
let initMapper: IPackageModeValue[] = [];
|
|
44
|
-
|
|
45
|
-
// 如果不存在packageFields配置或者packageFields为空对象,则使用 packageMode
|
|
46
|
-
if (
|
|
47
|
-
!packageFields ||
|
|
48
|
-
(typeof packageFields === 'object' &&
|
|
49
|
-
Object.keys(packageFields).length === 0)
|
|
50
|
-
) {
|
|
51
|
-
initMapper =
|
|
52
|
-
constants.PACKAGE_MODES[packageMode || constants.DEFAULT_PACKAGE_MODE];
|
|
53
|
-
} else if (packageFields && Object.keys(packageFields).length > 0) {
|
|
54
|
-
if (packageFields.modern) {
|
|
55
|
-
initMapper = updateMapper(packageFields.modern, 'modern', initMapper);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (packageFields.main) {
|
|
59
|
-
initMapper = updateMapper(packageFields.main, 'node', initMapper);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (packageFields.module) {
|
|
63
|
-
initMapper = updateMapper(
|
|
64
|
-
packageFields.module,
|
|
65
|
-
'treeshaking',
|
|
66
|
-
initMapper,
|
|
67
|
-
);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// TODO: 如果存在其他配置,需要提示
|
|
71
|
-
if (!packageFields.modern && !packageFields.main && !packageFields.module) {
|
|
72
|
-
console.error(
|
|
73
|
-
chalk.red(
|
|
74
|
-
`Unrecognized ${JSON.stringify(
|
|
75
|
-
packageFields,
|
|
76
|
-
)} configuration, please use keys: 'modern, main, jupiter:modern' and use values: 'CJS+ES6, ESM+ES5, ESM+ES6'`,
|
|
77
|
-
),
|
|
78
|
-
);
|
|
79
|
-
|
|
80
|
-
// eslint-disable-next-line no-process-exit
|
|
81
|
-
process.exit(1);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return initMapper;
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
// 获取执行构建源码的参数
|
|
89
|
-
export const getCodeMapper = ({
|
|
90
|
-
logger,
|
|
91
|
-
taskPath,
|
|
92
|
-
config,
|
|
93
|
-
initMapper,
|
|
94
|
-
srcRootDir,
|
|
95
|
-
willCompilerDirOrFile,
|
|
96
|
-
}: ITaskMapper & {
|
|
97
|
-
config: IBuildConfig;
|
|
98
|
-
initMapper: IPackageModeValue[];
|
|
99
|
-
srcRootDir: string;
|
|
100
|
-
willCompilerDirOrFile: string;
|
|
101
|
-
}) => {
|
|
102
|
-
const { appDirectory } = core.useAppContext();
|
|
103
|
-
const modernConfig = core.useResolvedConfigContext();
|
|
104
|
-
const {
|
|
105
|
-
output: { enableSourceMap, jsPath = 'js', path: distDir = 'dist' },
|
|
106
|
-
} = modernConfig as ModuleToolsConfig;
|
|
107
|
-
|
|
108
|
-
const { tsconfigName = 'tsconfig.json' } = config;
|
|
109
|
-
const tsconfigPath = path.join(appDirectory, tsconfigName);
|
|
110
|
-
|
|
111
|
-
return initMapper.map(option => {
|
|
112
|
-
// 不是output.copy配置,而是内部的js copy逻辑
|
|
113
|
-
const copyDirs = option.copyDirs?.map(copyDir =>
|
|
114
|
-
path.join(appDirectory, `./${distDir}/${jsPath}/${copyDir}`),
|
|
115
|
-
);
|
|
116
|
-
return {
|
|
117
|
-
logger,
|
|
118
|
-
taskPath,
|
|
119
|
-
params: [
|
|
120
|
-
`--syntax=${option.syntax}`,
|
|
121
|
-
`--type=${option.type}`,
|
|
122
|
-
`--srcRootDir=${srcRootDir}`,
|
|
123
|
-
`--willCompilerDirOrFile=${willCompilerDirOrFile}`,
|
|
124
|
-
copyDirs ? `--copyDirs=${copyDirs.join(',')}` : '',
|
|
125
|
-
`--distDir=${path.join(
|
|
126
|
-
appDirectory,
|
|
127
|
-
`./${distDir}/${jsPath}/${option.outDir}`,
|
|
128
|
-
)}`,
|
|
129
|
-
`--appDirectory=${appDirectory}`,
|
|
130
|
-
enableSourceMap ? '--sourceMaps' : '',
|
|
131
|
-
`--tsconfigPath=${tsconfigPath}`,
|
|
132
|
-
],
|
|
133
|
-
};
|
|
134
|
-
});
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
// 获取执行生成 d.ts 的参数
|
|
138
|
-
export const getDtsMapper = (config: IBuildConfig, logger: LoggerText) => {
|
|
139
|
-
const { appDirectory } = core.useAppContext();
|
|
140
|
-
const modernConfig = core.useResolvedConfigContext();
|
|
141
|
-
const {
|
|
142
|
-
output: { disableTsChecker, path: outputPath = 'dist' },
|
|
143
|
-
} = modernConfig as ModuleToolsConfig;
|
|
144
|
-
const { tsconfigName = 'tsconfig.json', enableWatchMode, sourceDir } = config;
|
|
145
|
-
const srcDir = path.join(appDirectory, './src');
|
|
146
|
-
const tsconfigPath = path.join(appDirectory, tsconfigName);
|
|
147
|
-
return [
|
|
148
|
-
{
|
|
149
|
-
logger,
|
|
150
|
-
taskPath: require.resolve('../../tasks/generator-dts'),
|
|
151
|
-
params: [
|
|
152
|
-
`--srcDir=${srcDir}`,
|
|
153
|
-
`--distDir=${path.join(appDirectory, `./${outputPath}/types`)}`,
|
|
154
|
-
`--appDirectory=${appDirectory}`,
|
|
155
|
-
`--tsconfigPath=${tsconfigPath}`,
|
|
156
|
-
`--sourceDirName=${sourceDir}`,
|
|
157
|
-
enableWatchMode ? `--watch` : '',
|
|
158
|
-
disableTsChecker ? '' : `--tsCheck`,
|
|
159
|
-
],
|
|
160
|
-
},
|
|
161
|
-
];
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* 处理日志信息
|
|
166
|
-
*/
|
|
167
|
-
export class LogStack {
|
|
168
|
-
private _codeLogStack: string[];
|
|
169
|
-
|
|
170
|
-
constructor() {
|
|
171
|
-
this._codeLogStack = [];
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
update(latestLog: string, { splitEOL = false } = {}) {
|
|
175
|
-
if (splitEOL) {
|
|
176
|
-
latestLog.split(os.EOL).forEach(log => {
|
|
177
|
-
this._codeLogStack.unshift(log.trim());
|
|
178
|
-
});
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
this._codeLogStack.unshift(latestLog.trim());
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
clear() {
|
|
186
|
-
this._codeLogStack = [];
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
get value() {
|
|
190
|
-
return [...new Set(this._codeLogStack)];
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
export const logTemplate = (
|
|
195
|
-
title: string,
|
|
196
|
-
messageStack: string[],
|
|
197
|
-
maxLength: number,
|
|
198
|
-
{
|
|
199
|
-
noBottomBorder = false,
|
|
200
|
-
bottomBorderText = '',
|
|
201
|
-
noLeftBorder = false,
|
|
202
|
-
leftBorder = '│',
|
|
203
|
-
contentColor = (s: string) => s,
|
|
204
|
-
} = {},
|
|
205
|
-
) => {
|
|
206
|
-
const leftBorderFlag = noLeftBorder ? '' : leftBorder;
|
|
207
|
-
const messageFragments = messageStack
|
|
208
|
-
.map(p => {
|
|
209
|
-
p.trim();
|
|
210
|
-
|
|
211
|
-
return `${leftBorderFlag}${p.replace(constants.clearFlag, '')}`;
|
|
212
|
-
}) // 移除 clearFlag
|
|
213
|
-
.slice(0, maxLength) // 控制长度
|
|
214
|
-
.filter(s => s !== leftBorderFlag) // 过滤空字符串
|
|
215
|
-
.reverse(); // 调换顺序,最新的消息在最后面
|
|
216
|
-
const template = `${title}:
|
|
217
|
-
${contentColor(messageFragments.join(os.EOL))}${
|
|
218
|
-
noBottomBorder ? '' : `\n${bottomBorderText}`
|
|
219
|
-
}`;
|
|
220
|
-
return template;
|
|
221
|
-
};
|
|
222
|
-
|
|
223
|
-
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
|
|
224
|
-
export class TimeCounter {
|
|
225
|
-
static _now: number;
|
|
226
|
-
|
|
227
|
-
static time() {
|
|
228
|
-
this._now = Date.now();
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
static timeEnd() {
|
|
232
|
-
const span = Date.now() - this._now;
|
|
233
|
-
return span < 1000 ? `${span}ms` : `${(span / 1000).toFixed(2)}s`;
|
|
234
|
-
}
|
|
235
|
-
}
|