@tmsfe/tmskit 0.0.41 → 0.0.43

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmsfe/tmskit",
3
- "version": "0.0.41",
3
+ "version": "0.0.43",
4
4
  "description": "tmskit",
5
5
  "main": "dist/index.cjs",
6
6
  "bin": {
@@ -59,7 +59,7 @@
59
59
  "lodash": "^4.17.21",
60
60
  "metalsmith": "^2.3.0",
61
61
  "minimatch": "^5.1.0",
62
- "miniprogram-ci": "1.8.25",
62
+ "miniprogram-ci": "2.1.14",
63
63
  "moment": "^2.29.2",
64
64
  "object-assign": "^4.0.1",
65
65
  "ora": "^5.4.1",
@@ -9,6 +9,8 @@ module.exports = {
9
9
  dependencies: {},
10
10
  /** 编译输出文件夹位置 */
11
11
  outputDir: 'dist',
12
+ /** 是否在编译前清空输出目录 */
13
+ cleanOutputDirBeforeCompile: true,
12
14
  /** 源码监听路径 */
13
15
  sourceDir: './',
14
16
  /** 静态资源目录 */
@@ -71,27 +71,39 @@ const getAppJsonContent = (sourceAppJsonPath) => {
71
71
  */
72
72
  const fixAppJson = (appJson) => {
73
73
  const { subpackages } = appJson;
74
+ // 创建插件映射,用于检测插件配置冲突
74
75
  const pluginsMap = {};
76
+ // 记录app.json顶层的插件配置
75
77
  Object.keys(appJson.plugins || {}).forEach(key => pluginsMap[key] = ['app.json']);
78
+
79
+ // 处理每个分包的配置
76
80
  const subps = subpackages.map((subp) => {
81
+ // 需要从分包提取到app.json顶层的数组类型字段
77
82
  const arrOfFileType = ['requiredBackgroundModes', 'embeddedAppIdList'];
83
+ // 需要从分包提取到app.json顶层的对象类型字段
78
84
  const objOfFileType = ['preloadRule'];
85
+
86
+ // 遍历分包中的每个配置项
79
87
  Object.keys(subp).forEach((key) => {
88
+ // 处理插件配置,检查是否有重复配置的插件
80
89
  if (key === 'plugins') {
81
90
  Object.keys(subp.plugins).forEach((pk) => {
91
+ // 记录每个插件被哪些地方配置
82
92
  pluginsMap[pk]
83
93
  ? pluginsMap[pk].push(`分包${subp.name}`)
84
94
  : pluginsMap[pk] = [`分包${subp.name}`];
85
95
  });
86
96
  }
87
- // 分包里数组类型字段,提到appjson最上层
97
+
98
+ // 处理数组类型字段(如requiredBackgroundModes),合并到app.json顶层并去重
88
99
  if (arrOfFileType.indexOf(key) > -1) {
89
100
  const preVal = appJson[key];
90
101
  preVal
91
102
  ? appJson[key] = Array.from(new Set(preVal.slice(0).concat(subp[key])))
92
103
  : appJson[key] = subp[key].slice(0);
93
104
  }
94
- // 分包里对象类型字段,提到appjson最上层
105
+
106
+ // 处理对象类型字段(如preloadRule),合并到app.json顶层,已存在的键值不覆盖
95
107
  if (objOfFileType.indexOf(key) > -1) {
96
108
  const preloadRuleMap = appJson[key] || {};
97
109
  subp[key] && Object.keys(subp[key]).forEach((page) => {
@@ -102,9 +114,12 @@ const fixAppJson = (appJson) => {
102
114
  appJson[key] = preloadRuleMap;
103
115
  }
104
116
  });
117
+
118
+ // 从分包配置中移除已提取到顶层的字段以及一些构建相关的内部字段
105
119
  return filterField(subp, [...arrOfFileType, ...objOfFileType, 'dependencies', 'path']);
106
120
  });
107
- // 如果plugins重复,则错误提示
121
+
122
+ // 检查插件配置冲突,如果同一个插件在多处配置则报错
108
123
  const pluginsErrMsg = Object.keys(pluginsMap).map((pk) => {
109
124
  if (pluginsMap[pk].length > 1) {
110
125
  return `${pluginsMap[pk].join(',')}重复配置plugin(${pk});`;
@@ -115,9 +130,45 @@ const fixAppJson = (appJson) => {
115
130
  if (pluginsErrMsg) {
116
131
  throw new Error(`plugins配置出现错误:${pluginsErrMsg}`);
117
132
  }
133
+
134
+ // 用处理后的分包配置更新app.json的subpackages字段
118
135
  appJson.subpackages = subps;
119
136
  };
120
137
 
138
+ /**
139
+ * 合并分包配置,保留已有分包,添加新分包
140
+ * @param {Array} existingPackages 现有的分包配置
141
+ * @param {Array} newPackages 新的分包配置
142
+ * @returns {Array} 合并后的分包配置
143
+ */
144
+ function mergeSubPackages(existingPackages, newPackages) {
145
+ // 创建结果数组和root到索引的映射
146
+ const resultPackages = [...existingPackages];
147
+ const rootMap = {};
148
+
149
+ // 建立现有分包的root映射
150
+ existingPackages.forEach((pkg, index) => {
151
+ if (pkg.root) {
152
+ rootMap[pkg.root] = index;
153
+ }
154
+ });
155
+
156
+ // 合并新分包
157
+ newPackages.forEach((newPkg) => {
158
+ if (!newPkg.root) return; // 忽略没有root的分包
159
+
160
+ if (rootMap[newPkg.root] !== undefined) {
161
+ // 如果已存在相同root的分包,则不覆盖保留原有配置
162
+ info(`保留已有分包配置, root: ${newPkg.root}, name: ${newPkg.name || '未命名'}`);
163
+ } else {
164
+ // 如果不存在,则添加新分包
165
+ resultPackages.push(newPkg);
166
+ }
167
+ });
168
+
169
+ return resultPackages;
170
+ }
171
+
121
172
  /**
122
173
  * 动态生成编译后的app.json
123
174
  * @param {object} tmsConfig
@@ -129,13 +180,14 @@ async function buildOutputAppJson(tmsConfig, modules) {
129
180
  // 获取所有模块,合并模块依赖的模块
130
181
  const modulesConfig = getModulesConfig(modules, tmsConfig, false);
131
182
  // 获取所有的分包
132
- const subPackages = getSubPackages(modulesConfig);
183
+ const newSubPackages = getSubPackages(modulesConfig);
133
184
  // 获取app.json的配置
134
185
  let appJson = getAppJsonContent(resolve('./app.json'));
135
186
 
136
- // 更新app.json中的subpackages
137
- appJson.subpackages = subPackages;
138
- appJson.subpackages.sort((item1, item2) => item1.name.localeCompare(item2.name));
187
+ // 保留已有分包配置,合并新的分包配置
188
+ const existingSubpackages = appJson.subpackages || [];
189
+ appJson.subpackages = mergeSubPackages(existingSubpackages, newSubPackages);
190
+ appJson.subpackages.sort((item1, item2) => item1.root.localeCompare(item2.root));
139
191
 
140
192
  // 处理appJson中重复||冲突的地方
141
193
  fixAppJson(appJson);
package/src/core/mpCi.js CHANGED
@@ -114,8 +114,22 @@ const uploadMp = async (params = {}) => {
114
114
  });
115
115
  };
116
116
 
117
+ const getDevSourceMap = async (params = {}) => {
118
+ const { appId, projectPath, privateKey, robot, sourceMapSavePath } = params;
119
+ const project = await getMpCi({
120
+ appId,
121
+ projectPath,
122
+ privateKey,
123
+ });
124
+ return await ci.getDevSourceMap({
125
+ project,
126
+ robot,
127
+ sourceMapSavePath,
128
+ });
129
+ };
117
130
  module.exports = {
118
131
  buildMpNpm,
119
132
  previewMp,
120
133
  uploadMp,
134
+ getDevSourceMap,
121
135
  };
package/src/entry.js CHANGED
@@ -81,6 +81,15 @@ module.exports = [
81
81
  require('./scripts/run/index')('upload', cmdOptions);
82
82
  },
83
83
  },
84
+ {
85
+ command: 'sourcemap',
86
+ description: '获取sourcemap',
87
+ options: [
88
+ ],
89
+ action: (cmdOptions) => {
90
+ require('./scripts/run/index')('sourcemap', cmdOptions);
91
+ },
92
+ },
84
93
  {
85
94
  command: 'cloud-dev',
86
95
  description: '云函数开发',
@@ -8,7 +8,9 @@ const { global } = require('../../../utils/global');
8
8
 
9
9
  async function build(tmsConfig, targetModules) {
10
10
  // 开始构建前,清理输出目录
11
- await shelljs.rm('-rf', resolve(tmsConfig.outputDir));
11
+ if (tmsConfig.cleanOutputDirBeforeCompile !== false) {
12
+ await shelljs.rm('-rf', resolve(tmsConfig.outputDir));
13
+ }
12
14
 
13
15
  const { modules: newModules } = await init(tmsConfig, targetModules);
14
16
  info('当前build有效模块', newModules.map(item => item.moduleName).sort());
@@ -31,7 +31,9 @@ function delOtherPackages(tmsConfig, targetSubPackages) {
31
31
  async function dev(tmsConfig, targetModules) {
32
32
  const { noCache } = global.getData('cmdOptions');
33
33
  if (noCache) {
34
- shelljs.rm('-rf', resolve(tmsConfig.outputDir));
34
+ if (tmsConfig.cleanOutputDirBeforeCompile !== false) {
35
+ shelljs.rm('-rf', resolve(tmsConfig.outputDir));
36
+ }
35
37
  shelljs.rm('-rf', MODULE_CODE_DIR);
36
38
  shelljs.rm('-rf', NODE_MODULES_DIR);
37
39
  }
@@ -5,6 +5,7 @@ const build = require('./build/index');
5
5
  const install = require('./install/index');
6
6
  const preview = require('./preview/index');
7
7
  const upload = require('./upload/index');
8
+ const sourcemap = require('./sourcemap/index');
8
9
  const cloudLink = require('./cloud/link');
9
10
  const cloudDev = require('./cloud/dev');
10
11
  const { fail, info } = require('../../utils/log');
@@ -121,6 +122,10 @@ function otherCommands(tmsConfig, commandName, cmdOptions) {
121
122
  upload(tmsConfig, cmdOptions);
122
123
  report('run:upload', { appName: tmsConfig.appName });
123
124
  return;
125
+ case 'sourcemap':
126
+ sourcemap(tmsConfig);
127
+ report('run:sourcemap', { appName: tmsConfig.appName });
128
+ return;
124
129
  default:
125
130
  return;
126
131
  }
@@ -0,0 +1,48 @@
1
+ const mpCi = require('../../../core/mpCi');
2
+ const { resolve, createTask } = require('../../../utils/widgets');
3
+ const { handleError } = require('../../../core/handleError');
4
+ const { info } = require('../../../utils/log');
5
+ const { getDesc } = require('../preview/utils');
6
+ const report = require('../../../core/report');
7
+
8
+ const handleParams = (tmsConfig, cmdOptions) => {
9
+ const params = {
10
+ ...(tmsConfig.upload ? tmsConfig.upload : {}),
11
+ ...cmdOptions,
12
+ };
13
+
14
+ return {
15
+ sourceMapSavePath: './sourcemap.zip',
16
+ ...params,
17
+ appId: params.appId || tmsConfig.appId,
18
+ projectPath: resolve(tmsConfig.outputDir),
19
+ privateKey: params.privateKey || tmsConfig.privateKey,
20
+ robot: params.robot || 30,
21
+ desc: params.desc || getDesc(params.desc),
22
+ };
23
+ };
24
+
25
+ /**
26
+ * 获取sourcemap
27
+ * @param {object} tmsConfig
28
+ * @param {object} cmdOptions {version: '2022.28.5', desc: '', robot: 2, infoOutput: './a.txt' }
29
+ */
30
+ async function sourcemap(tmsConfig, cmdOptions) {
31
+ try {
32
+ const params = handleParams(tmsConfig, cmdOptions);
33
+ report('sourcemap');
34
+
35
+ await createTask(
36
+ mpCi.getDevSourceMap,
37
+ '正在获取小程序代码sourcemap',
38
+ '获取小程序代码sourcemap完成',
39
+ )({
40
+ ...params,
41
+ });
42
+ info(`sourcemap文件已保存到${params.sourceMapSavePath}`);
43
+ } catch (e) {
44
+ console.log('详细错误:', e);
45
+ handleError(`获取sourcemap错误: ${e.message}`, true);
46
+ }
47
+ }
48
+ module.exports = sourcemap;