xhs-mp-compiler-cli 2.0.1 → 2.0.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.
Files changed (3) hide show
  1. package/dist/index.d.ts +47 -38
  2. package/dist/index.js +260 -167
  3. package/package.json +10 -10
package/dist/index.d.ts CHANGED
@@ -2,46 +2,54 @@ import { COMPILE_ENTRY } from './config/constant.config';
2
2
  import { Project } from 'xhs-mp-project';
3
3
  import EventEmitter from 'events';
4
4
  import { Logger } from 'xhs-mp-shared';
5
- import { getProjectCompilerClass, prepareDevPool } from './compiler';
5
+ import { prepareDevPool } from './compiler';
6
+ export { COMPILE_ENTRY, prepareDevPool };
6
7
  interface ILibFeatures {
7
8
  supportV2?: boolean;
8
- supportVdom?: boolean;
9
+ supportVDom?: boolean;
9
10
  }
10
- export { COMPILE_ENTRY, getProjectCompilerClass, prepareDevPool };
11
+ export type IArchType = 'v0' | 'v1' | 'v2';
12
+ export type IZipResult = {
13
+ type: IArchType;
14
+ zipPath: string;
15
+ zipSize: number;
16
+ root: string;
17
+ originSize: number;
18
+ };
11
19
  export type ICompileOpts = {
12
20
  logger?: any;
13
- compileDir?: string;
21
+ report?: (key: string, data?: any) => void;
22
+ compileDistDir?: string;
23
+ compileCacheDir?: string;
14
24
  nodeJsPath?: string;
15
- mpDevJsonPath?: string;
16
- libFeatures?: ILibFeatures;
25
+ getLibFeatures?: () => Promise<ILibFeatures>;
17
26
  };
18
- export type ICompilerProps = ({
19
- projectPath: string;
20
- } | {
21
- project: Project;
22
- }) & ICompileOpts;
27
+ export type ICompilerProps = {
28
+ projectPath?: string;
29
+ project?: Project;
30
+ } & ICompileOpts;
23
31
  interface ICompileBaseConfig {
24
32
  compressCss?: boolean;
25
33
  compressJs?: boolean;
26
34
  enableSourcemap?: boolean;
27
35
  appendSourcemapComment?: boolean;
28
36
  runInServiceSandbox?: boolean;
29
- enableV2: boolean;
30
- enableV1: boolean;
31
- enableVDom: boolean;
37
+ enableV2?: boolean;
38
+ enableV1?: boolean;
39
+ enableVDom?: boolean;
32
40
  }
33
41
  type IMakePkgsReadyConfig = ICompileBaseConfig & {
34
- compilePkgs: any;
42
+ compilePkgs?: string[];
35
43
  };
36
44
  type ICompilePkgsConfig = ICompileBaseConfig & {
37
- compilePkgs: any;
45
+ compilePkgs?: string[];
38
46
  };
