@tmsfe/tmskit 0.0.5 → 0.0.8
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/README.md +18 -2
- package/dist/index.cjs.js +2286 -1258
- package/package.json +24 -8
- package/src/compile/build.js +5 -0
- package/src/compile/compile.js +90 -0
- package/src/compile/dev.js +129 -0
- package/src/compile/plugins/less.js +116 -0
- package/src/compile/plugins/mpCommonDep.js +131 -0
- package/src/compile/plugins/mpJsonDep.js +112 -0
- package/src/compile/plugins/mpWxmlDep.js +194 -0
- package/src/compile/plugins/postcss-font-base64.js +72 -0
- package/src/compile/plugins/replaceEnv.js +29 -0
- package/src/compile/plugins/utils/pluginError.js +25 -0
- package/src/config/constant.js +11 -10
- package/src/config/defaultTmsConfig.js +9 -10
- package/src/core/buildAppJson.js +166 -0
- package/src/{utils → core}/checkDependencies.js +6 -6
- package/src/core/cloneModules.js +203 -0
- package/src/core/handleError.js +18 -0
- package/src/core/isInIt.js +69 -0
- package/src/{utils/mpCiUtils.js → core/mpCi.js} +0 -1
- package/src/core/npm.js +218 -0
- package/src/core/symbolicLink.js +24 -0
- package/src/core/tmsMpconfig.js +234 -0
- package/src/entry.js +50 -8
- package/src/index.js +31 -15
- package/src/init.js +2 -7
- package/src/scripts/create/index.js +2 -2
- package/src/scripts/run/build/index.js +3 -3
- package/src/scripts/run/dev/index.js +27 -52
- package/src/scripts/run/index.js +70 -36
- package/src/scripts/run/init/index.js +40 -42
- package/src/scripts/run/install/index.js +21 -29
- package/src/utils/findCssImport.js +30 -0
- package/src/utils/global.js +19 -33
- package/src/utils/io.js +86 -0
- package/src/utils/log.js +3 -0
- package/src/utils/widgets.js +59 -51
- package/CHANGELOG.md +0 -0
- package/main.js +0 -3
- package/rollup.config.js +0 -179
- package/src/utils/buildAppJson.js +0 -144
- package/src/utils/cliUtils.js +0 -35
- package/src/utils/cloneModules.js +0 -90
- package/src/utils/npmUtils.js +0 -126
- package/src/utils/tkitUtils.js +0 -84
- package/src/webpack/base.js +0 -65
- package/src/webpack/build.js +0 -21
- package/src/webpack/buildServer.js +0 -34
- package/src/webpack/dev.js +0 -31
- package/src/webpack/devServer.js +0 -37
- package/src/webpack/plugins/entryExtractPlugin/index.js +0 -28
- package/src/webpack/utils.js +0 -244
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 用来读取处理tms.config.js与module.config.json字段
|
|
3
|
+
*/
|
|
4
|
+
const loadash = require('lodash');
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const { TMS_NAME, TMS_CONFIG_FILENAME, MODULE_CONFIG_FILENAME, TMS_PRIVATE_FILENAME } = require('../config/constant');
|
|
7
|
+
const { resolve, isObject, isArray } = require('../utils/widgets');
|
|
8
|
+
const defaultTmsConfig = require('../config/defaultTmsConfig');
|
|
9
|
+
const { fail } = require('../utils/log');
|
|
10
|
+
const path = require('path');
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 读取tms.config.js
|
|
14
|
+
* @param env {string} 环境变量
|
|
15
|
+
*/
|
|
16
|
+
const readTmsConfig = function (env) {
|
|
17
|
+
const tmsConfigPath = resolve(TMS_CONFIG_FILENAME);
|
|
18
|
+
if (!fs.existsSync(tmsConfigPath)) {
|
|
19
|
+
fail('当前执行目录没有tms.config.js的配置项,请进行配置');
|
|
20
|
+
process.exit(1);
|
|
21
|
+
}
|
|
22
|
+
const tmsConfigFn = require(tmsConfigPath);
|
|
23
|
+
const tmsConfig = tmsConfigFn({
|
|
24
|
+
env,
|
|
25
|
+
});
|
|
26
|
+
// 合并默认值
|
|
27
|
+
loadash.mergeWith(tmsConfig, defaultTmsConfig);
|
|
28
|
+
|
|
29
|
+
// modules兼容处理
|
|
30
|
+
tmsConfig.modules = convertModules(tmsConfig.modules);
|
|
31
|
+
return tmsConfig;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// convertModules 处理默认值
|
|
35
|
+
const convertModules = (modules) => {
|
|
36
|
+
const newModules = [];
|
|
37
|
+
modules.forEach((module) => {
|
|
38
|
+
const newModule = {};
|
|
39
|
+
if (typeof module === 'string') {
|
|
40
|
+
// 路径字符串
|
|
41
|
+
Object.assign(newModule, {
|
|
42
|
+
name: path.basename(module),
|
|
43
|
+
path: module,
|
|
44
|
+
});
|
|
45
|
+
} else if (typeof module === 'object') {
|
|
46
|
+
Object.assign(newModule, module);
|
|
47
|
+
if (module.name === undefined) {
|
|
48
|
+
newModule.name = path.basename(module.path);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
newModules.push(newModule);
|
|
52
|
+
});
|
|
53
|
+
return newModules;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 读取tms.private.config.js
|
|
58
|
+
*/
|
|
59
|
+
const readTmsPrivateCf = function () {
|
|
60
|
+
let tmsPrivateCf = {};
|
|
61
|
+
const tmsPrivatePath = resolve(TMS_PRIVATE_FILENAME);
|
|
62
|
+
if (fs.existsSync(tmsPrivatePath)) {
|
|
63
|
+
tmsPrivateCf = require(tmsPrivatePath);
|
|
64
|
+
}
|
|
65
|
+
// 处理modules字段
|
|
66
|
+
if (tmsPrivateCf.modules instanceof Array) {
|
|
67
|
+
Object.assign(tmsPrivateCf.modules, {
|
|
68
|
+
include: tmsPrivateCf.modules,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
return tmsPrivateCf;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 从tms.config.json中检索用户传入的有效modules
|
|
76
|
+
* @param { object } tmsConfig
|
|
77
|
+
* @param { array } modules
|
|
78
|
+
* @returns
|
|
79
|
+
*/
|
|
80
|
+
const checkModules = function (tmsConfig, modules) {
|
|
81
|
+
const targetModules = [];
|
|
82
|
+
modules.forEach((moduleName) => {
|
|
83
|
+
const module = tmsConfig.modules.find(module => module.name === moduleName);
|
|
84
|
+
module && targetModules.push(module);
|
|
85
|
+
});
|
|
86
|
+
if (targetModules.length === 0) {
|
|
87
|
+
fail(`你启动的模块无效,尝试 ${TMS_NAME} -m moduleName`);
|
|
88
|
+
process.exit(1);
|
|
89
|
+
}
|
|
90
|
+
return targetModules;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 过滤页面为空的分包
|
|
95
|
+
* @param {Array} moduleCfg 模块配置内容
|
|
96
|
+
* @returns pages不为空的分包
|
|
97
|
+
*/
|
|
98
|
+
const getValidModules = (moduleCfg) => {
|
|
99
|
+
// 过滤 pages 为空的情况
|
|
100
|
+
const validModules = moduleCfg.filter(item => item.pages.length > 0);
|
|
101
|
+
return validModules;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* 适配处理module.config.json的字段
|
|
106
|
+
* @param { object } fileContent module.config.json的内容
|
|
107
|
+
* @param { string } appName 小程序的名称
|
|
108
|
+
*/
|
|
109
|
+
function adaptMpCgContent(fileContent, appName) {
|
|
110
|
+
const content = fileContent.contents ? JSON.parse(fileContent.contents.toString()) : JSON.parse(fileContent);
|
|
111
|
+
|
|
112
|
+
if (isArray(content)) {
|
|
113
|
+
let i = content.length - 1;
|
|
114
|
+
while (i >= 0) {
|
|
115
|
+
let current = content[i];
|
|
116
|
+
|
|
117
|
+
if (appName && current.mpConfig && current.mpConfig[appName]) {
|
|
118
|
+
current = { ...current, ...current.mpConfig[appName] };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
delete current.mpConfig;
|
|
122
|
+
delete current.isSubpackages;
|
|
123
|
+
|
|
124
|
+
content[i] = current;
|
|
125
|
+
i--; // eslint-disable-line
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return content;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 递归获取本地所有模块的配置信息
|
|
133
|
+
* @param {array} modules 用户要编译的模块列表
|
|
134
|
+
* @param { string } appName 小程序的名称
|
|
135
|
+
* @param { string } moduleConfigFilename moduleConfig的文件名
|
|
136
|
+
*/
|
|
137
|
+
function getModuleConfig(modules = [], appName, moduleConfigFilename) {
|
|
138
|
+
const modulesConfig = {};
|
|
139
|
+
|
|
140
|
+
modules.forEach(({ path }) => {
|
|
141
|
+
const moduleConfigPath = resolve(path, moduleConfigFilename);
|
|
142
|
+
if (fs.existsSync(moduleConfigPath)) {
|
|
143
|
+
const content = fs.readFileSync(moduleConfigPath, 'utf-8');
|
|
144
|
+
modulesConfig[moduleConfigPath] = adaptMpCgContent(content, appName);
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
return modulesConfig;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* tms.config.js的modules 合并 module.config.json的配置项
|
|
153
|
+
* @param {array} modules
|
|
154
|
+
* @param {string} appName
|
|
155
|
+
* @param {string} moduleDir
|
|
156
|
+
* @returns
|
|
157
|
+
*/
|
|
158
|
+
const tmsModulesMergeLocalModuleCfg = (modules, appName) => {
|
|
159
|
+
const newModules = [];
|
|
160
|
+
modules.forEach(({ path: relativePath, name: moduleName }, moduleIndex) => {
|
|
161
|
+
const moduleConfigPath = resolve(relativePath, MODULE_CONFIG_FILENAME);
|
|
162
|
+
if (fs.existsSync(moduleConfigPath)) {
|
|
163
|
+
try {
|
|
164
|
+
let findModule = false;
|
|
165
|
+
let moduleConfigContent = fs.readFileSync(moduleConfigPath, 'utf-8');
|
|
166
|
+
moduleConfigContent = adaptMpCgContent(moduleConfigContent, appName);
|
|
167
|
+
const moduleContentArr = isObject(moduleConfigContent) ? [moduleConfigContent] : moduleConfigContent;
|
|
168
|
+
getValidModules(moduleContentArr).forEach(({ name }, moduleContentArrIndex) => {
|
|
169
|
+
if (name === moduleName) {
|
|
170
|
+
findModule = true;
|
|
171
|
+
newModules.push({
|
|
172
|
+
...modules[moduleIndex],
|
|
173
|
+
...moduleContentArr[moduleContentArrIndex],
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
if (!findModule) {
|
|
178
|
+
fail(`启动模块${moduleName}在${moduleConfigPath}没有找到,请检查配置`);
|
|
179
|
+
process.exit(1);
|
|
180
|
+
}
|
|
181
|
+
} catch (e) {
|
|
182
|
+
fail(`${moduleConfigPath}配置错误: ${e}`);
|
|
183
|
+
newModules.push({
|
|
184
|
+
...modules[moduleIndex],
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
} else {
|
|
188
|
+
newModules.push({
|
|
189
|
+
...modules[moduleIndex],
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
return newModules;
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 分包依赖了分包的模块 合并所依赖的modules
|
|
198
|
+
* @param { object } tmsConfig
|
|
199
|
+
* @param {array} modules
|
|
200
|
+
* @param {string} moduleDir
|
|
201
|
+
* @returns
|
|
202
|
+
*/
|
|
203
|
+
const subModulesMergeDepModules = (tmsConfig, modules) => {
|
|
204
|
+
const moduleNames = [];
|
|
205
|
+
modules.forEach(({ name: moduleName }) => {
|
|
206
|
+
moduleNames.push(moduleName);
|
|
207
|
+
});
|
|
208
|
+
let mergeModules = modules;
|
|
209
|
+
let isOver = true;
|
|
210
|
+
|
|
211
|
+
modules.forEach(({ dependencies: dependencyModules }) => {
|
|
212
|
+
dependencyModules?.forEach((item) => {
|
|
213
|
+
// 如果所有模块的dep都在moduleNames内,则所有依赖都齐了
|
|
214
|
+
// 否则递归处理,根据name找到相关配置加到modules里
|
|
215
|
+
if (moduleNames.indexOf(item) === -1) {
|
|
216
|
+
isOver = false;
|
|
217
|
+
const tmpModules = checkModules(tmsConfig, [...new Set([item])]);
|
|
218
|
+
mergeModules = [...mergeModules, ...tmpModules];
|
|
219
|
+
mergeModules = tmsModulesMergeLocalModuleCfg(mergeModules, tmsConfig.appName);
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
return isOver ? mergeModules : subModulesMergeDepModules(tmsConfig, mergeModules);
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
module.exports = {
|
|
227
|
+
readTmsConfig,
|
|
228
|
+
readTmsPrivateCf,
|
|
229
|
+
getModuleConfig,
|
|
230
|
+
getValidModules,
|
|
231
|
+
checkModules,
|
|
232
|
+
tmsModulesMergeLocalModuleCfg,
|
|
233
|
+
subModulesMergeDepModules,
|
|
234
|
+
};
|
package/src/entry.js
CHANGED
|
@@ -7,14 +7,56 @@ module.exports = [
|
|
|
7
7
|
},
|
|
8
8
|
},
|
|
9
9
|
{
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
name: 'run',
|
|
11
|
+
type: 'child',
|
|
12
|
+
description: '项目开发使用的命令',
|
|
13
|
+
commands: [
|
|
14
|
+
{
|
|
15
|
+
command: 'install',
|
|
16
|
+
description: '安装依赖',
|
|
17
|
+
options: [
|
|
18
|
+
['-m, --module [moduleName]', '模块名称'],
|
|
19
|
+
['-e, --env [env]', '环境变量'],
|
|
20
|
+
],
|
|
21
|
+
action: (cmd) => {
|
|
22
|
+
require('./scripts/run/index')('install', cmd);
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
command: 'dev',
|
|
27
|
+
description: 'dev 打包编译',
|
|
28
|
+
options: [
|
|
29
|
+
['-m, --module [moduleName]', '模块名称'],
|
|
30
|
+
['-e, --env [env]', '环境变量'],
|
|
31
|
+
['-latest, --latest', '下载最新第三方模块代码、安装最新依赖'],
|
|
32
|
+
],
|
|
33
|
+
action: (cmd) => {
|
|
34
|
+
require('./scripts/run/index')('dev', cmd);
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
command: 'build',
|
|
39
|
+
description: 'prod 打包编译',
|
|
40
|
+
options: [
|
|
41
|
+
['-m, --module [moduleName]', '模块名称'],
|
|
42
|
+
['-e, --env [env]', '环境变量'],
|
|
43
|
+
],
|
|
44
|
+
action: (cmd) => {
|
|
45
|
+
require('./scripts/run/index')('build', cmd);
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
// 对外暂不暴露该命令
|
|
49
|
+
// {
|
|
50
|
+
// command: 'init',
|
|
51
|
+
// description: '模块配置初始化项目(eg: 下载第三方模块代码、安装依赖、生成app.json等)',
|
|
52
|
+
// options: [
|
|
53
|
+
// ['-m, --module [moduleName]', '模块名称'],
|
|
54
|
+
// ['-e, --env [env]', '环境变量'],
|
|
55
|
+
// ],
|
|
56
|
+
// action: (cmd) => {
|
|
57
|
+
// require('./scripts/run/index')('init', cmd);
|
|
58
|
+
// },
|
|
59
|
+
// },
|
|
14
60
|
],
|
|
15
|
-
description: '运行模块',
|
|
16
|
-
action: (commandName, cmd) => {
|
|
17
|
-
require('./scripts/run/index')(commandName, cmd);
|
|
18
|
-
},
|
|
19
61
|
},
|
|
20
62
|
];
|
package/src/index.js
CHANGED
|
@@ -1,31 +1,47 @@
|
|
|
1
1
|
const chalk = require('chalk');
|
|
2
|
-
const
|
|
3
|
-
const {
|
|
2
|
+
const commander = require('commander');
|
|
3
|
+
const { suggestCommands } = require('./utils/widgets');
|
|
4
|
+
const { info } = require('./utils/log');
|
|
4
5
|
const { TMS_NAME } = require('./config/constant.js');
|
|
5
6
|
const commands = require('./entry');
|
|
6
7
|
const init = require('./init');
|
|
7
8
|
|
|
8
9
|
init();
|
|
9
10
|
|
|
11
|
+
const program = new commander.Command(TMS_NAME);
|
|
12
|
+
|
|
10
13
|
program
|
|
11
|
-
.version(`${TMS_NAME} ${require('../package.json').version}
|
|
14
|
+
.version(`${TMS_NAME} ${require('../package.json').version}`, '-v, -V, --version');
|
|
12
15
|
|
|
13
|
-
commands
|
|
14
|
-
|
|
16
|
+
function registerCommand(program, commands) {
|
|
17
|
+
commands.forEach((cmd) => {
|
|
18
|
+
if (cmd.type === 'child') {
|
|
19
|
+
const childProgram = new commander.Command(cmd.name);
|
|
20
|
+
cmd.usage && childProgram.usage(cmd.usage);
|
|
21
|
+
cmd.description && childProgram.description(cmd.description);
|
|
22
|
+
registerCommand(childProgram, cmd.commands);
|
|
15
23
|
|
|
16
|
-
|
|
24
|
+
program.addCommand(childProgram);
|
|
25
|
+
} else {
|
|
26
|
+
const command = program.command(cmd.command);
|
|
17
27
|
|
|
18
|
-
|
|
28
|
+
cmd.usage && command.usage(cmd.usage);
|
|
19
29
|
|
|
20
|
-
|
|
30
|
+
cmd.description && command.description(cmd.description);
|
|
21
31
|
|
|
22
|
-
|
|
23
|
-
|
|
32
|
+
cmd.options?.forEach(opt => command.option(...opt));
|
|
33
|
+
|
|
34
|
+
command.action(cmd.action);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
registerCommand(program, commands);
|
|
24
40
|
|
|
25
41
|
program.on('--help', () => {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
42
|
+
info();
|
|
43
|
+
info(` Run ${chalk.cyan(`${TMS_NAME} <command> --help`)} for detailed usage of given command.`);
|
|
44
|
+
info();
|
|
29
45
|
});
|
|
30
46
|
|
|
31
47
|
// 捕获未注册的命令
|
|
@@ -33,8 +49,8 @@ program
|
|
|
33
49
|
.arguments('<command>')
|
|
34
50
|
.action((cmd) => {
|
|
35
51
|
program.outputHelp();
|
|
36
|
-
|
|
37
|
-
|
|
52
|
+
info(` ${chalk.red(`Unknown command ${chalk.yellow(cmd)}.`)}`);
|
|
53
|
+
info();
|
|
38
54
|
suggestCommands(cmd);
|
|
39
55
|
process.exitCode = 1;
|
|
40
56
|
});
|
package/src/init.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
const { exec } = require('shelljs');
|
|
2
1
|
const semver = require('semver');
|
|
3
2
|
const packageJson = require('../package.json');
|
|
4
3
|
const chalk = require('chalk');
|
|
5
|
-
const {
|
|
4
|
+
const { info } = require('./utils/log');
|
|
6
5
|
|
|
7
6
|
const requiredVersion = packageJson.engines.node;
|
|
8
7
|
const packName = packageJson.name;
|
|
@@ -15,7 +14,7 @@ const packName = packageJson.name;
|
|
|
15
14
|
*/
|
|
16
15
|
const checkNodeVersion = (wanted, id) => {
|
|
17
16
|
if (!semver.satisfies(process.version, wanted, { includePrerelease: true })) {
|
|
18
|
-
|
|
17
|
+
info(chalk.red(`你正在使用的Node版本: ${process.version}, 但是此版本的 ${id
|
|
19
18
|
} 需要的Node最小版本 ${wanted}.\n请先升级你的Node版本.`));
|
|
20
19
|
process.exit(1);
|
|
21
20
|
}
|
|
@@ -29,10 +28,6 @@ function initCliContext() {
|
|
|
29
28
|
// 执行操作前检查node版本
|
|
30
29
|
// 旧版本node直接提示升级,退出脚本
|
|
31
30
|
checkNodeVersion(requiredVersion, packName);
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
// 执行前配置正确的npm源
|
|
35
|
-
exec('npm config set registry https://mirrors.tencent.com/npm/ --global', { silent: true });
|
|
36
31
|
}
|
|
37
32
|
|
|
38
33
|
module.exports = initCliContext;
|
|
@@ -4,7 +4,7 @@ const shelljs = require('shelljs');
|
|
|
4
4
|
const { TEMPLATE_DIR, TEMPLATE_PATH, TEMPLATE_TKIT_DIR } = require('../../config/constant.js');
|
|
5
5
|
const { downloadRepoForGit, createTask, resolve } = require('../../utils/widgets');
|
|
6
6
|
const io = require('../../utils/io');
|
|
7
|
-
const { fail, succeed } = require('../../utils/log');
|
|
7
|
+
const { fail, succeed, info } = require('../../utils/log');
|
|
8
8
|
const generator = require('./generator');
|
|
9
9
|
|
|
10
10
|
/**
|
|
@@ -65,7 +65,7 @@ async function create(appName) {
|
|
|
65
65
|
})
|
|
66
66
|
.catch((err) => {
|
|
67
67
|
fail(err.message);
|
|
68
|
-
|
|
68
|
+
info('详细的错误信息:', err);
|
|
69
69
|
});
|
|
70
70
|
}
|
|
71
71
|
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
const shelljs = require('shelljs');
|
|
2
|
-
const webpackBuildServer = require('../../../webpack/buildServer');
|
|
3
2
|
const { resolve } = require('../../../utils/widgets');
|
|
4
3
|
const init = require('../init/index');
|
|
4
|
+
const compileBuild = require('../../../compile/build');
|
|
5
5
|
|
|
6
6
|
async function build(tmsConfig, targetModules, env) {
|
|
7
7
|
// 开始构建前,清理输出目录
|
|
8
|
-
await shelljs.rm('-rf', resolve(
|
|
8
|
+
await shelljs.rm('-rf', resolve(tmsConfig.outputDir));
|
|
9
9
|
|
|
10
10
|
const { targetModules: newModules } = await init(tmsConfig, targetModules);
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
compileBuild(tmsConfig, newModules, env);
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
module.exports = build;
|
|
@@ -1,66 +1,41 @@
|
|
|
1
|
-
const
|
|
2
|
-
const
|
|
1
|
+
const shelljs = require('shelljs');
|
|
2
|
+
const compileDev = require('../../../compile/dev');
|
|
3
3
|
const { resolve } = require('../../../utils/widgets');
|
|
4
4
|
const init = require('../init/index');
|
|
5
|
-
const {
|
|
6
|
-
const { tmsModulesMergeLocalModuleCfg } = require('../../../
|
|
7
|
-
const {
|
|
8
|
-
const {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
if (!fs.existsSync(`${contextDir}/miniprogram_npm`)) {
|
|
22
|
-
return true;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// 判断dist是否存在app.json
|
|
26
|
-
if (!fs.existsSync(`${contextDir}/app.json`)) {
|
|
27
|
-
return true;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// 判断\源码\dist\是否存在用户指定的模块
|
|
31
|
-
for (const item of targetModules) {
|
|
32
|
-
// 此模块没有root字段(原因:没有merge到module.config.json的配置项。第三方模块的代码可能还没有下载)
|
|
33
|
-
if (!item.root) {
|
|
34
|
-
return true;
|
|
35
|
-
}
|
|
36
|
-
// 判断编译目录是否有该模块
|
|
37
|
-
if (!fs.existsSync(`${contextDir}/${item.root}`)) {
|
|
38
|
-
return true;
|
|
5
|
+
const { isInit } = require('../../../core/isInIt');
|
|
6
|
+
const { tmsModulesMergeLocalModuleCfg } = require('../../../core/tmsMpconfig');
|
|
7
|
+
const { info } = require('../../../utils/log');
|
|
8
|
+
const { global } = require('../../../utils/global');
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
// 用户编译分包时,需要将dist中其他分包(主包不能删除)的内容删除,否则其他分包的内容混入到主包(导致主包的体积超2M)
|
|
12
|
+
function delOtherModule(tmsConfig, targetModules) {
|
|
13
|
+
const modules = tmsModulesMergeLocalModuleCfg(tmsConfig.modules, tmsConfig.appName);
|
|
14
|
+
const targetModulesName = targetModules.map(item => item.name);
|
|
15
|
+
modules.forEach((item) => {
|
|
16
|
+
if (item.root && targetModulesName.indexOf(item.name) === -1) {
|
|
17
|
+
const moduleRootDir = resolve(`dist/${item.root}`);
|
|
18
|
+
shelljs.rm('-rf', `${moduleRootDir}/*`, { silent: true });
|
|
19
|
+
// 解决微信开发者工具(dist/app.json: ["subpackages"][0]["root"] 字段需为 目录)错误 - 提前创建该目录
|
|
20
|
+
// io.ensureDirExist(moduleRootDir);
|
|
39
21
|
}
|
|
40
|
-
|
|
41
|
-
if (item.path && !fs.existsSync(resolve(item.path))) {
|
|
42
|
-
fail(`${item.path}模块代码路径不存在, 请检查tms.config.js的${item.name}模块的path`);
|
|
43
|
-
process.exit(1);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// 判断package.json的版本是否有新的版本
|
|
48
|
-
return checkDependencies(targetModules, resolve('./'), tmsConfig.webpack.outputDir);
|
|
22
|
+
});
|
|
49
23
|
}
|
|
50
24
|
|
|
51
25
|
async function dev(tmsConfig, targetModules, env) {
|
|
52
|
-
|
|
53
|
-
|
|
26
|
+
let newModules = targetModules;
|
|
27
|
+
const isLatest = global.getData('cmd').latest;
|
|
54
28
|
|
|
55
29
|
// 判断是否进行init命令
|
|
56
|
-
if (isInit(tmsConfig,
|
|
57
|
-
// init
|
|
58
|
-
const initData = await init(tmsConfig,
|
|
30
|
+
if (isLatest || isInit(tmsConfig, targetModules, resolve('dist'))) {
|
|
31
|
+
// init函数 下载第三方代码后,会将最新的tms.config.js的modules 合并 module.config.json的配置项返回
|
|
32
|
+
const initData = await init(tmsConfig, newModules);
|
|
59
33
|
newModules = initData.targetModules;
|
|
60
34
|
}
|
|
61
35
|
|
|
62
|
-
|
|
63
|
-
|
|
36
|
+
info('当前dev启动的有效模块', newModules.map(item => item.name).sort());
|
|
37
|
+
delOtherModule(tmsConfig, newModules);
|
|
38
|
+
compileDev(tmsConfig, newModules, env);
|
|
64
39
|
}
|
|
65
40
|
|
|
66
41
|
module.exports = dev;
|
package/src/scripts/run/index.js
CHANGED
|
@@ -2,58 +2,92 @@ const init = require('./init/index');
|
|
|
2
2
|
const dev = require('./dev/index');
|
|
3
3
|
const build = require('./build/index');
|
|
4
4
|
const install = require('./install/index');
|
|
5
|
-
const {
|
|
6
|
-
const {
|
|
7
|
-
|
|
5
|
+
const { global } = require('../../utils/global');
|
|
6
|
+
const {
|
|
7
|
+
readTmsConfig,
|
|
8
|
+
readTmsPrivateCf,
|
|
9
|
+
checkModules,
|
|
10
|
+
tmsModulesMergeLocalModuleCfg,
|
|
11
|
+
subModulesMergeDepModules,
|
|
12
|
+
} = require('../../core/tmsMpconfig');
|
|
8
13
|
|
|
9
|
-
const
|
|
10
|
-
//
|
|
11
|
-
if (cmd.module ===
|
|
12
|
-
return
|
|
14
|
+
const handleModuleArg = (cmd) => {
|
|
15
|
+
// 单模块或多模块开发-用户通过脚手架参数指定的模块
|
|
16
|
+
if (typeof cmd.module === 'string') {
|
|
17
|
+
return cmd.module.split(',');
|
|
13
18
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
return [];
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* 获取用户指定要跑的模块
|
|
23
|
+
* @param {Array} moduleArg 参数指定的模块
|
|
24
|
+
* @param {Object} modulePrivateCfg 私有配置里的模块
|
|
25
|
+
* @param {Array} moduleAll 当前小程序全部模块
|
|
26
|
+
*/
|
|
27
|
+
const getSpecificModules = (moduleArg, modulePrivateCfg, moduleAll) => {
|
|
28
|
+
if (moduleArg.length > 0) {
|
|
29
|
+
return moduleArg;
|
|
30
|
+
}
|
|
31
|
+
// 单模块或多模块开发-用户在tms.private.js指定的模块
|
|
32
|
+
if (modulePrivateCfg) {
|
|
33
|
+
if (modulePrivateCfg.include?.length > 0) {
|
|
34
|
+
return modulePrivateCfg.include;
|
|
35
|
+
}
|
|
36
|
+
if (modulePrivateCfg.exclude?.length > 0) {
|
|
37
|
+
return moduleAll.filter(module => !modulePrivateCfg.exclude.includes(module.name)).map(item => item.name);
|
|
38
|
+
}
|
|
39
|
+
if (modulePrivateCfg.blockRemote === true) {
|
|
40
|
+
return moduleAll.filter(module => module.repoInfo === undefined).map(item => item.name);
|
|
41
|
+
}
|
|
20
42
|
}
|
|
21
|
-
|
|
22
|
-
return MODE.all;
|
|
43
|
+
return moduleAll.map(item => item.name);
|
|
23
44
|
};
|
|
24
45
|
|
|
25
46
|
async function run(commandName, cmd) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
47
|
+
// 用户本地的私有项目配置(用来配置环境\模块信息\账号信息)
|
|
48
|
+
const tmsPrivateCf = readTmsPrivateCf();
|
|
49
|
+
const { env = tmsPrivateCf?.env } = cmd;
|
|
50
|
+
const tmsConfig = readTmsConfig(env);
|
|
51
|
+
// 处理module参数
|
|
52
|
+
const specificModules = getSpecificModules(
|
|
53
|
+
handleModuleArg(cmd),
|
|
54
|
+
tmsPrivateCf.modules,
|
|
55
|
+
tmsConfig.modules,
|
|
56
|
+
);
|
|
30
57
|
|
|
31
|
-
const
|
|
32
|
-
|
|
58
|
+
const modules = checkModules(tmsConfig, [
|
|
59
|
+
...new Set([
|
|
60
|
+
...tmsConfig.mainPackages,
|
|
61
|
+
...specificModules,
|
|
62
|
+
]),
|
|
63
|
+
]);
|
|
33
64
|
|
|
34
|
-
|
|
65
|
+
// tms.config.js的modules 合并 module.config.json的配置项
|
|
66
|
+
let newModules = tmsModulesMergeLocalModuleCfg(modules, tmsConfig.appName);
|
|
67
|
+
// A分包依赖了B分包的代码, merge B分包进行编译;
|
|
68
|
+
newModules = subModulesMergeDepModules(tmsConfig, newModules);
|
|
35
69
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
modules = tmsConfig.modules;
|
|
43
|
-
} else {
|
|
44
|
-
// 检查用户输入modules的有效性
|
|
45
|
-
modules = checkModules(tmsConfig, [...new Set([...tmsConfig.mainPackages, ...moduleArg])]);;
|
|
46
|
-
}
|
|
70
|
+
// 缓存数据
|
|
71
|
+
global.setData({
|
|
72
|
+
env,
|
|
73
|
+
cmd,
|
|
74
|
+
tmsPrivateCf,
|
|
75
|
+
});
|
|
47
76
|
|
|
48
77
|
switch (commandName) {
|
|
49
78
|
case 'init':
|
|
50
|
-
init(tmsConfig,
|
|
79
|
+
init(tmsConfig, newModules, env);
|
|
51
80
|
return;
|
|
52
81
|
case 'dev':
|
|
53
|
-
|
|
82
|
+
global.setData('isDev', true);
|
|
83
|
+
dev(tmsConfig, newModules, env);
|
|
84
|
+
return;
|
|
85
|
+
case 'install':
|
|
86
|
+
install(tmsConfig, newModules, env);
|
|
54
87
|
return;
|
|
55
88
|
case 'build':
|
|
56
|
-
|
|
89
|
+
global.setData('isDev', false);
|
|
90
|
+
build(tmsConfig, newModules, env);
|
|
57
91
|
return;
|
|
58
92
|
default:
|
|
59
93
|
return;
|