@modern-js/module-tools 1.0.0-alpha.3
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 +21 -0
- package/LICENSE +21 -0
- package/README.md +32 -0
- package/bin/modern.js +3 -0
- package/dist/js/modern/cli/build.js +9 -0
- package/dist/js/modern/cli/dev.js +8 -0
- package/dist/js/modern/cli/index.js +3 -0
- package/dist/js/modern/cli/new.js +16 -0
- package/dist/js/modern/commands/build.js +56 -0
- package/dist/js/modern/commands/dev.js +29 -0
- package/dist/js/modern/commands/index.js +2 -0
- package/dist/js/modern/features/build/build-platform.js +70 -0
- package/dist/js/modern/features/build/build-watch.js +96 -0
- package/dist/js/modern/features/build/build.js +110 -0
- package/dist/js/modern/features/build/constants.js +66 -0
- package/dist/js/modern/features/build/index.js +29 -0
- package/dist/js/modern/features/build/logger/index.js +2 -0
- package/dist/js/modern/features/build/logger/logText.js +63 -0
- package/dist/js/modern/features/build/logger/loggerManager.js +101 -0
- package/dist/js/modern/features/build/utils.js +198 -0
- package/dist/js/modern/features/dev/index.js +41 -0
- package/dist/js/modern/index.js +46 -0
- package/dist/js/modern/locale/en.js +23 -0
- package/dist/js/modern/locale/index.js +10 -0
- package/dist/js/modern/locale/zh.js +23 -0
- package/dist/js/modern/schema/index.js +3 -0
- package/dist/js/modern/schema/output.js +29 -0
- package/dist/js/modern/schema/source.js +15 -0
- package/dist/js/modern/tasks/build-source-code.js +213 -0
- package/dist/js/modern/tasks/build-style.js +152 -0
- package/dist/js/modern/tasks/build-watch-source-code.js +144 -0
- package/dist/js/modern/tasks/build-watch-style.js +213 -0
- package/dist/js/modern/tasks/constants.js +1 -0
- package/dist/js/modern/tasks/copy-assets.js +108 -0
- package/dist/js/modern/tasks/generator-dts.js +220 -0
- package/dist/js/modern/types.js +0 -0
- package/dist/js/modern/utils/babel.js +85 -0
- package/dist/js/modern/utils/color.js +2 -0
- package/dist/js/modern/utils/copy.js +80 -0
- package/dist/js/modern/utils/json.js +13 -0
- package/dist/js/modern/utils/language.js +6 -0
- package/dist/js/modern/utils/logger.js +110 -0
- package/dist/js/modern/utils/readline.js +19 -0
- package/dist/js/modern/utils/tsconfig.js +15 -0
- package/dist/js/modern/utils/tspaths-transform/constants.js +3 -0
- package/dist/js/modern/utils/tspaths-transform/index.js +130 -0
- package/dist/js/modern/utils/tspaths-transform/utils.js +21 -0
- package/dist/js/modern/utils/valide.js +28 -0
- package/dist/js/node/cli/build.js +21 -0
- package/dist/js/node/cli/dev.js +20 -0
- package/dist/js/node/cli/index.js +44 -0
- package/dist/js/node/cli/new.js +28 -0
- package/dist/js/node/commands/build.js +76 -0
- package/dist/js/node/commands/dev.js +49 -0
- package/dist/js/node/commands/index.js +21 -0
- package/dist/js/node/features/build/build-platform.js +84 -0
- package/dist/js/node/features/build/build-watch.js +118 -0
- package/dist/js/node/features/build/build.js +133 -0
- package/dist/js/node/features/build/constants.js +79 -0
- package/dist/js/node/features/build/index.js +41 -0
- package/dist/js/node/features/build/logger/index.js +31 -0
- package/dist/js/node/features/build/logger/logText.js +79 -0
- package/dist/js/node/features/build/logger/loggerManager.js +111 -0
- package/dist/js/node/features/build/utils.js +231 -0
- package/dist/js/node/features/dev/index.js +60 -0
- package/dist/js/node/index.js +64 -0
- package/dist/js/node/locale/en.js +30 -0
- package/dist/js/node/locale/index.js +22 -0
- package/dist/js/node/locale/zh.js +30 -0
- package/dist/js/node/schema/index.js +14 -0
- package/dist/js/node/schema/output.js +36 -0
- package/dist/js/node/schema/source.js +22 -0
- package/dist/js/node/tasks/build-source-code.js +240 -0
- package/dist/js/node/tasks/build-style.js +168 -0
- package/dist/js/node/tasks/build-watch-source-code.js +156 -0
- package/dist/js/node/tasks/build-watch-style.js +231 -0
- package/dist/js/node/tasks/constants.js +8 -0
- package/dist/js/node/tasks/copy-assets.js +126 -0
- package/dist/js/node/tasks/generator-dts.js +243 -0
- package/dist/js/node/types.js +0 -0
- package/dist/js/node/utils/babel.js +99 -0
- package/dist/js/node/utils/color.js +12 -0
- package/dist/js/node/utils/copy.js +99 -0
- package/dist/js/node/utils/json.js +22 -0
- package/dist/js/node/utils/language.js +15 -0
- package/dist/js/node/utils/logger.js +133 -0
- package/dist/js/node/utils/readline.js +33 -0
- package/dist/js/node/utils/tsconfig.js +30 -0
- package/dist/js/node/utils/tspaths-transform/constants.js +10 -0
- package/dist/js/node/utils/tspaths-transform/index.js +155 -0
- package/dist/js/node/utils/tspaths-transform/utils.js +32 -0
- package/dist/js/node/utils/valide.js +41 -0
- package/dist/types/cli/build.d.ts +2 -0
- package/dist/types/cli/dev.d.ts +2 -0
- package/dist/types/cli/index.d.ts +3 -0
- package/dist/types/cli/new.d.ts +2 -0
- package/dist/types/commands/build.d.ts +16 -0
- package/dist/types/commands/dev.d.ts +4 -0
- package/dist/types/commands/index.d.ts +2 -0
- package/dist/types/features/build/build-platform.d.ts +6 -0
- package/dist/types/features/build/build-watch.d.ts +3 -0
- package/dist/types/features/build/build.d.ts +3 -0
- package/dist/types/features/build/constants.d.ts +14 -0
- package/dist/types/features/build/index.d.ts +3 -0
- package/dist/types/features/build/logger/index.d.ts +2 -0
- package/dist/types/features/build/logger/logText.d.ts +39 -0
- package/dist/types/features/build/logger/loggerManager.d.ts +39 -0
- package/dist/types/features/build/utils.d.ts +58 -0
- package/dist/types/features/dev/index.d.ts +7 -0
- package/dist/types/index.d.ts +17 -0
- package/dist/types/locale/en.d.ts +23 -0
- package/dist/types/locale/index.d.ts +47 -0
- package/dist/types/locale/zh.d.ts +23 -0
- package/dist/types/schema/index.d.ts +36 -0
- package/dist/types/schema/output.d.ts +22 -0
- package/dist/types/schema/source.d.ts +15 -0
- package/dist/types/tasks/build-source-code.d.ts +42 -0
- package/dist/types/tasks/build-style.d.ts +1 -0
- package/dist/types/tasks/build-watch-source-code.d.ts +1 -0
- package/dist/types/tasks/build-watch-style.d.ts +1 -0
- package/dist/types/tasks/constants.d.ts +1 -0
- package/dist/types/tasks/copy-assets.d.ts +1 -0
- package/dist/types/tasks/generator-dts.d.ts +1 -0
- package/dist/types/types.d.ts +48 -0
- package/dist/types/utils/babel.d.ts +8 -0
- package/dist/types/utils/color.d.ts +2 -0
- package/dist/types/utils/copy.d.ts +5 -0
- package/dist/types/utils/json.d.ts +4 -0
- package/dist/types/utils/language.d.ts +1 -0
- package/dist/types/utils/logger.d.ts +56 -0
- package/dist/types/utils/readline.d.ts +9 -0
- package/dist/types/utils/tsconfig.d.ts +16 -0
- package/dist/types/utils/tspaths-transform/constants.d.ts +1 -0
- package/dist/types/utils/tspaths-transform/index.d.ts +15 -0
- package/dist/types/utils/tspaths-transform/utils.d.ts +4 -0
- package/dist/types/utils/valide.d.ts +12 -0
- package/modern.config.js +6 -0
- package/package.json +95 -0
- package/src/.eslintrc.json +5 -0
- package/src/cli/build.ts +39 -0
- package/src/cli/dev.ts +24 -0
- package/src/cli/index.ts +3 -0
- package/src/cli/new.ts +32 -0
- package/src/commands/build.ts +81 -0
- package/src/commands/dev.ts +41 -0
- package/src/commands/index.ts +2 -0
- package/src/features/build/build-platform.ts +76 -0
- package/src/features/build/build-watch.ts +93 -0
- package/src/features/build/build.ts +111 -0
- package/src/features/build/constants.ts +52 -0
- package/src/features/build/index.ts +36 -0
- package/src/features/build/logger/index.ts +2 -0
- package/src/features/build/logger/logText.ts +80 -0
- package/src/features/build/logger/loggerManager.ts +132 -0
- package/src/features/build/utils.ts +250 -0
- package/src/features/dev/index.ts +62 -0
- package/src/index.ts +55 -0
- package/src/locale/en.ts +21 -0
- package/src/locale/index.ts +15 -0
- package/src/locale/zh.ts +21 -0
- package/src/schema/index.ts +4 -0
- package/src/schema/output.ts +34 -0
- package/src/schema/schema.d.ts +13 -0
- package/src/schema/source.ts +16 -0
- package/src/tasks/build-source-code.ts +245 -0
- package/src/tasks/build-style.ts +175 -0
- package/src/tasks/build-watch-source-code.ts +185 -0
- package/src/tasks/build-watch-style.ts +260 -0
- package/src/tasks/constants.ts +1 -0
- package/src/tasks/copy-assets.ts +117 -0
- package/src/tasks/generator-dts.ts +277 -0
- package/src/type.d.ts +1 -0
- package/src/types.ts +63 -0
- package/src/utils/babel.ts +100 -0
- package/src/utils/color.ts +3 -0
- package/src/utils/copy.ts +70 -0
- package/src/utils/json.ts +13 -0
- package/src/utils/language.ts +9 -0
- package/src/utils/logger.ts +144 -0
- package/src/utils/readline.ts +28 -0
- package/src/utils/tsconfig.ts +37 -0
- package/src/utils/tspaths-transform/constants.ts +19 -0
- package/src/utils/tspaths-transform/index.ts +139 -0
- package/src/utils/tspaths-transform/utils.ts +25 -0
- package/src/utils/valide.ts +37 -0
- package/tsconfig.json +13 -0
- package/types.d.ts +1 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
const _excluded = ["glob"];
|
|
2
|
+
|
|
3
|
+
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
4
|
+
|
|
5
|
+
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
6
|
+
|
|
7
|
+
import * as path from 'path';
|
|
8
|
+
import { fs, Import } from '@modern-js/utils';
|
|
9
|
+
const globby = Import.lazy('globby', require);
|
|
10
|
+
const fastGlob = Import.lazy('fast-glob', require);
|
|
11
|
+
const normalizePath = Import.lazy('normalize-path', require); // eslint-disable-next-line max-statements
|
|
12
|
+
|
|
13
|
+
export const copyTask = async option => {
|
|
14
|
+
const {
|
|
15
|
+
modernConfig,
|
|
16
|
+
appContext
|
|
17
|
+
} = option;
|
|
18
|
+
const {
|
|
19
|
+
appDirectory
|
|
20
|
+
} = appContext;
|
|
21
|
+
const {
|
|
22
|
+
output: {
|
|
23
|
+
copy,
|
|
24
|
+
path: outputPath = 'dist'
|
|
25
|
+
}
|
|
26
|
+
} = modernConfig;
|
|
27
|
+
|
|
28
|
+
if (!copy) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
try {
|
|
33
|
+
// 类型暂时这样处理,待之后优化copy的逻辑
|
|
34
|
+
for (const copyOption of copy) {
|
|
35
|
+
// 在原来的基础上,引入了类似于 copy-webpck-plugin 的 context 属性,可以设置项目根路径
|
|
36
|
+
const {
|
|
37
|
+
context = appDirectory,
|
|
38
|
+
from,
|
|
39
|
+
globOptions,
|
|
40
|
+
to: toOrigin
|
|
41
|
+
} = copyOption;
|
|
42
|
+
let fromOrigin = from; // 获取 glob 规则
|
|
43
|
+
|
|
44
|
+
let glob = '';
|
|
45
|
+
let options = {};
|
|
46
|
+
|
|
47
|
+
if (fromOrigin !== null && typeof fromOrigin === 'object') {
|
|
48
|
+
var _fromOrigin = fromOrigin;
|
|
49
|
+
({
|
|
50
|
+
glob
|
|
51
|
+
} = _fromOrigin);
|
|
52
|
+
options = _objectWithoutProperties(_fromOrigin, _excluded);
|
|
53
|
+
_fromOrigin;
|
|
54
|
+
fromOrigin = glob;
|
|
55
|
+
} else if (globOptions && typeof globOptions === 'object') {
|
|
56
|
+
options = globOptions;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
glob = path.isAbsolute(fromOrigin) ? fromOrigin : path.posix.join(fastGlob.escapePath(normalizePath(path.resolve(context))), fromOrigin); // 计算 glob,获取目标文件
|
|
60
|
+
|
|
61
|
+
const paths = await globby(glob, options);
|
|
62
|
+
|
|
63
|
+
if (!paths.length) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const to = path.normalize(toOrigin ? toOrigin : '');
|
|
68
|
+
const isToDirectory = path.extname(to) === '';
|
|
69
|
+
paths.forEach(item => {
|
|
70
|
+
const relativeFrom = path.relative(context, item); // 如果 to 是目录,通过相对路径计算完整的产物路径;如果 to 是文件,直接作为最终产物路径
|
|
71
|
+
|
|
72
|
+
const fileName = isToDirectory ? path.join(to, relativeFrom) : to;
|
|
73
|
+
const finalToPath = path.resolve(outputPath, fileName);
|
|
74
|
+
fs.copySync(item, finalToPath);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
} catch (e) {
|
|
78
|
+
console.error(`copy error: ${e.message}`);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/* eslint-disable max-classes-per-file */
|
|
2
|
+
import EventEmitter from 'events';
|
|
3
|
+
import { Import } from '@modern-js/utils';
|
|
4
|
+
const chalk = Import.lazy('chalk', require);
|
|
5
|
+
export const clearFlag = '\x1Bc';
|
|
6
|
+
export const logTemplate = (title, messageStack, maxLength, {
|
|
7
|
+
noBottomBorder: _noBottomBorder = false,
|
|
8
|
+
bottomBorderText: _bottomBorderText = '',
|
|
9
|
+
noLeftBorder: _noLeftBorder = false,
|
|
10
|
+
leftBorder: _leftBorder = '│',
|
|
11
|
+
contentColor: _contentColor = s => s
|
|
12
|
+
} = {}) => {
|
|
13
|
+
const leftBorderFlag = _noLeftBorder ? '' : _leftBorder;
|
|
14
|
+
const messageFragments = messageStack.map(p => {
|
|
15
|
+
p.trim();
|
|
16
|
+
return `${leftBorderFlag}${p.replace(clearFlag, '')}`;
|
|
17
|
+
}) // 移除 clearFlag
|
|
18
|
+
.filter(s => s !== leftBorderFlag) // 过滤空字符串
|
|
19
|
+
.slice(0, maxLength) // 控制长度
|
|
20
|
+
.reverse(); // 调换顺序,最新的消息在最后面
|
|
21
|
+
|
|
22
|
+
messageFragments[messageFragments.length - 1] = messageFragments[messageFragments.length - 1].trim();
|
|
23
|
+
const template = `${title}:
|
|
24
|
+
${_contentColor(messageFragments.join(''))}${_noBottomBorder ? '' : `\n${_bottomBorderText}`}`;
|
|
25
|
+
console.info('template', messageFragments);
|
|
26
|
+
return template;
|
|
27
|
+
};
|
|
28
|
+
export class LoggerText {
|
|
29
|
+
constructor(option) {
|
|
30
|
+
this.messages = void 0;
|
|
31
|
+
this.option = void 0;
|
|
32
|
+
this.messages = [];
|
|
33
|
+
this.option = option;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
append(message) {
|
|
37
|
+
var _this$option$contentC;
|
|
38
|
+
|
|
39
|
+
const replace = ((_this$option$contentC = this.option.contentConfig) === null || _this$option$contentC === void 0 ? void 0 : _this$option$contentC.replace) || [];
|
|
40
|
+
let content = message;
|
|
41
|
+
|
|
42
|
+
for (const r of replace) {
|
|
43
|
+
content = content.replace(new RegExp(r, 'g'), '');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
this.messages.push(content);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
get value() {
|
|
50
|
+
const {
|
|
51
|
+
title,
|
|
52
|
+
maxLength,
|
|
53
|
+
contentConfig
|
|
54
|
+
} = this.option;
|
|
55
|
+
const messages = [...new Set(this.messages)];
|
|
56
|
+
return logTemplate(title, messages, maxLength, contentConfig);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
}
|
|
60
|
+
export class LoggerManager extends EventEmitter {
|
|
61
|
+
// constructor() {}
|
|
62
|
+
createLoggerText(option) {
|
|
63
|
+
return new LoggerText(option);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
addStdout(loggerText, stdout, config = {}) {
|
|
67
|
+
const {
|
|
68
|
+
event = {
|
|
69
|
+
data: true,
|
|
70
|
+
error: true
|
|
71
|
+
},
|
|
72
|
+
colors = {
|
|
73
|
+
data: chalk.green,
|
|
74
|
+
error: chalk.red,
|
|
75
|
+
warning: chalk.yellow
|
|
76
|
+
}
|
|
77
|
+
} = config;
|
|
78
|
+
|
|
79
|
+
if (event.data) {
|
|
80
|
+
stdout === null || stdout === void 0 ? void 0 : stdout.on('data', chunk => {
|
|
81
|
+
const data = chunk.toString();
|
|
82
|
+
const content = colors.data ? colors.data(data) : chalk.green(data);
|
|
83
|
+
loggerText.append(content);
|
|
84
|
+
this.emit('data');
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (event.error) {
|
|
89
|
+
stdout === null || stdout === void 0 ? void 0 : stdout.on('error', error => {
|
|
90
|
+
const data = error.message;
|
|
91
|
+
const content = colors.error ? colors.error(data) : chalk.red(data);
|
|
92
|
+
loggerText.append(content);
|
|
93
|
+
this.emit('data');
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
addStderr(loggerText, stderr) {
|
|
99
|
+
stderr === null || stderr === void 0 ? void 0 : stderr.on('data', chunk => {
|
|
100
|
+
const data = chunk.toString();
|
|
101
|
+
loggerText.append(data);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
show(loggerText) {
|
|
106
|
+
console.info(loggerText.value);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
}
|
|
110
|
+
/* eslint-enable max-classes-per-file */
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import * as readline from 'readline'; // eslint-disable-next-line @typescript-eslint/no-extraneous-class
|
|
2
|
+
|
|
3
|
+
export class ReadlineUtils {
|
|
4
|
+
static clearPrevLine(output) {
|
|
5
|
+
ReadlineUtils.clearLine(output, 1);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
static clearLine(output, n = 1, dir = -1) {
|
|
9
|
+
// -1 向上,1 向下
|
|
10
|
+
const dx = dir === 1 ? 1 : -1;
|
|
11
|
+
|
|
12
|
+
for (let i = 0; i < n; i++) {
|
|
13
|
+
readline.moveCursor(output, 0, dx);
|
|
14
|
+
readline.clearLine(output, 0);
|
|
15
|
+
readline.cursorTo(output, 0);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { fs, Import } from '@modern-js/utils';
|
|
2
|
+
const json5 = Import.lazy('json5', require);
|
|
3
|
+
export const readTsConfig = (tsconfigPath, noExistReturn = null) => {
|
|
4
|
+
// 如果不存在,则返回 noExistReturn
|
|
5
|
+
if (!fs.existsSync(tsconfigPath)) {
|
|
6
|
+
return noExistReturn;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const content = fs.readFileSync(tsconfigPath, 'utf-8');
|
|
10
|
+
return json5.parse(content);
|
|
11
|
+
};
|
|
12
|
+
export const existTsConfigFile = tsconfigAbsolutePath => {
|
|
13
|
+
const tsconfig = readTsConfig(tsconfigAbsolutePath);
|
|
14
|
+
return Boolean(tsconfig);
|
|
15
|
+
};
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export const defaultTransformedFunctions = ['require', 'require.resolve', 'System.import', // Jest methods
|
|
2
|
+
'jest.genMockFromModule', 'jest.mock', 'jest.unmock', 'jest.doMock', 'jest.dontMock', 'jest.setMock', 'jest.requireActual', 'jest.requireMock', // Older Jest methods
|
|
3
|
+
'require.requireActual', 'require.requireMock'];
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import * as path from 'path';
|
|
2
|
+
import * as parser from '@babel/parser';
|
|
3
|
+
import traverse 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
|
+
const getPathsMap = (paths, sourceDirName = 'src') => {
|
|
12
|
+
const pathKeys = Object.keys(paths);
|
|
13
|
+
const pathsMap = {};
|
|
14
|
+
|
|
15
|
+
const replaceSrcToTypes = s => s.replace(sourceDirName, 'types');
|
|
16
|
+
|
|
17
|
+
for (const key of pathKeys) {
|
|
18
|
+
const p = paths[key];
|
|
19
|
+
|
|
20
|
+
if (typeof p === 'string') {
|
|
21
|
+
pathsMap[key] = [replaceSrcToTypes(p)];
|
|
22
|
+
} else {
|
|
23
|
+
pathsMap[key] = paths[key].map(sp => replaceSrcToTypes(sp));
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return pathsMap;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
function mapPathString(nodePath, {
|
|
31
|
+
filename,
|
|
32
|
+
baseUrl,
|
|
33
|
+
paths
|
|
34
|
+
}) {
|
|
35
|
+
if (!t.isStringLiteral(nodePath)) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const sourcePath = nodePath.node.value;
|
|
40
|
+
const currentFile = filename;
|
|
41
|
+
const pathsMap = getPathsMap(paths);
|
|
42
|
+
const matchPath = createMatchPath(baseUrl, pathsMap, ['index']);
|
|
43
|
+
const result = matchPath(sourcePath, packageJsonPath => {
|
|
44
|
+
if (!fs.existsSync(packageJsonPath)) {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return fs.readJSONSync(packageJsonPath);
|
|
49
|
+
}, filePath => fs.existsSync(filePath), ['.d.ts']);
|
|
50
|
+
|
|
51
|
+
if (result) {
|
|
52
|
+
const relativePath = path.relative(path.dirname(currentFile), path.dirname(result));
|
|
53
|
+
const fileName = path.basename(result); // 如果是同级文件,则返回的是 ''
|
|
54
|
+
|
|
55
|
+
const filePath = path.normalize(`${relativePath.length === 0 ? '.' : relativePath}/${fileName}`);
|
|
56
|
+
const replaceString = filePath.startsWith('.') ? filePath : `./${filePath}`;
|
|
57
|
+
nodePath.replaceWith(t.stringLiteral(replaceString));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const transformCall = option => nodePath => {
|
|
62
|
+
const calleePath = nodePath.get('callee');
|
|
63
|
+
const isNormalCall = defaultTransformedFunctions.some(pattern => matchesPattern(calleePath, pattern));
|
|
64
|
+
|
|
65
|
+
if (isNormalCall || isImportCall(nodePath)) {
|
|
66
|
+
mapPathString(nodePath.get('arguments.0'), option);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const transformImport = option => nodePath => {
|
|
71
|
+
mapPathString(nodePath.get('source'), option);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const transformExport = option => nodePath => {
|
|
75
|
+
mapPathString(nodePath.get('source'), option);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const transfromSingleFileAlias = ({
|
|
79
|
+
filename,
|
|
80
|
+
baseUrl,
|
|
81
|
+
paths
|
|
82
|
+
}) => {
|
|
83
|
+
const sourceCode = fs.readFileSync(filename, 'utf-8');
|
|
84
|
+
const ast = parser.parse(sourceCode, {
|
|
85
|
+
sourceType: 'module',
|
|
86
|
+
errorRecovery: true,
|
|
87
|
+
// 防止typescript不支持的语法出现而报错
|
|
88
|
+
plugins: ['typescript']
|
|
89
|
+
});
|
|
90
|
+
traverse(ast, {
|
|
91
|
+
CallExpression: transformCall({
|
|
92
|
+
filename,
|
|
93
|
+
baseUrl,
|
|
94
|
+
paths
|
|
95
|
+
}),
|
|
96
|
+
ImportDeclaration: transformImport({
|
|
97
|
+
filename,
|
|
98
|
+
baseUrl,
|
|
99
|
+
paths
|
|
100
|
+
}),
|
|
101
|
+
ExportDeclaration: transformExport({
|
|
102
|
+
filename,
|
|
103
|
+
baseUrl,
|
|
104
|
+
paths
|
|
105
|
+
})
|
|
106
|
+
});
|
|
107
|
+
return generator(ast).code;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
export const transformDtsAlias = option => {
|
|
111
|
+
const {
|
|
112
|
+
filenames = [],
|
|
113
|
+
baseUrl,
|
|
114
|
+
paths
|
|
115
|
+
} = option;
|
|
116
|
+
const transformResult = [];
|
|
117
|
+
|
|
118
|
+
for (const filename of filenames) {
|
|
119
|
+
transformResult.push({
|
|
120
|
+
path: filename,
|
|
121
|
+
content: transfromSingleFileAlias({
|
|
122
|
+
filename,
|
|
123
|
+
baseUrl,
|
|
124
|
+
paths
|
|
125
|
+
})
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return transformResult;
|
|
130
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Import } from '@modern-js/utils';
|
|
2
|
+
const t = Import.lazy('@babel/types', require);
|
|
3
|
+
export function matchesPattern(calleePath, pattern) {
|
|
4
|
+
const {
|
|
5
|
+
node
|
|
6
|
+
} = calleePath;
|
|
7
|
+
|
|
8
|
+
if (t.isMemberExpression(node)) {
|
|
9
|
+
return calleePath.matchesPattern(pattern);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (!t.isIdentifier(node) || pattern.includes('.')) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const name = pattern.split('.')[0];
|
|
17
|
+
return node.name === name;
|
|
18
|
+
}
|
|
19
|
+
export function isImportCall(calleePath) {
|
|
20
|
+
return t.isImport(calleePath.node.callee);
|
|
21
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { validAlias } from '@modern-js/utils';
|
|
2
|
+
export const valideBeforeTask = ({
|
|
3
|
+
modernConfig,
|
|
4
|
+
tsconfigPath
|
|
5
|
+
}) => {
|
|
6
|
+
const modernConfigValidResult = modernConfigValid(modernConfig, {
|
|
7
|
+
tsconfigPath
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
if (modernConfigValidResult) {
|
|
11
|
+
console.error(modernConfigValidResult); // eslint-disable-next-line no-process-exit
|
|
12
|
+
|
|
13
|
+
process.exit(0);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
export const modernConfigValid = (modernConfig, option) => {
|
|
17
|
+
const valids = [validAlias];
|
|
18
|
+
|
|
19
|
+
for (const validFn of valids) {
|
|
20
|
+
const result = validFn(modernConfig, option);
|
|
21
|
+
|
|
22
|
+
if (result) {
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return null;
|
|
28
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.buildCli = void 0;
|
|
7
|
+
|
|
8
|
+
var _utils = require("@modern-js/utils");
|
|
9
|
+
|
|
10
|
+
const local = _utils.Import.lazy('../locale/index', require);
|
|
11
|
+
|
|
12
|
+
const commands = _utils.Import.lazy('../commands', require);
|
|
13
|
+
|
|
14
|
+
const buildCli = program => {
|
|
15
|
+
// TODO: 初始化环境变量
|
|
16
|
+
program.command('build').usage('[options]').description(local.i18n.t(local.localeKeys.command.build.describe)).option('-w, --watch', local.i18n.t(local.localeKeys.command.build.watch)).option('--tsconfig [tsconfig]', local.i18n.t(local.localeKeys.command.build.tsconfig), './tsconfig.json').option('--style-only', local.i18n.t(local.localeKeys.command.build.style_only)).option('--platform [platform]', local.i18n.t(local.localeKeys.command.build.platform)).option('--no-tsc', local.i18n.t(local.localeKeys.command.build.no_tsc)).option('--no-clear', local.i18n.t(local.localeKeys.command.build.no_clear)).action(async subCommand => {
|
|
17
|
+
await commands.build(subCommand);
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
exports.buildCli = buildCli;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.devCli = void 0;
|
|
7
|
+
|
|
8
|
+
var _utils = require("@modern-js/utils");
|
|
9
|
+
|
|
10
|
+
const local = _utils.Import.lazy('../locale', require);
|
|
11
|
+
|
|
12
|
+
const commands = _utils.Import.lazy('../commands', require);
|
|
13
|
+
|
|
14
|
+
const devCli = program => {
|
|
15
|
+
program.command('dev').usage('[options]').description(local.i18n.t(local.localeKeys.command.dev.describe)).option('--tsconfig [tsconfig]', local.i18n.t(local.localeKeys.command.build.tsconfig), './tsconfig.json').action(async params => {
|
|
16
|
+
await commands.dev(params);
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
exports.devCli = devCli;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
var _build = require("./build");
|
|
8
|
+
|
|
9
|
+
Object.keys(_build).forEach(function (key) {
|
|
10
|
+
if (key === "default" || key === "__esModule") return;
|
|
11
|
+
if (key in exports && exports[key] === _build[key]) return;
|
|
12
|
+
Object.defineProperty(exports, key, {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return _build[key];
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
var _dev = require("./dev");
|
|
21
|
+
|
|
22
|
+
Object.keys(_dev).forEach(function (key) {
|
|
23
|
+
if (key === "default" || key === "__esModule") return;
|
|
24
|
+
if (key in exports && exports[key] === _dev[key]) return;
|
|
25
|
+
Object.defineProperty(exports, key, {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
get: function () {
|
|
28
|
+
return _dev[key];
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
var _new = require("./new");
|
|
34
|
+
|
|
35
|
+
Object.keys(_new).forEach(function (key) {
|
|
36
|
+
if (key === "default" || key === "__esModule") return;
|
|
37
|
+
if (key in exports && exports[key] === _new[key]) return;
|
|
38
|
+
Object.defineProperty(exports, key, {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
get: function () {
|
|
41
|
+
return _new[key];
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.newCli = void 0;
|
|
7
|
+
|
|
8
|
+
var _utils = require("@modern-js/utils");
|
|
9
|
+
|
|
10
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
11
|
+
|
|
12
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
13
|
+
|
|
14
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
15
|
+
|
|
16
|
+
const newAction = _utils.Import.lazy('@modern-js/new-action', require);
|
|
17
|
+
|
|
18
|
+
const local = _utils.Import.lazy('../locale', require);
|
|
19
|
+
|
|
20
|
+
const newCli = (program, locale) => {
|
|
21
|
+
program.command('new').usage('[options]').description(local.i18n.t(local.localeKeys.command.new.describe)).option('-d, --debug', local.i18n.t(local.localeKeys.command.new.debug), false).option('-c, --config <config>', local.i18n.t(local.localeKeys.command.new.config)).option('--dist-tag <tag>', local.i18n.t(local.localeKeys.command.new.distTag)).option('--registry', local.i18n.t(local.localeKeys.command.new.registry)).action(async options => {
|
|
22
|
+
await newAction.ModuleNewAction(_objectSpread(_objectSpread({}, options), {}, {
|
|
23
|
+
locale
|
|
24
|
+
}));
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
exports.newCli = newCli;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.build = void 0;
|
|
7
|
+
|
|
8
|
+
var path = _interopRequireWildcard(require("path"));
|
|
9
|
+
|
|
10
|
+
var _utils = require("@modern-js/utils");
|
|
11
|
+
|
|
12
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
13
|
+
|
|
14
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
15
|
+
|
|
16
|
+
const tsConfigutils = _utils.Import.lazy('../utils/tsconfig', require);
|
|
17
|
+
|
|
18
|
+
const valid = _utils.Import.lazy('../utils/valide', require);
|
|
19
|
+
|
|
20
|
+
const buildFeature = _utils.Import.lazy('../features/build', require);
|
|
21
|
+
|
|
22
|
+
const core = _utils.Import.lazy('@modern-js/core', require);
|
|
23
|
+
|
|
24
|
+
const dotenv = _utils.Import.lazy('dotenv', require);
|
|
25
|
+
|
|
26
|
+
const build = async ({
|
|
27
|
+
watch: _watch = false,
|
|
28
|
+
tsconfig: tsconfigName,
|
|
29
|
+
tsc,
|
|
30
|
+
clear: _clear = true,
|
|
31
|
+
platform
|
|
32
|
+
}) => {
|
|
33
|
+
const {
|
|
34
|
+
value: {
|
|
35
|
+
appDirectory
|
|
36
|
+
}
|
|
37
|
+
} = core.useAppContext();
|
|
38
|
+
const modernConfig = core.useResolvedConfigContext().value;
|
|
39
|
+
const {
|
|
40
|
+
output: {
|
|
41
|
+
path: outputPath = 'dist'
|
|
42
|
+
}
|
|
43
|
+
} = modernConfig;
|
|
44
|
+
const tsconfigPath = path.join(appDirectory, tsconfigName);
|
|
45
|
+
dotenv.config();
|
|
46
|
+
const isTsProject = tsConfigutils.existTsConfigFile(tsconfigPath);
|
|
47
|
+
const enableTscCompiler = isTsProject && tsc;
|
|
48
|
+
valid.valideBeforeTask({
|
|
49
|
+
modernConfig,
|
|
50
|
+
tsconfigPath
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
if (_clear) {
|
|
54
|
+
_utils.fs.removeSync(path.join(appDirectory, outputPath));
|
|
55
|
+
} // TODO: 一些配置只需要从modernConfig中获取
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
await buildFeature.build({
|
|
59
|
+
enableWatchMode: _watch,
|
|
60
|
+
isTsProject,
|
|
61
|
+
platform,
|
|
62
|
+
sourceDir: 'src',
|
|
63
|
+
tsconfigName,
|
|
64
|
+
enableTscCompiler
|
|
65
|
+
}, modernConfig);
|
|
66
|
+
process.on('SIGBREAK', () => {
|
|
67
|
+
console.info('exit');
|
|
68
|
+
const tempTsconfigFilePath = path.join(appDirectory, './tsconfig.temp.json');
|
|
69
|
+
|
|
70
|
+
if (_utils.fs.existsSync(tempTsconfigFilePath)) {
|
|
71
|
+
_utils.fs.removeSync(tempTsconfigFilePath);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
exports.build = build;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.dev = void 0;
|
|
7
|
+
|
|
8
|
+
var path = _interopRequireWildcard(require("path"));
|
|
9
|
+
|
|
10
|
+
var _utils = require("@modern-js/utils");
|
|
11
|
+
|
|
12
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
13
|
+
|
|
14
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
15
|
+
|
|
16
|
+
const devFeature = _utils.Import.lazy('../features/dev', require);
|
|
17
|
+
|
|
18
|
+
const core = _utils.Import.lazy('@modern-js/core', require);
|
|
19
|
+
|
|
20
|
+
const dotenv = _utils.Import.lazy('dotenv', require);
|
|
21
|
+
|
|
22
|
+
const tsConfigutils = _utils.Import.lazy('../utils/tsconfig', require);
|
|
23
|
+
|
|
24
|
+
const valid = _utils.Import.lazy('../utils/valide', require);
|
|
25
|
+
|
|
26
|
+
const dev = async option => {
|
|
27
|
+
const {
|
|
28
|
+
tsconfig: tsconfigName
|
|
29
|
+
} = option;
|
|
30
|
+
const appContext = core.useAppContext().value;
|
|
31
|
+
const modernConfig = core.useResolvedConfigContext().value;
|
|
32
|
+
const {
|
|
33
|
+
appDirectory
|
|
34
|
+
} = appContext;
|
|
35
|
+
const tsconfigPath = path.join(appDirectory, tsconfigName);
|
|
36
|
+
dotenv.config();
|
|
37
|
+
valid.valideBeforeTask({
|
|
38
|
+
modernConfig,
|
|
39
|
+
tsconfigPath
|
|
40
|
+
});
|
|
41
|
+
const isTsProject = tsConfigutils.existTsConfigFile(tsconfigPath); // await devFeature.showMenu({ isTsProject, appDirectory });
|
|
42
|
+
|
|
43
|
+
await devFeature.devStorybook({
|
|
44
|
+
isTsProject,
|
|
45
|
+
appDirectory
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
exports.dev = dev;
|