39
- type ICompileBuildConfig = ICompileBaseConfig & {
40
- entryType: string;
47
+ type ICompileProjectConfig = ICompileBaseConfig & {
48
+ entryType?: string;
41
49
  mpUploadOptions?: ICompileAndZipOptions | false;
42
50
  };
43
51
  export interface ICompileAndZipOptions {
44
- entryType: string;
52
+ entryType?: string;
45
53
  can_upload_ext_json?: boolean;
46
54
  upload_app_id?: string;
47
55
  pkgInfo?: {
@@ -52,35 +60,36 @@ export interface ISubPackages {
52
60
  root: string;
53
61
  pages: string;
54
62
  }
55
- export declare class ProjectCompiler extends EventEmitter {
56
- projectPath: string;
63
+ export declare class ProjectCompilerManager extends EventEmitter {
57
64
  compilerProps: ICompilerProps;
58
65
  project: Project;
59
66
  compilerImplMap: {};
60
67
  logger: Logger;
68
+ report?: (key: string, data?: any) => void;
61
69
  constructor(props: ICompilerProps);
62
- createProject(props: any): any;
63
- private getCompilerDir;
64
- private initSimulatorCompiler;
65
- private initBuildCompiler;
66
- getUsingPackageType(entryType?: string): Promise<{
67
- enableV1: boolean;
68
- enableV2: boolean;
69
- enableVDom: boolean;
70
- }>;
70
+ bindProject(props: any): void;
71
71
  private getCompilerType;
72
72
  getDistDir(entryType?: string): string;
73
73
  removeDistDir(entryType?: string): void;
74
74
  getCacheDir(entryType?: string): string;
75
75
  removeCacheDir(entryType?: string): void;
76
- private createCompilerImpl;
77
- private getComilerImpl;
78
- makePkgsReady(config: IMakePkgsReadyConfig): Promise<void>;
79
- reCompilePkgs(config: ICompilePkgsConfig): Promise<void>;
80
- compileProject(config: ICompileBuildConfig): Promise<void>;
81
- compileAndZip(opts: ICompileAndZipOptions): Promise<any>;
76
+ removeAllCacheDir(): void;
77
+ private createCompiler;
78
+ private getCompiler;
79
+ getUsingPackageType(entryType?: string): Promise<{
80
+ enableV1: boolean;
81
+ enableV2: boolean;
82
+ enableVDom: boolean;
83
+ }>;
84
+ private initSimulatorCompiler;
85
+ private initBuildCompiler;
86
+ makePkgsReady(opts: IMakePkgsReadyConfig): Promise<void>;
87
+ reCompilePkgs(opts: ICompilePkgsConfig): Promise<void>;
88
+ compileProject(config: ICompileProjectConfig): Promise<void>;
89
+ compileAndZip(opts: ICompileAndZipOptions): Promise<IZipResult[]>;
90
+ closeCompiler(entryType?: string): void;
91
+ killCompiler(entryType?: string): void;
92
+ killAll(): void;
82
93
  private zipWithFullPackage;
83
94
  private zipWithSubPackage;
84
- close(entryType?: string): void;
85
- kill(entryType?: string): void;
86
95
  }
package/dist/index.js CHANGED
@@ -16,12 +16,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
16
16
  if (v !== undefined) module.exports = v;
17
17
  }
18
18
  else if (typeof define === "function" && define.amd) {
19
- define(["require", "exports", "./config/constant.config", "xhs-mp-project", "events", "fs-extra", "path", "./utils/utils", "./utils/project", "./config/dir.config", "./compiler", "./config/constant.config"], factory);
19
+ define(["require", "exports", "./config/constant.config", "xhs-mp-project", "events", "fs-extra", "path", "./utils/utils", "./utils/project", "./config/dir.config", "./compiler", "./sharedFs"], factory);
20
20
  }
21
21
  })(function (require, exports) {
22
22
  "use strict";
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
- exports.ProjectCompiler = exports.prepareDevPool = exports.getProjectCompilerClass = exports.COMPILE_ENTRY = void 0;
24
+ exports.ProjectCompilerManager = exports.prepareDevPool = exports.COMPILE_ENTRY = void 0;
25
25
  const constant_config_1 = require("./config/constant.config");
26
26
  Object.defineProperty(exports, "COMPILE_ENTRY", { enumerable: true, get: function () { return constant_config_1.COMPILE_ENTRY; } });
27
27
  const xhs_mp_project_1 = require("xhs-mp-project");
@@ -32,138 +32,91 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
32
32
  const project_1 = require("./utils/project");
33
33
  const dir_config_1 = require("./config/dir.config");
34
34
  const compiler_1 = require("./compiler");
35
- Object.defineProperty(exports, "getProjectCompilerClass", { enumerable: true, get: function () { return compiler_1.getProjectCompilerClass; } });
36
35
  Object.defineProperty(exports, "prepareDevPool", { enumerable: true, get: function () { return compiler_1.prepareDevPool; } });
37
- const constant_config_2 = require("./config/constant.config");
38
- class ProjectCompiler extends events_1.default {
36
+ const sharedFs_1 = __importDefault(require("./sharedFs"));
37
+ class ProjectCompilerManager extends events_1.default {
39
38
  constructor(props) {
40
39
  super();
41
40
  this.compilerImplMap = {};
41
+ this.getCompiler = (entryType = constant_config_1.COMPILE_ENTRY.simulator) => {
42
+ if (!this.compilerImplMap[entryType]) {
43
+ this.compilerImplMap[entryType] = this.createCompiler(entryType);
44
+ if (entryType === constant_config_1.COMPILE_ENTRY.simulator) {
45
+ this.initSimulatorCompiler(this.compilerImplMap[entryType]);
46
+ }
47
+ else {
48
+ this.initBuildCompiler(this.compilerImplMap[entryType]);
49
+ }
50
+ }
51
+ return this.compilerImplMap[entryType];
52
+ };
42
53
  this.initSimulatorCompiler = compiler => {
43
54
  compiler.on('buildStart', data => {
44
- this.emit('buildStart', data);
55
+ this.emit('simulator-compile-start', data);
45
56
  });
46
57
  compiler.on('compileInfo', data => {
47
- this.emit('compileInfo', data);
58
+ this.emit('simulator-compile-info', data);
48
59
  });
49
60
  compiler.on('buildSuccess', data => {
50
- this.emit('buildSuccess', data);
61
+ this.emit('simulator-compile-success', data);
51
62
  });
52
63
  compiler.on('buildError', data => {
53
- this.emit('buildError', data);
64
+ this.emit('simulator-compile-error', data);
54
65
  });
55
66
  compiler.on('compileFinishWhenFileChange', data => {
56
- this.emit('compileFinishWhenFileChange', data);
67
+ this.emit('simulator-file-change-compile-finish', data);
57
68
  });
58
69
  compiler.on('compilePercent', data => {
59
- this.emit('compilePercent', data);
70
+ this.emit('simulator-compile-percent', data);
60
71
  });
61
72
  compiler.on('emitCompileStats', data => {
62
- this.emit('emitCompileStats', data);
73
+ this.emit('simulator-compile-stats', data);
63
74
  });
64
75
  compiler.on('unexpectedExit', data => {
65
- this.emit('unexpectedExit', data);
76
+ this.emit('simulator-compile-unexpectedExit', data);
66
77
  });
67
78
  };
68
79
  this.initBuildCompiler = compiler => {
69
80
  compiler.on('compilePercent', data => {
70
- this.emit('compilePercent', data);
81
+ this.emit('full-compile-percent', data);
71
82
  });
72
83
  compiler.on('compileInfo', data => {
73
- this.emit('compileInfo', data);
84
+ this.emit('full-compiler-info', data);
74
85
  });
75
86
  compiler.on('emitCompileStats', data => {
76
- this.emit('emitCompileStats', data);
87
+ this.emit('full-compile-stats', data);
77
88
  });
78
89
  };
79
- this.getComilerImpl = (entryType = constant_config_1.COMPILE_ENTRY.simulator) => {
80
- const compilerType = this.getCompilerType(entryType);
81
- if (!this.compilerImplMap[compilerType]) {
82
- this.compilerImplMap[compilerType] = this.createCompilerImpl(entryType);
83
- if (entryType === constant_config_1.COMPILE_ENTRY.simulator) {
84
- this.initSimulatorCompiler(this.compilerImplMap[compilerType]);
85
- }
86
- else {
87
- this.initBuildCompiler(this.compilerImplMap[compilerType]);
88
- }
89
- }
90
- return this.compilerImplMap[compilerType];
91
- };
92
- const { logger } = props;
90
+ const { logger, report, projectPath, project } = props;
93
91
  this.logger = logger || console;
92
+ this.report = report;
94
93
  this.compilerProps = props;
95
- this.project = this.createProject(props);
96
- this.projectPath = this.project.projectPath;
97
- }
98
- createProject(props) {
99
- const { projectPath, project } = props;
100
- return (project ||
94
+ if (!project && !projectPath)
95
+ return;
96
+ this.project = (project ||
101
97
  new xhs_mp_project_1.Project({
102
- projectPath
98
+ projectPath: projectPath
103
99
  }));
104
100
  }
105
- getCompilerDir() {
106
- let compileDirPath;
107
- let compileCachePath;
108
- const { compileDir } = this.compilerProps;
109
- if (compileDir) {
110
- compileDirPath = path_1.default.join(compileDir, 'xhs-mp-baba');
111
- compileCachePath = path_1.default.join(compileDir, 'xhs-compile-cache');
112
- }
113
- else {
114
- compileDirPath = dir_config_1.mpCompileDirPath;
115
- compileCachePath = dir_config_1.mpCompileCachePath;
101
+ bindProject(props) {
102
+ const { projectPath, project } = props;
103
+ if (!project && !projectPath) {
104
+ throw new Error('project projectPath 均为undefined');
116
105
  }
117
- // this.logger.log('compileDirPath:', compileDirPath)
118
- return {
119
- mpCompileDirPath: compileDirPath,
120
- mpCompileCachePath: compileCachePath
121
- };
122
- }
123
- // 使用的编译目标包类型
124
- getUsingPackageType() {
125
- return __awaiter(this, arguments, void 0, function* (entryType = constant_config_1.COMPILE_ENTRY.simulator) {
126
- var _a, _b;
127
- const { libFeatures } = this.compilerProps;
128
- let enableV1 = false;
129
- let enableV2 = true;
130
- let enableVDom = false;
131
- const settings = this.project.settings || {};
132
- const isUpload = entryType === constant_config_1.COMPILE_ENTRY.upload;
133
- if (this.project.appMode === constant_config_1.MiniMode.minigame) {
134
- enableV1 = true;
135
- enableV2 = false;
136
- enableVDom = false;
137
- }
138
- else if (entryType === constant_config_1.COMPILE_ENTRY.simulator) {
139
- const supportV2 = (_a = libFeatures === null || libFeatures === void 0 ? void 0 : libFeatures.supportV2) !== null && _a !== void 0 ? _a : true;
140
- const supportVDom = (_b = libFeatures === null || libFeatures === void 0 ? void 0 : libFeatures.supportVdom) !== null && _b !== void 0 ? _b : true;
141
- // 模拟器预览模式下,按照基础库是否支持相关功能 + 用户是否勾选进行判断
142
- enableV2 = supportV2 && settings.enableV2;
143
- // 开启v2不编译vdom
144
- enableVDom = !enableV2 && supportVDom && settings.enableVDom;
145
- enableV1 = !enableV2;
146
- }
147
- else {
148
- // 上传模式下按照勾选开启
149
- enableVDom = settings.enableVDom;
150
- enableV2 = settings.enableV2;
151
- enableV1 = true;
152
- }
153
- return {
154
- enableV1,
155
- enableV2,
156
- enableVDom
157
- };
158
- });
106
+ this.project = (project ||
107
+ new xhs_mp_project_1.Project({
108
+ projectPath: projectPath
109
+ }));
159
110
  }
160
111
  // 根据使用的场景或者配置,采用不同的compiler类型
161
112
  getCompilerType(entryType = constant_config_1.COMPILE_ENTRY.simulator) {
162
113
  const isGame = this.project.appMode === constant_config_1.MiniMode.minigame;
163
- const settings = this.project.projectJsonContent.setting || {};
114
+ const settings = this.project.settings || {};
164
115
  // 使用旧编译
165
116
  if (isGame || !settings.useNewCompiler) {
166
- return entryType === constant_config_1.COMPILE_ENTRY.simulator ? constant_config_1.COMPILER_TYPE.legacy_dev : constant_config_1.COMPILER_TYPE.legacy_build;
117
+ return entryType === constant_config_1.COMPILE_ENTRY.simulator
118
+ ? constant_config_1.COMPILER_TYPE.legacy_dev
119
+ : constant_config_1.COMPILER_TYPE.legacy_build;
167
120
  }
168
121
  // 使用新编译
169
122
  return entryType === constant_config_1.COMPILE_ENTRY.simulator
@@ -173,26 +126,31 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
173
126
  : constant_config_1.COMPILER_TYPE.build;
174
127
  }
175
128
  getDistDir(entryType = constant_config_1.COMPILE_ENTRY.simulator) {
176
- const { mpCompileDirPath } = this.getCompilerDir();
129
+ const { compileDistDir = dir_config_1.mpCompileDirPath } = this.compilerProps;
177
130
  const compilerType = this.getCompilerType(entryType);
178
- const basename = path_1.default.basename(this.project.projectPath);
179
- const hash = (0, utils_1.getMd5)(this.project.projectPath).slice(0, 8);
180
- const distDir = path_1.default.join(path_1.default.resolve(mpCompileDirPath, `${basename}_${hash}`), compilerType);
131
+ const projectPath = this.project.projectPath;
132
+ const basename = path_1.default.basename(projectPath);
133
+ const hash = (0, utils_1.getMd5)(projectPath).slice(0, 8);
134
+ const distDir = path_1.default.join(path_1.default.resolve(compileDistDir, `${basename}_${hash}`), compilerType);
181
135
  fs_extra_1.default.ensureDirSync(distDir);
182
136
  return distDir;
183
137
  }
184
138
  removeDistDir(entryType = constant_config_1.COMPILE_ENTRY.simulator) {
185
- const distDir = this.getDistDir(entryType);
186
- if (fs_extra_1.default.existsSync(distDir)) {
187
- fs_extra_1.default.removeSync(distDir);
139
+ try {
140
+ const distDir = this.getDistDir(entryType);
141
+ if (fs_extra_1.default.existsSync(distDir)) {
142
+ fs_extra_1.default.removeSync(distDir);
143
+ }
188
144
  }
145
+ catch (error) { }
189
146
  }
190
147
  getCacheDir(entryType = constant_config_1.COMPILE_ENTRY.simulator) {
191
- const { mpCompileCachePath } = this.getCompilerDir();
148
+ const { compileCacheDir = dir_config_1.mpCompileCachePath } = this.compilerProps;
149
+ const projectPath = this.project.projectPath;
192
150
  const compilerType = this.getCompilerType(entryType);
193
- const basename = path_1.default.basename(this.project.projectPath);
194
- const hash = (0, utils_1.getMd5)(this.project.projectPath).slice(0, 8);
195
- const distDir = path_1.default.join(path_1.default.resolve(mpCompileCachePath, `${basename}_${hash}`), compilerType);
151
+ const basename = path_1.default.basename(projectPath);
152
+ const hash = (0, utils_1.getMd5)(projectPath).slice(0, 8);
153
+ const distDir = path_1.default.join(path_1.default.resolve(compileCacheDir, `${basename}_${hash}`), compilerType);
196
154
  fs_extra_1.default.ensureDirSync(distDir);
197
155
  return distDir;
198
156
  }
@@ -202,68 +160,199 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
202
160
  fs_extra_1.default.removeSync(cacheDir);
203
161
  }
204
162
  }
205
- createCompilerImpl(entryType = constant_config_1.COMPILE_ENTRY.simulator) {
206
- const { nodeJsPath, mpDevJsonPath } = this.compilerProps;
163
+ removeAllCacheDir() {
164
+ // 移除编译缓存
165
+ const cacheDir = this.getCacheDir();
166
+ const parenCacheDir = path_1.default.dirname(cacheDir);
167
+ if (fs_extra_1.default.existsSync(parenCacheDir)) {
168
+ fs_extra_1.default.removeSync(parenCacheDir);
169
+ }
170
+ // 移除编译产物
171
+ const distDir = this.getDistDir();
172
+ const parentDistDir = path_1.default.dirname(distDir);
173
+ if (fs_extra_1.default.existsSync(parentDistDir)) {
174
+ fs_extra_1.default.removeSync(parentDistDir);
175
+ }
176
+ }
177
+ createCompiler(entryType = constant_config_1.COMPILE_ENTRY.simulator) {
178
+ const { nodeJsPath } = this.compilerProps;
207
179
  const distDir = this.getDistDir(entryType);
208
180
  const cacheDir = this.getCacheDir(entryType);
209
181
  const compilerType = this.getCompilerType(entryType);
210
- compilerType === constant_config_1.COMPILER_TYPE.legacy_dev || compilerType === constant_config_1.COMPILER_TYPE.legacy_build
211
- ? constant_config_2.PACK_MODE.WEBPACK
212
- : constant_config_2.PACK_MODE.MPPACK;
182
+ const packMode = compilerType === constant_config_1.COMPILER_TYPE.legacy_dev || compilerType === constant_config_1.COMPILER_TYPE.legacy_build
183
+ ? 'webpack'
184
+ : 'mp-pack';
213
185
  const options = {
186
+ packMode,
214
187
  projectPath: this.project.projectPath,
215
188
  appMode: this.project.appMode,
216
189
  extJsonPath: this.project.extJsonDir,
217
190
  platform: 'xhs',
218
191
  distDir,
192
+ enablePersistCache: entryType !== constant_config_1.COMPILE_ENTRY.upload,
219
193
  cacheDirectory: cacheDir
220
194
  };
221
195
  this.removeDistDir(entryType);
222
- this.removeCacheDir(entryType);
196
+ // this.removeCacheDir(entryType)
223
197
  const CompilerClass = (0, compiler_1.getProjectCompilerClass)(compilerType);
224
- const compilerImpl = new CompilerClass(options, {
225
- logger: this.logger,
198
+ const compiler = new CompilerClass(options, {
199
+ workerType: entryType === constant_config_1.COMPILE_ENTRY.simulator ? 'web' : 'process',
226
200
  nodeJsPath
227
201
  });
228
- return compilerImpl;
202
+ return compiler;
203
+ }
204
+ // 使用的编译目标包类型
205
+ getUsingPackageType() {
206
+ return __awaiter(this, arguments, void 0, function* (entryType = constant_config_1.COMPILE_ENTRY.simulator) {
207
+ let enableV1 = false;
208
+ let enableV2 = true;
209
+ let enableVDom = false;
210
+ const settings = this.project.settings || {};
211
+ const isUpload = entryType === constant_config_1.COMPILE_ENTRY.upload;
212
+ if (this.project.appMode === constant_config_1.MiniMode.minigame) {
213
+ enableV1 = true;
214
+ enableV2 = false;
215
+ enableVDom = false;
216
+ }
217
+ else if (entryType === constant_config_1.COMPILE_ENTRY.simulator) {
218
+ if (this.compilerProps.getLibFeatures) {
219
+ const libFeatures = yield this.compilerProps.getLibFeatures();
220
+ enableV2 = libFeatures.supportV2 && settings.enableV2;
221
+ enableVDom = !enableV2 && libFeatures.supportVDom && settings.enableVDom;
222
+ }
223
+ else {
224
+ enableV2 = settings.enableV2;
225
+ enableVDom = !enableV2 && settings.enableVDom;
226
+ }
227
+ // enableVDom = !enableV2 && settings.enableVDom
228
+ enableV1 = !enableV2;
229
+ }
230
+ else {
231
+ // 上传模式下按照勾选开启
232
+ enableVDom = settings.enableVDom;
233
+ enableV2 = settings.enableV2;
234
+ enableV1 = true;
235
+ }
236
+ return {
237
+ enableV1,
238
+ enableV2,
239
+ enableVDom
240
+ };
241
+ });
229
242
  }
230
- makePkgsReady(config) {
243
+ makePkgsReady(opts) {
231
244
  return __awaiter(this, void 0, void 0, function* () {
245
+ var _a, _b;
246
+ const { compilePkgs } = opts;
232
247
  const entryType = constant_config_1.COMPILE_ENTRY.simulator;
233
- const compilerImpl = this.getComilerImpl(entryType);
234
- yield compilerImpl.makePkgsReady(config);
248
+ const defaultOptions = yield this.getUsingPackageType(entryType);
249
+ const compiler = this.getCompiler(entryType);
250
+ const startTime = Date.now();
251
+ // 如果要编主包,看看入口页是否普通分包,也加进去
252
+ if (compilePkgs === null || compilePkgs === void 0 ? void 0 : compilePkgs.includes(constant_config_1.MAIN_PKG_ROOT)) {
253
+ const customEntry = this.project.customEntry;
254
+ const subPackages = this.project.getSubPackages();
255
+ const subPkg = subPackages.find((item) => customEntry.startsWith(`${item.root}/`));
256
+ if (subPkg && !subPkg.independent) {
257
+ compilePkgs.push(subPkg.root);
258
+ }
259
+ }
260
+ const config = Object.assign(Object.assign({ compilePkgs, compressCss: false, compressJs: true, enableSourcemap: true, appendSourcemapComment: true, runInServiceSandbox: true, devWriteToDisk: true }, defaultOptions), opts);
261
+ console.log('[compiler]makePkgsReady', config);
262
+ try {
263
+ this.emit('makePkgsReady-start', { config });
264
+ yield compiler.makePkgsReady(config);
265
+ this.emit('makePkgsReady-end', { config });
266
+ (_a = this.report) === null || _a === void 0 ? void 0 : _a.call(this, 'compile', {
267
+ result: 'success',
268
+ entryType,
269
+ config: JSON.stringify(config),
270
+ duration: Date.now() - startTime
271
+ });
272
+ }
273
+ catch (error) {
274
+ this.emit('makePkgsReady-error', { config, error });
275
+ (_b = this.report) === null || _b === void 0 ? void 0 : _b.call(this, 'compile', {
276
+ result: 'fail',
277
+ entryType,
278
+ config: JSON.stringify(config),
279
+ reason: error.message
280
+ });
281
+ throw error;
282
+ }
235
283
  });
236
284
  }
237
- reCompilePkgs(config) {
285
+ reCompilePkgs(opts) {
238
286
  return __awaiter(this, void 0, void 0, function* () {
287
+ var _a, _b, _c;
288
+ const { compilePkgs } = opts;
239
289
  const entryType = constant_config_1.COMPILE_ENTRY.simulator;
240
- const compilerImpl = this.getComilerImpl(entryType);
241
- yield compilerImpl.reCompilePkgs(config);
290
+ const compiler = this.getCompiler(entryType);
291
+ const defaultOptions = yield this.getUsingPackageType(entryType);
292
+ const startTime = Date.now();
293
+ const config = Object.assign(Object.assign({ compilePkgs, compressCss: false, compressJs: true, enableSourcemap: true, appendSourcemapComment: true, runInServiceSandbox: true, devWriteToDisk: true }, defaultOptions), opts);
294
+ console.log('[compiler]reCompilePkgs', config);
295
+ try {
296
+ this.emit('recompile-pkgs-start', { config });
297
+ (_a = (0, sharedFs_1.default)(true)) === null || _a === void 0 ? void 0 : _a.clear();
298
+ yield compiler.reCompilePkgs(config);
299
+ this.emit('recompile-pkgs-end', { config });
300
+ (_b = this.report) === null || _b === void 0 ? void 0 : _b.call(this, 'compile', {
301
+ result: 'success',
302
+ entryType,
303
+ config: JSON.stringify(config),
304
+ duration: Date.now() - startTime
305
+ });
306
+ }
307
+ catch (error) {
308
+ this.emit('recompile-pkgs-error', { config, error });
309
+ (_c = this.report) === null || _c === void 0 ? void 0 : _c.call(this, 'compile', {
310
+ result: 'fail',
311
+ entryType,
312
+ config: JSON.stringify(config),
313
+ reason: error.message
314
+ });
315
+ throw error;
316
+ }
242
317
  });
243
318
  }
244
319
  compileProject(config) {
245
320
  return __awaiter(this, void 0, void 0, function* () {
246
- var _a, _b, _c;
321
+ var _a, _b, _c, _d, _e;
322
+ const { entryType } = config;
247
323
  try {
248
- const { entryType } = config;
249
324
  this.emit('compile-project-start');
250
- (_a = this.logger) === null || _a === void 0 ? void 0 : _a.log('Compile project...');
251
- const compilerImpl = this.getComilerImpl(entryType);
252
- yield compilerImpl.build(config);
253
- this.emit('compile-project-done');
254
- (_b = this.logger) === null || _b === void 0 ? void 0 : _b.log('Compile project done');
325
+ const startTime = Date.now();
326
+ const compiler = this.getCompiler(entryType);
327
+ this.removeDistDir(entryType);
328
+ (_a = (0, sharedFs_1.default)(true)) === null || _a === void 0 ? void 0 : _a.clear();
329
+ (_b = this.logger) === null || _b === void 0 ? void 0 : _b.log('Compile project...');
330
+ yield compiler.build(config);
331
+ (_c = this.logger) === null || _c === void 0 ? void 0 : _c.log('Compile project done...');
332
+ this.emit('compile-project-end');
333
+ (_d = this.report) === null || _d === void 0 ? void 0 : _d.call(this, 'compile', {
334
+ result: 'success',
335
+ entryType,
336
+ config: JSON.stringify(config),
337
+ duration: Date.now() - startTime
338
+ });
255
339
  }
256
340
  catch (stats) {
257
- let error;
341
+ let errors = [];
258
342
  if (stats instanceof Error) {
259
- error = stats;
343
+ errors = [stats.message];
260
344
  }
261
345
  else {
262
- const { errors } = stats || {};
263
- error = new Error(errors.join('\n\n'));
346
+ errors = (stats || {}).errors;
264
347
  }
265
- (_c = this.logger) === null || _c === void 0 ? void 0 : _c.log('Compile project failed', error);
266
- this.emit('compile-project-fail', error);
348
+ this.emit('compile-project-error', { config, errors });
349
+ const error = new Error(errors.join('\n\n'));
350
+ (_e = this.report) === null || _e === void 0 ? void 0 : _e.call(this, 'compile', {
351
+ result: 'fail',
352
+ entryType,
353
+ config: JSON.stringify(config),
354
+ reason: error.message
355
+ });
267
356
  throw error;
268
357
  }
269
358
  });
@@ -271,15 +360,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
271
360
  compileAndZip(opts) {
272
361
  return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
273
362
  var _a;
274
- const { can_upload_ext_json, upload_app_id, entryType } = opts;
363
+ const { can_upload_ext_json, entryType } = opts;
275
364
  try {
276
365
  const appJsonContent = this.project.appJsonContent;
277
366
  const settings = this.project.settings || {};
278
367
  let subPackages = appJsonContent.subPackages || appJsonContent.subpackages || [];
279
368
  const formatGameAppJSON = this.project.formatGameAppJSON;
280
369
  subPackages =
281
- this.project.appMode === constant_config_1.MiniMode.miniprogram ? subPackages : formatGameAppJSON(subPackages);
282
- const { enableV2, enableV1, enableVDom } = yield this.getUsingPackageType(entryType);
370
+ this.project.appMode === constant_config_1.MiniMode.miniprogram
371
+ ? subPackages
372
+ : formatGameAppJSON(subPackages);
373
+ const { enableV1, enableV2, enableVDom } = yield this.getUsingPackageType(entryType);
283
374
  let compressJs = false;
284
375
  let compressCss = false;
285
376
  let enableSourcemap = true;
@@ -288,29 +379,28 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
288
379
  if (entryType === constant_config_1.COMPILE_ENTRY.preview) {
289
380
  compressCss = true;
290
381
  compressJs = true;
382
+ enableSourcemap = false;
383
+ appendSourcemapComment = false;
291
384
  }
292
385
  else if (entryType === constant_config_1.COMPILE_ENTRY.upload) {
293
386
  compressCss = true;
294
387
  compressJs = true;
295
388
  appendSourcemapComment = false;
296
389
  }
297
- else { // 真机调试
390
+ else {
298
391
  compressCss = false;
299
392
  compressJs = (_a = settings.minified) !== null && _a !== void 0 ? _a : false;
300
393
  runInServiceSandbox = true;
301
394
  }
302
- const buildConfig = {
303
- entryType,
395
+ const buildConfig = Object.assign({ entryType,
304
396
  compressCss,
305
397
  compressJs,
306
398
  enableSourcemap,
307
399
  appendSourcemapComment,
308
400
  runInServiceSandbox,
309
- enableV2,
310
401
  enableV1,
311
- enableVDom,
312
- mpUploadOptions: this.project.appMode === constant_config_1.MiniMode.miniprogram && opts
313
- };
402
+ enableV2,
403
+ enableVDom, mpUploadOptions: this.project.appMode === constant_config_1.MiniMode.miniprogram && opts }, opts);
314
404
  // 编译
315
405
  this.emit('compile-and-zip-status', { status: 'build-start' });
316
406
  yield this.compileProject(buildConfig);
@@ -323,7 +413,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
323
413
  this.zipWithFullPackage(opts, subPackages, 'v0'),
324
414
  this.zipWithSubPackage(opts, subPackages, 'v1'),
325
415
  // 小程序模式有v2包
326
- enableV2 && this.zipWithSubPackage(opts, subPackages, 'v2')
416
+ buildConfig.enableV2 && this.zipWithSubPackage(opts, subPackages, 'v2')
327
417
  ];
328
418
  }
329
419
  else if (entryType === constant_config_1.COMPILE_ENTRY.upload) {
@@ -352,18 +442,37 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
352
442
  }
353
443
  }));
354
444
  }
445
+ closeCompiler(entryType = constant_config_1.COMPILE_ENTRY.simulator) {
446
+ const compiler = this.compilerImplMap[entryType];
447
+ if (compiler) {
448
+ compiler.close();
449
+ }
450
+ }
451
+ killCompiler(entryType = constant_config_1.COMPILE_ENTRY.simulator) {
452
+ const compiler = this.compilerImplMap[entryType];
453
+ if (!compiler) {
454
+ return;
455
+ }
456
+ compiler.kill();
457
+ delete this.compilerImplMap[entryType];
458
+ }
459
+ killAll() {
460
+ Object.values(this.compilerImplMap).forEach((compiler) => {
461
+ compiler.kill();
462
+ });
463
+ this.compilerImplMap = {};
464
+ }
355
465
  /* 使用v0的入口,zip整包 */
356
466
  zipWithFullPackage(opts_1) {
357
467
  return __awaiter(this, arguments, void 0, function* (opts, subPackages = [], type, excludeSourcemap) {
358
- const project = this.project;
359
468
  const { upload_app_id, can_upload_ext_json, pkgInfo, entryType } = opts;
360
469
  const buildDir = this.getDistDir(entryType);
361
470
  const appendFile = [];
362
471
  const appendBuffer = [];
363
- appendFile.push(project.projectJsonPath); // zip添加project.config.json
472
+ appendFile.push(this.project.projectJsonPath); // zip添加project.config.json
364
473
  /* 是否需要上传ext.json */
365
- if (can_upload_ext_json && fs_extra_1.default.existsSync(project.extJsonPath)) {
366
- appendFile.push(project.extJsonPath);
474
+ if (can_upload_ext_json && fs_extra_1.default.existsSync(this.project.extJsonPath)) {
475
+ appendFile.push(this.project.extJsonPath);
367
476
  }
368
477
  if (pkgInfo) {
369
478
  /* 是否需要额外添加pkgInfo.json信息 */
@@ -402,12 +511,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
402
511
  /* 使用v1的入口。zip主包和分包 */
403
512
  zipWithSubPackage(opts, subPackages, type, excludeSourcemap) {
404
513
  return __awaiter(this, void 0, void 0, function* () {
405
- const project = this.project;
514
+ const projectConfig = this.project;
406
515
  const { can_upload_ext_json, pkgInfo, entryType } = opts;
407
516
  const isRemoteDebug = entryType === constant_config_1.COMPILE_ENTRY.remotedebug;
408
517
  const appendFile = []; // 额外的文件
409
518
  const appendBuffer = [];
410
- appendFile.push(project.projectJsonPath); // zip添加project.config.json
519
+ appendFile.push(projectConfig.projectJsonPath); // zip添加project.config.json
411
520
  const buildDir = this.getDistDir(entryType);
412
521
  const mainEntry = path_1.default.resolve(buildDir, type);
413
522
  // 主包里需要忽略分包的路径
@@ -418,8 +527,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
418
527
  subIgnore.push('**/*.map');
419
528
  }
420
529
  /* 是否需要上传ext.json */
421
- if (can_upload_ext_json && fs_extra_1.default.existsSync(project.extJsonPath)) {
422
- appendFile.push(project.extJsonPath);
530
+ if (can_upload_ext_json && fs_extra_1.default.existsSync(projectConfig.extJsonPath)) {
531
+ appendFile.push(projectConfig.extJsonPath);
423
532
  }
424
533
  if (pkgInfo) {
425
534
  /* 是否需要额外添加pkgInfo.json信息 */
@@ -480,22 +589,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
480
589
  return Promise.all(tasks);
481
590
  });
482
591
  }
483
- close(entryType = constant_config_1.COMPILE_ENTRY.simulator) {
484
- const compilerType = this.getCompilerType(entryType);
485
- const compiler = this.compilerImplMap[compilerType];
486
- if (compiler) {
487
- compiler.close();
488
- }
489
- }
490
- kill(entryType = constant_config_1.COMPILE_ENTRY.simulator) {
491
- const compilerType = this.getCompilerType(entryType);
492
- const compiler = this.compilerImplMap[compilerType];
493
- if (!compiler) {
494
- return;
495
- }
496
- compiler.kill();
497
- delete this.compilerImplMap[entryType];
498
- }
499
592
  }
500
- exports.ProjectCompiler = ProjectCompiler;
593
+ exports.ProjectCompilerManager = ProjectCompilerManager;
501
594
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xhs-mp-compiler-cli",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "description": "xhs mp command tool.",
5
5
  "preferGlobal": true,
6
6
  "category": "esm",
@@ -94,17 +94,17 @@
94
94
  "webpack-chain": "^6.5.1",
95
95
  "webpack-sources": "^3.2.2",
96
96
  "xhs-mp-workerpool": "^9.1.3",
97
- "xhs-mp-ml-loader": "2.0.1",
98
- "xhs-mp-compiler-utils": "2.0.1",
99
- "xhs-mp-pack": "2.0.1",
100
- "xhs-mp-project": "2.0.1",
101
- "xhs-mp-shared": "2.0.1",
102
- "xhs-mp-shared-fs": "2.0.1",
103
- "xhs-mp-sjs-loader": "2.0.1",
97
+ "xhs-mp-ml-loader": "2.0.3",
98
+ "xhs-mp-compiler-utils": "2.0.3",
99
+ "xhs-mp-pack": "2.0.3",
100
+ "xhs-mp-project": "2.0.3",
101
+ "xhs-mp-shared": "2.0.3",
102
+ "xhs-mp-shared-fs": "2.0.3",
103
+ "xhs-mp-sjs-loader": "2.0.3",
104
104
  "yauzl": "^2.10.0"
105
105
  },
106
106
  "peerDependencies": {
107
- "xhs-mp-ml-parser": "2.0.1"
107
+ "xhs-mp-ml-parser": "2.0.3"
108
108
  },
109
109
  "devDependencies": {
110
110
  "@types/babel__generator": "7.6.3",
@@ -129,7 +129,7 @@
129
129
  "typescript": "5.1.6",
130
130
  "vue3-jest": "27.0.0-alpha.2",
131
131
  "webpack-dev-server": "4.0.0-beta.3",
132
- "xhs-mp-ml-parser": "2.0.1"
132
+ "xhs-mp-ml-parser": "2.0.3"
133
133
  },
134
134
  "scripts": {
135
135
  "version": "formula changelog && git add .",