crabatool 1.0.394 → 1.0.395

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/lib/config.js CHANGED
@@ -21,6 +21,7 @@ class Config {
21
21
  this.Version = pg.version;
22
22
  this.Debug = false;
23
23
  this.version = 'master';
24
+ this.mergeInitAndBiz = false; // 合并最终生成的init.js和biz.js
24
25
 
25
26
  this.TempPath = path.join(os.tmpdir(), '_crabatemp');
26
27
  this.ImageCache = path.join(this.TempPath, "_img");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "crabatool",
3
- "version": "1.0.394",
3
+ "version": "1.0.395",
4
4
  "description": "crabatool",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -135,6 +135,112 @@ function mergeInit() {
135
135
  watchAction();// 启动就打包一次
136
136
  }
137
137
 
138
+ //合并init.js和biz.js
139
+ function mergeInitAndBiz() {
140
+ // 检查配置是否启用合并功能
141
+ if (!config.mergeInitAndBiz) {
142
+ utils.debug('mergeInitAndBiz 配置为 false,跳过合并');
143
+ return;
144
+ }
145
+
146
+ utils.log('开始合并 init.js 和 biz.js 文件');
147
+
148
+ // 收集所有需要合并的文件
149
+ var filesToMerge = [];
150
+
151
+ // 1. 添加主模块的 modName.js 文件
152
+ // var modJs = utils.join(config.webPath, '/js/' + config.modName + '.js');
153
+ // if (fs.existsSync(modJs)) {
154
+ // filesToMerge.push(modJs);
155
+ // utils.debug('添加主模块文件: ' + modJs);
156
+ // }
157
+
158
+ // 2. 添加所有子模块的 init.js 文件
159
+ config.childModList.forEach(childName => {
160
+ var childInitPath = utils.join(config.webPath, childName, 'js/init.js');
161
+ if (fs.existsSync(childInitPath)) {
162
+ filesToMerge.push(childInitPath);
163
+ utils.debug('添加子模块 init.js: ' + childInitPath);
164
+ } else {
165
+ utils.error('子模块 init.js 不存在: ' + childInitPath);
166
+ }
167
+ });
168
+
169
+ // 3. 添加所有子模块的 biz.js 文件
170
+ config.childModList.forEach(childName => {
171
+ var childBizPath = utils.join(config.webPath, childName, 'js/biz.js');
172
+ if (fs.existsSync(childBizPath)) {
173
+ filesToMerge.push(childBizPath);
174
+ utils.debug('添加子模块 biz.js: ' + childBizPath);
175
+ } else {
176
+ utils.debug('子模块 biz.js 不存在,跳过: ' + childBizPath);
177
+ }
178
+ });
179
+
180
+ // 4. 添加主模块的 biz.js 文件
181
+ var mainBizPath = utils.join(config.webPath, 'js/biz.js');
182
+ if (fs.existsSync(mainBizPath)) {
183
+ filesToMerge.push(mainBizPath);
184
+ utils.debug('添加主模块 biz.js: ' + mainBizPath);
185
+ }
186
+
187
+ // 检查是否有文件需要合并
188
+ if (filesToMerge.length === 0) {
189
+ utils.error('没有找到需要合并的文件');
190
+ return;
191
+ }
192
+
193
+ // 定义输出文件路径
194
+ var initJs = utils.join(config.webPath, '/js/init.js');
195
+ var initJsSource = utils.join(config.webPath, '/js/init_source.js');
196
+
197
+ var watchAction = function() {
198
+ utils.debug('正在合并文件...');
199
+ utils.debug('合并文件列表: ', filesToMerge);
200
+
201
+ // 生成压缩版本
202
+ mergeTool.run({
203
+ inPath: filesToMerge,
204
+ outPath: initJs,
205
+ ext: '.js',
206
+ ignoreCompress: config.ignoreCompress
207
+ });
208
+
209
+ // 生成源码版本
210
+ mergeTool.run({
211
+ inPath: filesToMerge,
212
+ outPath: initJsSource,
213
+ ext: '.js',
214
+ ignoreCompress: true // 源码版本不压缩
215
+ });
216
+
217
+ utils.log('合并完成: ' + initJs);
218
+ utils.log('源码版本: ' + initJsSource);
219
+ }
220
+
221
+ // 设置文件监听(如果不是仅打包模式)
222
+ if (!config.mergeinitjs) {
223
+ var watcher = chokidar.watch(filesToMerge, {
224
+ persistent: true,
225
+ usePolling: true
226
+ });
227
+
228
+ watcher
229
+ .on('ready', () => utils.debug('文件监听就绪,监听文件: ' + filesToMerge.join(', ')))
230
+ .on('change', path => {
231
+ utils.debug('文件变化: ' + path);
232
+ watchAction({ event: 'change', eventPath: path });
233
+ })
234
+ .on('unlink', path => {
235
+ utils.debug('文件删除: ' + path);
236
+ watchAction({ event: 'remove', eventPath: path });
237
+ });
238
+ }
239
+
240
+ // 立即执行一次合并
241
+ watchAction();
242
+ }
243
+
138
244
  utils.log('启动打包服务', config.childModList);
139
245
  utils.mkdirsSync(config.webPath);
140
246
 
@@ -149,3 +255,8 @@ mergeBiz('/js/biz');
149
255
 
150
256
  // 打包所有模块的init.js
151
257
  mergeInit(); // 如果有需要
258
+
259
+ // 如果配置了合并功能,执行合并
260
+ if (config.mergeInitAndBiz) {
261
+ mergeInitAndBiz();
262
+ }