@tmsfe/tmskit 0.0.16 → 0.0.19
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 +45 -0
- package/README.md +2 -3
- package/dist/index.cjs.js +701 -417
- package/main.js +0 -0
- package/package.json +2 -2
- package/src/compile/dev.js +19 -14
- package/src/compile/plugins/mpJsonDep.js +1 -1
- package/src/config/constant.js +6 -3
- package/src/config/defaultTmsConfig.js +1 -1
- package/src/core/buildAppJson.js +42 -46
- package/src/core/cache.js +36 -0
- package/src/core/checkDependencies.js +2 -2
- package/src/core/cloneModules.js +3 -3
- package/src/core/npm.js +3 -2
- package/src/core/tmsMpconfig.js +21 -60
- package/src/entry.js +11 -0
- package/src/scripts/run/build/index.js +7 -1
- package/src/scripts/run/cloud/index.js +12 -0
- package/src/scripts/run/dev/index.js +8 -4
- package/src/scripts/run/index.js +38 -17
- package/src/scripts/run/init/index.js +2 -4
- package/src/scripts/run/install/index.js +109 -18
- package/src/utils/md5.js +25 -0
- package/src/utils/widgets.js +31 -0
- package/src/core/isInIt.js +0 -65
package/main.js
CHANGED
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tmsfe/tmskit",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.19",
|
|
4
4
|
"description": "tmskit",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"bin": {
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
"lodash": "^4.17.21",
|
|
65
65
|
"metalsmith": "^2.3.0",
|
|
66
66
|
"miniprogram-ci": "1.4.13",
|
|
67
|
-
"moment": "^2.29.
|
|
67
|
+
"moment": "^2.29.2",
|
|
68
68
|
"object-assign": "^4.0.1",
|
|
69
69
|
"ora": "^5.1.0",
|
|
70
70
|
"plugin-error": "^1.0.0",
|
package/src/compile/dev.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
const path = require('path');
|
|
2
2
|
const fs = require('fs');
|
|
3
3
|
const ora = require('ora');
|
|
4
|
+
const chalk = require('chalk');
|
|
4
5
|
const { parallel, series } = require('gulp');
|
|
5
|
-
const { resolve, mergeMap } = require('../utils/widgets');
|
|
6
|
+
const { resolve, mergeMap, filterField } = require('../utils/widgets');
|
|
6
7
|
const { buildOutputAppJson } = require('../core/buildAppJson');
|
|
7
8
|
const { DEFAULT_COPY_CONFIG } = require('../config/constant');
|
|
8
9
|
const compile = require('./compile');
|
|
@@ -37,17 +38,6 @@ function adaptPath(pathDir) {
|
|
|
37
38
|
}
|
|
38
39
|
module.exports = async (tmsConfig, newModules, isDev = true) => {
|
|
39
40
|
const compileTasksMap = new Map();
|
|
40
|
-
// 监听app.json
|
|
41
|
-
if (isDev) {
|
|
42
|
-
watch(
|
|
43
|
-
[resolve('app.json')],
|
|
44
|
-
{ ignoreInitial: false, events: watchEvents },
|
|
45
|
-
() => buildOutputAppJson(tmsConfig, newModules, isDev),
|
|
46
|
-
{ from: resolve(), to: resolve(tmsConfig.outputDir) },
|
|
47
|
-
);
|
|
48
|
-
} else {
|
|
49
|
-
buildOutputAppJson(tmsConfig, newModules, isDev);
|
|
50
|
-
}
|
|
51
41
|
|
|
52
42
|
// 监听根目录的文件
|
|
53
43
|
mergeMap(compileTasksMap, compile(tmsConfig, {
|
|
@@ -148,15 +138,29 @@ module.exports = async (tmsConfig, newModules, isDev = true) => {
|
|
|
148
138
|
}
|
|
149
139
|
async function end(cb) {
|
|
150
140
|
if (isDev) {
|
|
141
|
+
// 监听app.json
|
|
142
|
+
watch(
|
|
143
|
+
[resolve('app.json')],
|
|
144
|
+
{ ignoreInitial: false, events: watchEvents },
|
|
145
|
+
() => buildOutputAppJson(tmsConfig, newModules, isDev),
|
|
146
|
+
{ from: resolve(), to: resolve(tmsConfig.outputDir) },
|
|
147
|
+
);
|
|
148
|
+
// 监听其他文件
|
|
151
149
|
compileTasksMap.forEach(({ taskFn, module }, globValue) => {
|
|
152
150
|
watch(globValue, { ignoreInitial: true, events: watchEvents }, taskFn, module);
|
|
153
151
|
});
|
|
152
|
+
} else {
|
|
153
|
+
buildOutputAppJson(tmsConfig, newModules, isDev);
|
|
154
154
|
}
|
|
155
155
|
eTime = new Date().getTime() - sTime;
|
|
156
156
|
if (typeof tmsConfig?.hooks?.afterCompile === 'function') {
|
|
157
|
-
await tmsConfig?.hooks?.afterCompile({
|
|
157
|
+
await tmsConfig?.hooks?.afterCompile({
|
|
158
|
+
isDev,
|
|
159
|
+
tmsConfig: filterField(tmsConfig, ['gitAccount']),
|
|
160
|
+
modules: newModules,
|
|
161
|
+
});
|
|
158
162
|
}
|
|
159
|
-
spinner.succeed(`首次编译完成, 耗时${eTime / 1000}s, 微信开发者工具打开项目即可预览。`);
|
|
163
|
+
spinner.succeed(chalk.green(`首次编译完成, 耗时${eTime / 1000}s, 微信开发者工具打开项目即可预览。`));
|
|
160
164
|
spinner.stop();
|
|
161
165
|
cb();
|
|
162
166
|
}
|
|
@@ -165,5 +169,6 @@ module.exports = async (tmsConfig, newModules, isDev = true) => {
|
|
|
165
169
|
compileTasksMap.forEach(({ taskFn }) => {
|
|
166
170
|
compileTasks.push(taskFn);
|
|
167
171
|
});
|
|
172
|
+
// 一次性完成编译任务(编译完成后再添加watch任务-封装到end函数里面)
|
|
168
173
|
series(start, parallel(...compileTasks), end)();
|
|
169
174
|
};
|
|
@@ -49,7 +49,7 @@ function mpJsonDep(
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
// eslint-disable-next-line
|
|
52
|
-
const reg = new RegExp(`^(
|
|
52
|
+
const reg = new RegExp(`^(\./|\.\.\/)+.*\/${includeName}\/(.*)`);
|
|
53
53
|
const regRes = componentPath.match(reg) || [];
|
|
54
54
|
if (regRes[2]) {
|
|
55
55
|
const depDestPath = resolve(tmsConfig.outputDir, module.to, includeName, regRes[2]);
|
package/src/config/constant.js
CHANGED
|
@@ -7,6 +7,9 @@ const HOME_DIR = os.homedir();
|
|
|
7
7
|
// 所有文件的缓存目录
|
|
8
8
|
const CACHE_DIR = path.resolve(HOME_DIR, '.tmskit');
|
|
9
9
|
|
|
10
|
+
// 缓存文件
|
|
11
|
+
const CACHE_FILE = 'cache_file.json';
|
|
12
|
+
|
|
10
13
|
// 脚手架模板代码所在目录
|
|
11
14
|
const TEMPLATE_DIR = path.resolve(CACHE_DIR, 'template');
|
|
12
15
|
|
|
@@ -14,7 +17,7 @@ const TEMPLATE_DIR = path.resolve(CACHE_DIR, 'template');
|
|
|
14
17
|
const MODULE_CODE_DIR = path.resolve(CACHE_DIR, 'modules_code');
|
|
15
18
|
|
|
16
19
|
// 脚手架模板代码的具体路径
|
|
17
|
-
const TEMPLATE_PATH = path.resolve(TEMPLATE_DIR, 'tools/
|
|
20
|
+
const TEMPLATE_PATH = path.resolve(TEMPLATE_DIR, 'tools/tmskit-template');
|
|
18
21
|
|
|
19
22
|
// 脚手架的名称
|
|
20
23
|
const TMS_NAME = 'tmskit';
|
|
@@ -47,11 +50,12 @@ const ENV = {
|
|
|
47
50
|
};
|
|
48
51
|
|
|
49
52
|
const TEMPLATE_TKIT_DIR = '_tmskit';
|
|
50
|
-
|
|
53
|
+
|
|
51
54
|
|
|
52
55
|
export {
|
|
53
56
|
HOME_DIR,
|
|
54
57
|
CACHE_DIR,
|
|
58
|
+
CACHE_FILE,
|
|
55
59
|
TEMPLATE_DIR,
|
|
56
60
|
TEMPLATE_PATH,
|
|
57
61
|
TMS_NAME,
|
|
@@ -64,6 +68,5 @@ export {
|
|
|
64
68
|
MODULE_CODE_DIR,
|
|
65
69
|
ENV,
|
|
66
70
|
TEMPLATE_TKIT_DIR,
|
|
67
|
-
MODULE_CONFIG_INVALID_KEY,
|
|
68
71
|
DEFAULT_CLOUD_MODULE_DIR,
|
|
69
72
|
};
|
package/src/core/buildAppJson.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 生成编译后的app.json
|
|
3
3
|
*/
|
|
4
|
+
/* eslint-disable no-param-reassign */
|
|
4
5
|
const fs = require('fs');
|
|
5
|
-
const { MODULE_CONFIG_FILENAME
|
|
6
|
-
const { getModuleConfig
|
|
6
|
+
const { MODULE_CONFIG_FILENAME } = require('../config/constant');
|
|
7
|
+
const { getModuleConfig } = require('./tmsMpconfig');
|
|
7
8
|
const { fail } = require('../utils/log');
|
|
8
|
-
const { resolve, isObject } = require('../utils/widgets');
|
|
9
|
+
const { resolve, isObject, filterField } = require('../utils/widgets');
|
|
9
10
|
const { handleError } = require('./handleError');
|
|
11
|
+
const { global } = require('../utils/global');
|
|
10
12
|
|
|
11
13
|
/**
|
|
12
14
|
* 更新appJson里面的主包配置
|
|
@@ -36,7 +38,6 @@ function updateMainPackages(appJson, mainPackages = []) {
|
|
|
36
38
|
|
|
37
39
|
// 去掉 subpackages 中的主包配置
|
|
38
40
|
const foundMainPackageNames = foundMainPackages.map(item => item.name);
|
|
39
|
-
// eslint-disable-next-line
|
|
40
41
|
appJson.subpackages = appJson.subpackages.filter(subpackage => !foundMainPackageNames.includes(subpackage.name));
|
|
41
42
|
|
|
42
43
|
return appJson;
|
|
@@ -55,24 +56,20 @@ const getAppJsonContent = (sourceAppJsonPath) => {
|
|
|
55
56
|
// 加入默认值
|
|
56
57
|
appJson.subpackages = [];
|
|
57
58
|
appJson.pages = [];
|
|
58
|
-
// appJson.plugins = {};
|
|
59
|
-
delete appJson.entranceDeclare;
|
|
60
59
|
return appJson;
|
|
61
60
|
};
|
|
62
61
|
/**
|
|
63
62
|
* 更新app.json中的subpackages
|
|
64
63
|
* @param {Object} appJson
|
|
65
|
-
* @param {Object}
|
|
64
|
+
* @param {Object} modulesConfigs
|
|
66
65
|
*/
|
|
67
|
-
const updateSubpackages = (appJson,
|
|
66
|
+
const updateSubpackages = (appJson, modulesConfigs) => {
|
|
68
67
|
// eslint-disable-next-line
|
|
69
|
-
for (const
|
|
70
|
-
const moduleInfo = isObject(
|
|
71
|
-
|
|
72
|
-
const validModules = getValidModules(moduleInfo);
|
|
73
|
-
// eslint-disable-next-line
|
|
74
|
-
appJson.subpackages = appJson.subpackages.concat(validModules);
|
|
68
|
+
for (const modulePath in modulesConfigs) {
|
|
69
|
+
const moduleInfo = isObject(modulesConfigs[modulePath]) ? [modulesConfigs[modulePath]] : modulesConfigs[modulePath];
|
|
70
|
+
appJson.subpackages = appJson.subpackages.concat(moduleInfo);
|
|
75
71
|
}
|
|
72
|
+
appJson.subpackages.sort((item1, item2) => item1.name.localeCompare(item2.name));
|
|
76
73
|
};
|
|
77
74
|
|
|
78
75
|
/**
|
|
@@ -84,40 +81,35 @@ const fixAppJson = (appJson) => {
|
|
|
84
81
|
const pluginsMap = {};
|
|
85
82
|
Object.keys(appJson.plugins || {}).forEach(key => pluginsMap[key] = ['app.json']);
|
|
86
83
|
const subps = subpackages.map((subp) => {
|
|
87
|
-
const
|
|
84
|
+
const arrOfFileType = ['requiredBackgroundModes', 'embeddedAppIdList'];
|
|
85
|
+
const objOfFileType = ['preloadRule'];
|
|
88
86
|
Object.keys(subp).forEach((key) => {
|
|
89
|
-
if (key === 'dependencies') {
|
|
90
|
-
// eslint-disable-next-line
|
|
91
|
-
delete subp.dependencies;
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
87
|
if (key === 'plugins') {
|
|
95
88
|
Object.keys(subp.plugins).forEach((pk) => {
|
|
96
|
-
pluginsMap[pk]
|
|
89
|
+
pluginsMap[pk]
|
|
90
|
+
? pluginsMap[pk].push(`分包${subp.name}`)
|
|
91
|
+
: pluginsMap[pk] = [`分包${subp.name}`];
|
|
97
92
|
});
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
if (MODULE_CONFIG_INVALID_KEY.indexOf(key) > -1) {
|
|
101
|
-
// 如果分包配置中有不支持的key,则错误提醒
|
|
102
|
-
invalidKeys.push(key);
|
|
103
|
-
return;
|
|
104
93
|
}
|
|
105
|
-
|
|
106
|
-
|
|
94
|
+
// 分包里数组类型字段,提到appjson最上层
|
|
95
|
+
if (arrOfFileType.indexOf(key) > -1) {
|
|
107
96
|
const preVal = appJson[key];
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
97
|
+
preVal
|
|
98
|
+
? appJson[key] = Array.from(new Set(preVal.slice(0).concat(subp[key])))
|
|
99
|
+
: appJson[key] = subp[key].slice(0);
|
|
100
|
+
}
|
|
101
|
+
// 分包里对象类型字段,提到appjson最上层
|
|
102
|
+
if (objOfFileType.indexOf(key) > -1) {
|
|
103
|
+
const preloadRuleMap = appJson[key] || {};
|
|
104
|
+
subp[key] && Object.keys(subp[key]).forEach((page) => {
|
|
105
|
+
if (!preloadRuleMap[page]) {
|
|
106
|
+
preloadRuleMap[page] = subp[key][page];
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
appJson[key] = preloadRuleMap;
|
|
113
110
|
}
|
|
114
111
|
});
|
|
115
|
-
|
|
116
|
-
fail(`不支持分包${subp?.name}配置${invalidKeys.join(',')}\n`);
|
|
117
|
-
}
|
|
118
|
-
// eslint-disable-next-line
|
|
119
|
-
invalidKeys.concat(['requiredBackgroundModes', 'embeddedAppIdList']).forEach(k => delete subp[k]);
|
|
120
|
-
return subp;
|
|
112
|
+
return filterField(subp, [...arrOfFileType, ...objOfFileType, 'dependencies']);
|
|
121
113
|
});
|
|
122
114
|
// 如果plugins重复,则错误提示
|
|
123
115
|
const pluginsErrMsg = Object.keys(pluginsMap).map((pk) => {
|
|
@@ -128,9 +120,8 @@ const fixAppJson = (appJson) => {
|
|
|
128
120
|
})
|
|
129
121
|
.reduce((pre, cur) => pre + cur, '');
|
|
130
122
|
if (pluginsErrMsg) {
|
|
131
|
-
|
|
123
|
+
throw new Error(`plugins配置出现错误:${pluginsErrMsg}`);
|
|
132
124
|
}
|
|
133
|
-
// eslint-disable-next-line
|
|
134
125
|
appJson.subpackages = subps;
|
|
135
126
|
};
|
|
136
127
|
|
|
@@ -143,21 +134,26 @@ const fixAppJson = (appJson) => {
|
|
|
143
134
|
function buildOutputAppJson(tmsConfig, modules) {
|
|
144
135
|
try {
|
|
145
136
|
// 获取当前 modules 下的所有子模块的配置内容
|
|
146
|
-
const
|
|
137
|
+
const modulesConfigs = getModuleConfig(modules, tmsConfig.appName, MODULE_CONFIG_FILENAME);
|
|
147
138
|
// 获取app.json的配置
|
|
148
139
|
const appJson = getAppJsonContent(resolve('./app.json'));
|
|
149
140
|
// 更新app.json中的subpackages
|
|
150
|
-
updateSubpackages(appJson,
|
|
141
|
+
updateSubpackages(appJson, modulesConfigs);
|
|
151
142
|
// 处理appJson中重复||冲突的地方
|
|
152
143
|
fixAppJson(appJson);
|
|
153
144
|
// 更新主包,需在subpackages处理完成后执行, pages/
|
|
154
145
|
updateMainPackages(appJson, tmsConfig.mainPackages);
|
|
155
146
|
|
|
156
147
|
fs.writeFileSync(resolve(`${tmsConfig.outputDir}/app.json`), JSON.stringify(appJson, null, 2), 'utf8');
|
|
148
|
+
|
|
157
149
|
if (typeof tmsConfig?.hooks?.updateAppJson === 'function') {
|
|
158
|
-
tmsConfig?.hooks?.updateAppJson(
|
|
150
|
+
tmsConfig?.hooks?.updateAppJson({
|
|
151
|
+
tmsConfig: filterField(tmsConfig, ['gitAccount']),
|
|
152
|
+
modules,
|
|
153
|
+
appJson,
|
|
154
|
+
isDev: global.getData('isDev'),
|
|
155
|
+
});
|
|
159
156
|
}
|
|
160
|
-
|
|
161
157
|
return appJson;
|
|
162
158
|
} catch (e) {
|
|
163
159
|
handleError(`生成app.json出现错误: ${e}`);
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const { CACHE_FILE, CACHE_DIR } = require('../config/constant');
|
|
4
|
+
const { ensureDirExist } = require('../utils/io');
|
|
5
|
+
|
|
6
|
+
function getCache(projectDir, type) {
|
|
7
|
+
const filePath = `${CACHE_DIR}/${CACHE_FILE}`;
|
|
8
|
+
if (!fs.existsSync(filePath)) {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
const content = require(filePath);
|
|
12
|
+
return content?.[projectDir]?.[type];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
function setCache(projectDir, type = 'miniprogram_npm', data) {
|
|
17
|
+
const filePath = `${CACHE_DIR}/${CACHE_FILE}`;
|
|
18
|
+
if (!fs.existsSync(filePath)) {
|
|
19
|
+
const dir = path.dirname(filePath);
|
|
20
|
+
ensureDirExist(dir);
|
|
21
|
+
fs.writeFileSync(filePath, '{}');
|
|
22
|
+
}
|
|
23
|
+
const content = require(filePath);
|
|
24
|
+
if (!content[projectDir]) {
|
|
25
|
+
content[projectDir] = {};
|
|
26
|
+
}
|
|
27
|
+
content[projectDir] = {
|
|
28
|
+
[type]: data,
|
|
29
|
+
};
|
|
30
|
+
fs.writeFileSync(filePath, JSON.stringify(content, null, 2));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
module.exports = {
|
|
34
|
+
setCache,
|
|
35
|
+
getCache,
|
|
36
|
+
};
|
|
@@ -17,7 +17,7 @@ const getLatestVersion = (npmName) => {
|
|
|
17
17
|
* @param {*} outputDir 待检查node_modules存放的目录 (eg: dist/node_modules)
|
|
18
18
|
* @returns
|
|
19
19
|
*/
|
|
20
|
-
const
|
|
20
|
+
const isDependenciesUpdate = (modules, cwd, outputDir) => {
|
|
21
21
|
// 步骤1. 收集package.json
|
|
22
22
|
const packageJsonName = 'package.json'; // 查找文件名
|
|
23
23
|
// 1.1根目录的package.json
|
|
@@ -73,5 +73,5 @@ const checkDependencies = (modules, cwd, outputDir) => {
|
|
|
73
73
|
};
|
|
74
74
|
|
|
75
75
|
module.exports = {
|
|
76
|
-
|
|
76
|
+
isDependenciesUpdate,
|
|
77
77
|
};
|
package/src/core/cloneModules.js
CHANGED
|
@@ -18,11 +18,11 @@ const { global } = require('../utils/global');
|
|
|
18
18
|
*/
|
|
19
19
|
function replaceGitUrlAccount(httpRepoUrl, moduleName) {
|
|
20
20
|
// 用户本地的私有项目配置(用来配置环境\模块信息\账号信息)
|
|
21
|
-
const
|
|
21
|
+
const tmsConfig = global.getData('tmsConfig');
|
|
22
22
|
|
|
23
23
|
let gitUrl = httpRepoUrl;
|
|
24
|
-
const { username = '', pass = '' } =
|
|
25
|
-
||
|
|
24
|
+
const { username = '', pass = '' } = tmsConfig?.gitAccout?.[moduleName]
|
|
25
|
+
|| tmsConfig?.gitAccount?.[moduleName]
|
|
26
26
|
|| {};
|
|
27
27
|
|
|
28
28
|
const urlPrefixReg = /http(s)?:\/\//;
|
package/src/core/npm.js
CHANGED
|
@@ -99,7 +99,7 @@ const collectNpmTasksMap = (packageJsonFiles, cacheDir) => {
|
|
|
99
99
|
|
|
100
100
|
|
|
101
101
|
// 遍历安装指定目录下所有项目的npm依赖
|
|
102
|
-
const
|
|
102
|
+
const npmInstallAll = async (modules, contextDir, cacheDir) => {
|
|
103
103
|
const cwd = process.cwd();
|
|
104
104
|
const packageJsonFiles = await findAllPackageJson(modules, contextDir);
|
|
105
105
|
|
|
@@ -124,6 +124,7 @@ const mpNpmInstallAll = async (modules, contextDir, cacheDir) => {
|
|
|
124
124
|
|
|
125
125
|
await Promise.all(arrPromises);
|
|
126
126
|
shell.cd(cwd);
|
|
127
|
+
return packageJsonFiles;
|
|
127
128
|
};
|
|
128
129
|
|
|
129
130
|
/**
|
|
@@ -215,7 +216,7 @@ function cloudNpmInstall(contextDir) {
|
|
|
215
216
|
|
|
216
217
|
module.exports = {
|
|
217
218
|
cloudNpmInstall,
|
|
218
|
-
|
|
219
|
+
npmInstallAll,
|
|
219
220
|
findAllPackageJson,
|
|
220
221
|
};
|
|
221
222
|
|
package/src/core/tmsMpconfig.js
CHANGED
|
@@ -7,7 +7,6 @@ const { TMS_CONFIG_FILENAME, MODULE_CONFIG_FILENAME, TMS_PRIVATE_FILENAME } = re
|
|
|
7
7
|
const { resolve, isObject, isArray } = require('../utils/widgets');
|
|
8
8
|
const defaultTmsConfig = require('../config/defaultTmsConfig');
|
|
9
9
|
const { fail } = require('../utils/log');
|
|
10
|
-
const path = require('path');
|
|
11
10
|
|
|
12
11
|
/**
|
|
13
12
|
* 读取tms.config.js
|
|
@@ -23,35 +22,11 @@ const readTmsConfig = function (env) {
|
|
|
23
22
|
const tmsConfig = tmsConfigFn({
|
|
24
23
|
env,
|
|
25
24
|
});
|
|
26
|
-
// 合并默认值
|
|
27
|
-
loadash.mergeWith(tmsConfig, defaultTmsConfig);
|
|
28
25
|
|
|
29
|
-
//
|
|
30
|
-
|
|
31
|
-
return tmsConfig;
|
|
26
|
+
// 合并默认值
|
|
27
|
+
return loadash.mergeWith(defaultTmsConfig, tmsConfig);
|
|
32
28
|
};
|
|
33
29
|
|
|
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
30
|
|
|
56
31
|
/**
|
|
57
32
|
* 读取tms.private.config.js
|
|
@@ -62,12 +37,7 @@ const readTmsPrivateCf = function () {
|
|
|
62
37
|
if (fs.existsSync(tmsPrivatePath)) {
|
|
63
38
|
tmsPrivateCf = require(tmsPrivatePath);
|
|
64
39
|
}
|
|
65
|
-
|
|
66
|
-
if (tmsPrivateCf.modules instanceof Array) {
|
|
67
|
-
Object.assign(tmsPrivateCf.modules, {
|
|
68
|
-
include: tmsPrivateCf.modules,
|
|
69
|
-
});
|
|
70
|
-
}
|
|
40
|
+
|
|
71
41
|
return tmsPrivateCf;
|
|
72
42
|
};
|
|
73
43
|
|
|
@@ -80,11 +50,10 @@ const readTmsPrivateCf = function () {
|
|
|
80
50
|
const checkModules = function (tmsConfig, modules, isQuit = false) {
|
|
81
51
|
const targetModules = [];
|
|
82
52
|
modules.forEach((moduleName) => {
|
|
83
|
-
const module = tmsConfig.modules.find(module => module.name === moduleName);
|
|
53
|
+
const module = tmsConfig.modules.all.find(module => module.name === moduleName);
|
|
84
54
|
module && targetModules.push(module);
|
|
85
55
|
});
|
|
86
56
|
|
|
87
|
-
|
|
88
57
|
if (targetModules.length === 0) {
|
|
89
58
|
fail(`你启动的模块无效${modules.join(',')}无效,请检查tms.config.json>modules>${modules.join(',')}
|
|
90
59
|
>name字段与module.config.json的name字段是否一致`);
|
|
@@ -93,40 +62,33 @@ const checkModules = function (tmsConfig, modules, isQuit = false) {
|
|
|
93
62
|
return targetModules;
|
|
94
63
|
};
|
|
95
64
|
|
|
96
|
-
/**
|
|
97
|
-
* 过滤页面为空的分包
|
|
98
|
-
* @param {Array} moduleCfg 模块配置内容
|
|
99
|
-
* @returns pages不为空的分包
|
|
100
|
-
*/
|
|
101
|
-
const getValidModules = (moduleCfg) => {
|
|
102
|
-
// 过滤 pages 为空的情况
|
|
103
|
-
const validModules = moduleCfg.filter(item => item.pages.length > 0);
|
|
104
|
-
return validModules;
|
|
105
|
-
};
|
|
106
|
-
|
|
107
65
|
/**
|
|
108
66
|
* 适配处理module.config.json的字段
|
|
109
67
|
* @param { object } fileContent module.config.json的内容
|
|
110
68
|
* @param { string } appName 小程序的名称
|
|
111
69
|
*/
|
|
112
70
|
function adaptMpCgContent(fileContent, appName) {
|
|
113
|
-
const
|
|
71
|
+
const handleContent = function (appName, current) {
|
|
72
|
+
let res = current;
|
|
73
|
+
if (appName && current.mpConfig && current.mpConfig[appName]) {
|
|
74
|
+
res = { ...current, ...current.mpConfig[appName] };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
delete res.mpConfig;
|
|
78
|
+
delete res.isSubpackages;
|
|
79
|
+
return res;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
let content = JSON.parse(fileContent);
|
|
114
83
|
|
|
115
84
|
if (isArray(content)) {
|
|
116
85
|
let i = content.length - 1;
|
|
117
86
|
while (i >= 0) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
if (appName && current.mpConfig && current.mpConfig[appName]) {
|
|
121
|
-
current = { ...current, ...current.mpConfig[appName] };
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
delete current.mpConfig;
|
|
125
|
-
delete current.isSubpackages;
|
|
126
|
-
|
|
127
|
-
content[i] = current;
|
|
87
|
+
content[i] = handleContent(appName, content[i]);
|
|
128
88
|
i--; // eslint-disable-line
|
|
129
89
|
}
|
|
90
|
+
} else {
|
|
91
|
+
content = handleContent(appName, content);
|
|
130
92
|
}
|
|
131
93
|
return content;
|
|
132
94
|
}
|
|
@@ -168,7 +130,7 @@ const tmsModulesMergeLocalModuleCfg = (modules, appName) => {
|
|
|
168
130
|
let moduleConfigContent = fs.readFileSync(moduleConfigPath, 'utf-8');
|
|
169
131
|
moduleConfigContent = adaptMpCgContent(moduleConfigContent, appName);
|
|
170
132
|
const moduleContentArr = isObject(moduleConfigContent) ? [moduleConfigContent] : moduleConfigContent;
|
|
171
|
-
|
|
133
|
+
moduleContentArr.forEach(({ name }, moduleContentArrIndex) => {
|
|
172
134
|
if (name === moduleName) {
|
|
173
135
|
findModule = true;
|
|
174
136
|
newModules.push({
|
|
@@ -178,7 +140,7 @@ const tmsModulesMergeLocalModuleCfg = (modules, appName) => {
|
|
|
178
140
|
}
|
|
179
141
|
});
|
|
180
142
|
if (!findModule) {
|
|
181
|
-
fail(`启动模块${moduleName}在${moduleConfigPath}
|
|
143
|
+
fail(`启动模块${moduleName}在${moduleConfigPath}没有找到,请检查配置`);
|
|
182
144
|
process.exit(1);
|
|
183
145
|
}
|
|
184
146
|
} catch (e) {
|
|
@@ -232,7 +194,6 @@ module.exports = {
|
|
|
232
194
|
readTmsConfig,
|
|
233
195
|
readTmsPrivateCf,
|
|
234
196
|
getModuleConfig,
|
|
235
|
-
getValidModules,
|
|
236
197
|
checkModules,
|
|
237
198
|
tmsModulesMergeLocalModuleCfg,
|
|
238
199
|
subModulesMergeDepModules,
|
package/src/entry.js
CHANGED
|
@@ -34,6 +34,17 @@ module.exports = [
|
|
|
34
34
|
require('./scripts/run/index')('dev', cmd);
|
|
35
35
|
},
|
|
36
36
|
},
|
|
37
|
+
{
|
|
38
|
+
command: 'cloud',
|
|
39
|
+
description: '云函数开发',
|
|
40
|
+
options: [
|
|
41
|
+
['-m, --module [moduleName]', '模块名称'],
|
|
42
|
+
['-e, --env [env]', '环境变量'],
|
|
43
|
+
],
|
|
44
|
+
action: (cmd) => {
|
|
45
|
+
require('./scripts/run/index')('cloud', cmd);
|
|
46
|
+
},
|
|
47
|
+
},
|
|
37
48
|
{
|
|
38
49
|
command: 'build',
|
|
39
50
|
description: 'prod 打包编译',
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const shelljs = require('shelljs');
|
|
2
|
-
const { resolve } = require('../../../utils/widgets');
|
|
2
|
+
const { resolve, filterField } = require('../../../utils/widgets');
|
|
3
3
|
const init = require('../init/index');
|
|
4
4
|
const compileBuild = require('../../../compile/build');
|
|
5
5
|
|
|
@@ -9,6 +9,12 @@ async function build(tmsConfig, targetModules, env) {
|
|
|
9
9
|
|
|
10
10
|
const { targetModules: newModules } = await init(tmsConfig, targetModules);
|
|
11
11
|
|
|
12
|
+
if (typeof tmsConfig?.hooks?.beforeCompile === 'function') {
|
|
13
|
+
await tmsConfig?.hooks?.beforeCompile({
|
|
14
|
+
isDev: false,
|
|
15
|
+
tmsConfig: filterField(tmsConfig, ['gitAccount']),
|
|
16
|
+
modules: newModules });
|
|
17
|
+
};
|
|
12
18
|
compileBuild(tmsConfig, newModules, env);
|
|
13
19
|
}
|
|
14
20
|
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const { symLink } = require('../../../core/symbolicLink');
|
|
2
|
+
const { handleError } = require('../../../core/handleError');
|
|
3
|
+
const { succeed } = require('../../../utils/log');
|
|
4
|
+
|
|
5
|
+
module.exports = async (tmsConfig) => {
|
|
6
|
+
try {
|
|
7
|
+
await symLink(tmsConfig);
|
|
8
|
+
succeed('云函数创建软链成功');
|
|
9
|
+
} catch (e) {
|
|
10
|
+
handleError(`创建软链错误: ${e}`);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const shelljs = require('shelljs');
|
|
2
2
|
const compileDev = require('../../../compile/dev');
|
|
3
|
-
const { resolve } = require('../../../utils/widgets');
|
|
3
|
+
const { resolve, filterField } = require('../../../utils/widgets');
|
|
4
4
|
const init = require('../init/index');
|
|
5
5
|
const { tmsModulesMergeLocalModuleCfg } = require('../../../core/tmsMpconfig');
|
|
6
6
|
const { info } = require('../../../utils/log');
|
|
@@ -10,11 +10,11 @@ const { CACHE_DIR } = require('../../../config/constant');
|
|
|
10
10
|
|
|
11
11
|
// 用户编译分包时,需要将dist中其他分包(主包不能删除)的内容删除,否则其他分包的内容混入到主包(导致主包的体积超2M)
|
|
12
12
|
function delOtherModule(tmsConfig, targetModules) {
|
|
13
|
-
const modules = tmsModulesMergeLocalModuleCfg(tmsConfig.modules, tmsConfig.appName);
|
|
13
|
+
const modules = tmsModulesMergeLocalModuleCfg(tmsConfig.modules.all, tmsConfig.appName);
|
|
14
14
|
const targetModulesName = targetModules.map(item => item.name);
|
|
15
15
|
modules.forEach((item) => {
|
|
16
16
|
if (item.root && targetModulesName.indexOf(item.name) === -1) {
|
|
17
|
-
const moduleRootDir = resolve(
|
|
17
|
+
const moduleRootDir = resolve(`${tmsConfig.outputDir}/${item.root}`);
|
|
18
18
|
shelljs.rm('-rf', `${moduleRootDir}/*`, { silent: true });
|
|
19
19
|
// 解决微信开发者工具(dist/app.json: ["subpackages"][0]["root"] 字段需为 目录)错误 - 提前创建该目录
|
|
20
20
|
// io.ensureDirExist(moduleRootDir);
|
|
@@ -36,7 +36,11 @@ async function dev(tmsConfig, targetModules, env) {
|
|
|
36
36
|
|
|
37
37
|
info('当前dev启动的有效模块', newModules.map(item => item.name).sort());
|
|
38
38
|
if (typeof tmsConfig?.hooks?.beforeCompile === 'function') {
|
|
39
|
-
await tmsConfig?.hooks?.beforeCompile({
|
|
39
|
+
await tmsConfig?.hooks?.beforeCompile({
|
|
40
|
+
isDev: true,
|
|
41
|
+
tmsConfig: filterField(tmsConfig, ['gitAccount']),
|
|
42
|
+
modules: newModules,
|
|
43
|
+
});
|
|
40
44
|
};
|
|
41
45
|
delOtherModule(tmsConfig, newModules);
|
|
42
46
|
compileDev(tmsConfig, newModules, env);
|