xhs-mp-compiler-cli 2.0.4 → 2.0.5
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/dist/compilerManager.d.ts +95 -0
- package/dist/compilerManager.js +590 -0
- package/dist/index.d.ts +1 -95
- package/dist/index.js +15 -582
- package/package.json +11 -11
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { COMPILE_ENTRY } from './config/constant.config';
|
|
2
|
+
import { Project } from 'xhs-mp-project';
|
|
3
|
+
import EventEmitter from 'events';
|
|
4
|
+
import { Logger } from 'xhs-mp-shared';
|
|
5
|
+
import { prepareDevPool } from './compiler';
|
|
6
|
+
export { COMPILE_ENTRY, prepareDevPool };
|
|
7
|
+
interface ILibFeatures {
|
|
8
|
+
supportV2?: boolean;
|
|
9
|
+
supportVDom?: boolean;
|
|
10
|
+
}
|
|
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
|
+
};
|
|
19
|
+
export type ICompileOpts = {
|
|
20
|
+
logger?: any;
|
|
21
|
+
report?: (key: string, data?: any) => void;
|
|
22
|
+
compileDistDir?: string;
|
|
23
|
+
compileCacheDir?: string;
|
|
24
|
+
nodeJsPath?: string;
|
|
25
|
+
getLibFeatures?: () => Promise<ILibFeatures>;
|
|
26
|
+
};
|
|
27
|
+
export type ICompilerProps = {
|
|
28
|
+
projectPath?: string;
|
|
29
|
+
project?: Project;
|
|
30
|
+
} & ICompileOpts;
|
|
31
|
+
interface ICompileBaseConfig {
|
|
32
|
+
compressCss?: boolean;
|
|
33
|
+
compressJs?: boolean;
|
|
34
|
+
enableSourcemap?: boolean;
|
|
35
|
+
appendSourcemapComment?: boolean;
|
|
36
|
+
runInServiceSandbox?: boolean;
|
|
37
|
+
enableV2?: boolean;
|
|
38
|
+
enableV1?: boolean;
|
|
39
|
+
enableVDom?: boolean;
|
|
40
|
+
}
|
|
41
|
+
type IMakePkgsReadyConfig = ICompileBaseConfig & {
|
|
42
|
+
compilePkgs?: string[];
|
|
43
|
+
};
|
|
44
|
+
type ICompilePkgsConfig = ICompileBaseConfig & {
|
|
45
|
+
compilePkgs?: string[];
|
|
46
|
+
};
|
|
47
|
+
type ICompileProjectConfig = ICompileBaseConfig & {
|
|
48
|
+
entryType?: string;
|
|
49
|
+
mpUploadOptions?: ICompileAndZipOptions | false;
|
|
50
|
+
};
|
|
51
|
+
export interface ICompileAndZipOptions {
|
|
52
|
+
entryType?: string;
|
|
53
|
+
can_upload_ext_json?: boolean;
|
|
54
|
+
upload_app_id?: string;
|
|
55
|
+
pkgInfo?: {
|
|
56
|
+
version: string;
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
export interface ISubPackages {
|
|
60
|
+
root: string;
|
|
61
|
+
pages: string;
|
|
62
|
+
}
|
|
63
|
+
export declare class ProjectCompilerManager extends EventEmitter {
|
|
64
|
+
compilerProps: ICompilerProps;
|
|
65
|
+
project: Project;
|
|
66
|
+
compilerImplMap: {};
|
|
67
|
+
logger: Logger;
|
|
68
|
+
report?: (key: string, data?: any) => void;
|
|
69
|
+
constructor(props: ICompilerProps);
|
|
70
|
+
bindProject(props: any): void;
|
|
71
|
+
private getCompilerType;
|
|
72
|
+
getDistDir(entryType?: string): string;
|
|
73
|
+
removeDistDir(entryType?: string): void;
|
|
74
|
+
getCacheDir(entryType?: string): string;
|
|
75
|
+
removeCacheDir(entryType?: string): void;
|
|
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;
|
|
93
|
+
private zipWithFullPackage;
|
|
94
|
+
private zipWithSubPackage;
|
|
95
|
+
}
|
|
@@ -0,0 +1,590 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
11
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
12
|
+
};
|
|
13
|
+
(function (factory) {
|
|
14
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
15
|
+
var v = factory(require, exports);
|
|
16
|
+
if (v !== undefined) module.exports = v;
|
|
17
|
+
}
|
|
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", "./sharedFs"], factory);
|
|
20
|
+
}
|
|
21
|
+
})(function (require, exports) {
|
|
22
|
+
"use strict";
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.ProjectCompilerManager = exports.prepareDevPool = exports.COMPILE_ENTRY = void 0;
|
|
25
|
+
const constant_config_1 = require("./config/constant.config");
|
|
26
|
+
Object.defineProperty(exports, "COMPILE_ENTRY", { enumerable: true, get: function () { return constant_config_1.COMPILE_ENTRY; } });
|
|
27
|
+
const xhs_mp_project_1 = require("xhs-mp-project");
|
|
28
|
+
const events_1 = __importDefault(require("events"));
|
|
29
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
30
|
+
const path_1 = __importDefault(require("path"));
|
|
31
|
+
const utils_1 = require("./utils/utils");
|
|
32
|
+
const project_1 = require("./utils/project");
|
|
33
|
+
const dir_config_1 = require("./config/dir.config");
|
|
34
|
+
const compiler_1 = require("./compiler");
|
|
35
|
+
Object.defineProperty(exports, "prepareDevPool", { enumerable: true, get: function () { return compiler_1.prepareDevPool; } });
|
|
36
|
+
const sharedFs_1 = __importDefault(require("./sharedFs"));
|
|
37
|
+
class ProjectCompilerManager extends events_1.default {
|
|
38
|
+
constructor(props) {
|
|
39
|
+
super();
|
|
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
|
+
};
|
|
53
|
+
this.initSimulatorCompiler = compiler => {
|
|
54
|
+
compiler.on('buildStart', data => {
|
|
55
|
+
this.emit('simulator-compile-start', data);
|
|
56
|
+
});
|
|
57
|
+
compiler.on('compileInfo', data => {
|
|
58
|
+
this.emit('simulator-compile-info', data);
|
|
59
|
+
});
|
|
60
|
+
compiler.on('buildSuccess', data => {
|
|
61
|
+
this.emit('simulator-compile-success', data);
|
|
62
|
+
});
|
|
63
|
+
compiler.on('buildError', data => {
|
|
64
|
+
this.emit('simulator-compile-error', data);
|
|
65
|
+
});
|
|
66
|
+
compiler.on('compileFinishWhenFileChange', data => {
|
|
67
|
+
this.emit('simulator-file-change-compile-finish', data);
|
|
68
|
+
});
|
|
69
|
+
compiler.on('compilePercent', data => {
|
|
70
|
+
this.emit('simulator-compile-percent', data);
|
|
71
|
+
});
|
|
72
|
+
compiler.on('emitCompileStats', data => {
|
|
73
|
+
this.emit('simulator-compile-stats', data);
|
|
74
|
+
});
|
|
75
|
+
compiler.on('unexpectedExit', data => {
|
|
76
|
+
this.emit('simulator-compile-unexpectedExit', data);
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
this.initBuildCompiler = compiler => {
|
|
80
|
+
compiler.on('compilePercent', data => {
|
|
81
|
+
this.emit('full-compile-percent', data);
|
|
82
|
+
});
|
|
83
|
+
compiler.on('compileInfo', data => {
|
|
84
|
+
this.emit('full-compiler-info', data);
|
|
85
|
+
});
|
|
86
|
+
compiler.on('emitCompileStats', data => {
|
|
87
|
+
this.emit('full-compile-stats', data);
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
const { logger, report, projectPath, project } = props;
|
|
91
|
+
this.logger = logger || console;
|
|
92
|
+
this.report = report;
|
|
93
|
+
this.compilerProps = props;
|
|
94
|
+
if (!project && !projectPath)
|
|
95
|
+
return;
|
|
96
|
+
this.project = (project ||
|
|
97
|
+
new xhs_mp_project_1.Project({
|
|
98
|
+
projectPath: projectPath
|
|
99
|
+
}));
|
|
100
|
+
}
|
|
101
|
+
bindProject(props) {
|
|
102
|
+
const { projectPath, project } = props;
|
|
103
|
+
if (!project && !projectPath) {
|
|
104
|
+
throw new Error('project 和 projectPath 均为undefined');
|
|
105
|
+
}
|
|
106
|
+
this.project = (project ||
|
|
107
|
+
new xhs_mp_project_1.Project({
|
|
108
|
+
projectPath: projectPath
|
|
109
|
+
}));
|
|
110
|
+
}
|
|
111
|
+
// 根据使用的场景或者配置,采用不同的compiler类型
|
|
112
|
+
getCompilerType(entryType = constant_config_1.COMPILE_ENTRY.simulator) {
|
|
113
|
+
const isGame = this.project.appMode === constant_config_1.MiniMode.minigame;
|
|
114
|
+
const settings = this.project.settings || {};
|
|
115
|
+
// 使用旧编译
|
|
116
|
+
if (isGame || !settings.useNewCompiler) {
|
|
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;
|
|
120
|
+
}
|
|
121
|
+
// 使用新编译
|
|
122
|
+
return entryType === constant_config_1.COMPILE_ENTRY.simulator
|
|
123
|
+
? constant_config_1.COMPILER_TYPE.dev
|
|
124
|
+
: entryType === constant_config_1.COMPILE_ENTRY.upload
|
|
125
|
+
? constant_config_1.COMPILER_TYPE.legacy_build
|
|
126
|
+
: constant_config_1.COMPILER_TYPE.build;
|
|
127
|
+
}
|
|
128
|
+
getDistDir(entryType = constant_config_1.COMPILE_ENTRY.simulator) {
|
|
129
|
+
const { compileDistDir = dir_config_1.mpCompileDirPath } = this.compilerProps;
|
|
130
|
+
const compilerType = this.getCompilerType(entryType);
|
|
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);
|
|
135
|
+
fs_extra_1.default.ensureDirSync(distDir);
|
|
136
|
+
return distDir;
|
|
137
|
+
}
|
|
138
|
+
removeDistDir(entryType = constant_config_1.COMPILE_ENTRY.simulator) {
|
|
139
|
+
try {
|
|
140
|
+
const distDir = this.getDistDir(entryType);
|
|
141
|
+
if (fs_extra_1.default.existsSync(distDir)) {
|
|
142
|
+
fs_extra_1.default.removeSync(distDir);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
catch (error) { }
|
|
146
|
+
}
|
|
147
|
+
getCacheDir(entryType = constant_config_1.COMPILE_ENTRY.simulator) {
|
|
148
|
+
const { compileCacheDir = dir_config_1.mpCompileCachePath } = this.compilerProps;
|
|
149
|
+
const projectPath = this.project.projectPath;
|
|
150
|
+
const compilerType = this.getCompilerType(entryType);
|
|
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);
|
|
154
|
+
fs_extra_1.default.ensureDirSync(distDir);
|
|
155
|
+
return distDir;
|
|
156
|
+
}
|
|
157
|
+
removeCacheDir(entryType = constant_config_1.COMPILE_ENTRY.simulator) {
|
|
158
|
+
const cacheDir = this.getCacheDir(entryType);
|
|
159
|
+
if (fs_extra_1.default.existsSync(cacheDir)) {
|
|
160
|
+
fs_extra_1.default.removeSync(cacheDir);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
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;
|
|
179
|
+
const distDir = this.getDistDir(entryType);
|
|
180
|
+
const cacheDir = this.getCacheDir(entryType);
|
|
181
|
+
const compilerType = this.getCompilerType(entryType);
|
|
182
|
+
const packMode = compilerType === constant_config_1.COMPILER_TYPE.legacy_dev || compilerType === constant_config_1.COMPILER_TYPE.legacy_build
|
|
183
|
+
? 'webpack'
|
|
184
|
+
: 'mp-pack';
|
|
185
|
+
const options = {
|
|
186
|
+
packMode,
|
|
187
|
+
projectPath: this.project.projectPath,
|
|
188
|
+
appMode: this.project.appMode,
|
|
189
|
+
extJsonPath: this.project.extJsonDir,
|
|
190
|
+
platform: 'xhs',
|
|
191
|
+
distDir,
|
|
192
|
+
enablePersistCache: entryType !== constant_config_1.COMPILE_ENTRY.upload,
|
|
193
|
+
cacheDirectory: cacheDir
|
|
194
|
+
};
|
|
195
|
+
this.removeDistDir(entryType);
|
|
196
|
+
// this.removeCacheDir(entryType)
|
|
197
|
+
const CompilerClass = (0, compiler_1.getProjectCompilerClass)(compilerType);
|
|
198
|
+
const compiler = new CompilerClass(options, {
|
|
199
|
+
workerType: entryType === constant_config_1.COMPILE_ENTRY.simulator ? 'web' : 'process',
|
|
200
|
+
nodeJsPath
|
|
201
|
+
});
|
|
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
|
+
});
|
|
242
|
+
}
|
|
243
|
+
makePkgsReady(opts) {
|
|
244
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
245
|
+
var _a, _b;
|
|
246
|
+
const { compilePkgs } = opts;
|
|
247
|
+
const entryType = constant_config_1.COMPILE_ENTRY.simulator;
|
|
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 === null || error === void 0 ? void 0 : error.message
|
|
280
|
+
});
|
|
281
|
+
throw error;
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
reCompilePkgs(opts) {
|
|
286
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
287
|
+
var _a, _b, _c;
|
|
288
|
+
const { compilePkgs } = opts;
|
|
289
|
+
const entryType = constant_config_1.COMPILE_ENTRY.simulator;
|
|
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
|
+
}
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
compileProject(config) {
|
|
320
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
321
|
+
var _a, _b, _c, _d, _e;
|
|
322
|
+
const { entryType } = config;
|
|
323
|
+
try {
|
|
324
|
+
this.emit('compile-project-start');
|
|
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
|
+
});
|
|
339
|
+
}
|
|
340
|
+
catch (stats) {
|
|
341
|
+
let errors = [];
|
|
342
|
+
if (stats.errors) {
|
|
343
|
+
errors = stats.errors;
|
|
344
|
+
}
|
|
345
|
+
else if (stats instanceof Error) {
|
|
346
|
+
errors = [stats.message];
|
|
347
|
+
}
|
|
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
|
+
});
|
|
356
|
+
throw error;
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
compileAndZip(opts) {
|
|
361
|
+
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
362
|
+
var _a;
|
|
363
|
+
const { can_upload_ext_json, entryType } = opts;
|
|
364
|
+
try {
|
|
365
|
+
const appJsonContent = this.project.appJsonContent;
|
|
366
|
+
const settings = this.project.settings || {};
|
|
367
|
+
let subPackages = appJsonContent.subPackages || appJsonContent.subpackages || [];
|
|
368
|
+
const formatGameAppJSON = this.project.formatGameAppJSON;
|
|
369
|
+
subPackages =
|
|
370
|
+
this.project.appMode === constant_config_1.MiniMode.miniprogram
|
|
371
|
+
? subPackages
|
|
372
|
+
: formatGameAppJSON(subPackages);
|
|
373
|
+
const { enableV1, enableV2, enableVDom } = yield this.getUsingPackageType(entryType);
|
|
374
|
+
let compressJs = false;
|
|
375
|
+
let compressCss = false;
|
|
376
|
+
let enableSourcemap = true;
|
|
377
|
+
let appendSourcemapComment = true;
|
|
378
|
+
let runInServiceSandbox = false;
|
|
379
|
+
if (entryType === constant_config_1.COMPILE_ENTRY.preview) {
|
|
380
|
+
compressCss = true;
|
|
381
|
+
compressJs = true;
|
|
382
|
+
enableSourcemap = false;
|
|
383
|
+
appendSourcemapComment = false;
|
|
384
|
+
}
|
|
385
|
+
else if (entryType === constant_config_1.COMPILE_ENTRY.upload) {
|
|
386
|
+
compressCss = true;
|
|
387
|
+
compressJs = true;
|
|
388
|
+
appendSourcemapComment = false;
|
|
389
|
+
}
|
|
390
|
+
else {
|
|
391
|
+
compressCss = false;
|
|
392
|
+
compressJs = (_a = settings.minified) !== null && _a !== void 0 ? _a : false;
|
|
393
|
+
runInServiceSandbox = true;
|
|
394
|
+
}
|
|
395
|
+
const buildConfig = Object.assign({ entryType,
|
|
396
|
+
compressCss,
|
|
397
|
+
compressJs,
|
|
398
|
+
enableSourcemap,
|
|
399
|
+
appendSourcemapComment,
|
|
400
|
+
runInServiceSandbox,
|
|
401
|
+
enableV1,
|
|
402
|
+
enableV2,
|
|
403
|
+
enableVDom, mpUploadOptions: this.project.appMode === constant_config_1.MiniMode.miniprogram && opts }, opts);
|
|
404
|
+
// 编译
|
|
405
|
+
this.emit('compile-and-zip-status', { status: 'build-start' });
|
|
406
|
+
yield this.compileProject(buildConfig);
|
|
407
|
+
this.emit('compile-and-zip-status', { status: 'build-done' });
|
|
408
|
+
// 生成zip
|
|
409
|
+
this.emit('compile-and-zip-status', { status: 'zip-start' });
|
|
410
|
+
let entryZips = [];
|
|
411
|
+
if (entryType === constant_config_1.COMPILE_ENTRY.preview) {
|
|
412
|
+
entryZips = [
|
|
413
|
+
this.zipWithFullPackage(opts, subPackages, 'v0'),
|
|
414
|
+
this.zipWithSubPackage(opts, subPackages, 'v1'),
|
|
415
|
+
// 小程序模式有v2包
|
|
416
|
+
buildConfig.enableV2 && this.zipWithSubPackage(opts, subPackages, 'v2')
|
|
417
|
+
];
|
|
418
|
+
}
|
|
419
|
+
else if (entryType === constant_config_1.COMPILE_ENTRY.upload) {
|
|
420
|
+
entryZips = [
|
|
421
|
+
this.zipWithFullPackage(opts, subPackages, 'v0', true),
|
|
422
|
+
this.zipWithSubPackage(opts, subPackages, 'v1', true),
|
|
423
|
+
// 小程序模式有v2包
|
|
424
|
+
enableV2 && this.zipWithSubPackage(opts, subPackages, 'v2', true)
|
|
425
|
+
];
|
|
426
|
+
}
|
|
427
|
+
else {
|
|
428
|
+
// 真机调试
|
|
429
|
+
entryZips = [this.zipWithSubPackage(opts, subPackages, enableV2 ? 'v2' : 'v1')];
|
|
430
|
+
}
|
|
431
|
+
const zipTask = yield Promise.all(entryZips.filter(Boolean));
|
|
432
|
+
const zipResult = zipTask.flat(2);
|
|
433
|
+
this.emit('compile-and-zip-status', { status: 'zip-done' });
|
|
434
|
+
resolve(zipResult);
|
|
435
|
+
}
|
|
436
|
+
catch (error) {
|
|
437
|
+
reject(error);
|
|
438
|
+
}
|
|
439
|
+
}));
|
|
440
|
+
}
|
|
441
|
+
closeCompiler(entryType = constant_config_1.COMPILE_ENTRY.simulator) {
|
|
442
|
+
const compiler = this.compilerImplMap[entryType];
|
|
443
|
+
if (compiler) {
|
|
444
|
+
compiler.close();
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
killCompiler(entryType = constant_config_1.COMPILE_ENTRY.simulator) {
|
|
448
|
+
const compiler = this.compilerImplMap[entryType];
|
|
449
|
+
if (!compiler) {
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
compiler.kill();
|
|
453
|
+
delete this.compilerImplMap[entryType];
|
|
454
|
+
}
|
|
455
|
+
killAll() {
|
|
456
|
+
Object.values(this.compilerImplMap).forEach((compiler) => {
|
|
457
|
+
compiler.kill();
|
|
458
|
+
});
|
|
459
|
+
this.compilerImplMap = {};
|
|
460
|
+
}
|
|
461
|
+
/* 使用v0的入口,zip整包 */
|
|
462
|
+
zipWithFullPackage(opts_1) {
|
|
463
|
+
return __awaiter(this, arguments, void 0, function* (opts, subPackages = [], type, excludeSourcemap) {
|
|
464
|
+
const { upload_app_id, can_upload_ext_json, pkgInfo, entryType } = opts;
|
|
465
|
+
const buildDir = this.getDistDir(entryType);
|
|
466
|
+
const appendFile = [];
|
|
467
|
+
const appendBuffer = [];
|
|
468
|
+
appendFile.push(this.project.projectJsonPath); // zip添加project.config.json
|
|
469
|
+
/* 是否需要上传ext.json */
|
|
470
|
+
if (can_upload_ext_json && fs_extra_1.default.existsSync(this.project.extJsonPath)) {
|
|
471
|
+
appendFile.push(this.project.extJsonPath);
|
|
472
|
+
}
|
|
473
|
+
if (pkgInfo) {
|
|
474
|
+
/* 是否需要额外添加pkgInfo.json信息 */
|
|
475
|
+
const content = { version: pkgInfo.version };
|
|
476
|
+
const contentStr = JSON.stringify(content);
|
|
477
|
+
appendBuffer.push({
|
|
478
|
+
name: 'pkgInfo.json',
|
|
479
|
+
buffer: Buffer.from(contentStr, 'utf-8')
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
let ignore = [];
|
|
483
|
+
if (excludeSourcemap) {
|
|
484
|
+
ignore = ['**/*.map'];
|
|
485
|
+
}
|
|
486
|
+
const tasks = [];
|
|
487
|
+
const baseZipConfig = {
|
|
488
|
+
inputDir: path_1.default.resolve(buildDir, !subPackages.length ? 'v1' : 'v0' // 如果没有开启分包,则把v1包当作v0包
|
|
489
|
+
), // 输入的路径
|
|
490
|
+
type,
|
|
491
|
+
prefix: upload_app_id,
|
|
492
|
+
meta: {
|
|
493
|
+
root: ''
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
tasks.push((0, project_1.zipFactory)(Object.assign(Object.assign({}, baseZipConfig), { pattern: '**/*', ignore,
|
|
497
|
+
appendFile,
|
|
498
|
+
appendBuffer, outputDir: path_1.default.resolve(buildDir, `${type}-full-pack.zip`) // 输出的路径
|
|
499
|
+
})));
|
|
500
|
+
if (excludeSourcemap) {
|
|
501
|
+
tasks.push((0, project_1.zipFactory)(Object.assign(Object.assign({}, baseZipConfig), { pattern: '**/*.map', ignore: [], appendFile: [], appendBuffer: [], outputDir: path_1.default.resolve(buildDir, `${type}-full-pack-sourcemap.zip`) // 输出的路径
|
|
502
|
+
})));
|
|
503
|
+
}
|
|
504
|
+
return Promise.all(tasks);
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
/* 使用v1的入口。zip主包和分包 */
|
|
508
|
+
zipWithSubPackage(opts, subPackages, type, excludeSourcemap) {
|
|
509
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
510
|
+
const projectConfig = this.project;
|
|
511
|
+
const { can_upload_ext_json, pkgInfo, entryType } = opts;
|
|
512
|
+
const isRemoteDebug = entryType === constant_config_1.COMPILE_ENTRY.remotedebug;
|
|
513
|
+
const appendFile = []; // 额外的文件
|
|
514
|
+
const appendBuffer = [];
|
|
515
|
+
appendFile.push(projectConfig.projectJsonPath); // zip添加project.config.json
|
|
516
|
+
const buildDir = this.getDistDir(entryType);
|
|
517
|
+
const mainEntry = path_1.default.resolve(buildDir, type);
|
|
518
|
+
// 主包里需要忽略分包的路径
|
|
519
|
+
const mainIgnore = (subPackages || []).map((item) => item.root.endsWith('/') ? `${item.root}**` : `${item.root}/**`);
|
|
520
|
+
const subIgnore = [];
|
|
521
|
+
if (excludeSourcemap) {
|
|
522
|
+
mainIgnore.push('**/*.map');
|
|
523
|
+
subIgnore.push('**/*.map');
|
|
524
|
+
}
|
|
525
|
+
/* 是否需要上传ext.json */
|
|
526
|
+
if (can_upload_ext_json && fs_extra_1.default.existsSync(projectConfig.extJsonPath)) {
|
|
527
|
+
appendFile.push(projectConfig.extJsonPath);
|
|
528
|
+
}
|
|
529
|
+
if (pkgInfo) {
|
|
530
|
+
/* 是否需要额外添加pkgInfo.json信息 */
|
|
531
|
+
const content = { version: pkgInfo.version };
|
|
532
|
+
const contentStr = JSON.stringify(content);
|
|
533
|
+
appendBuffer.push({
|
|
534
|
+
name: 'pkgInfo.json',
|
|
535
|
+
buffer: Buffer.from(contentStr, 'utf-8')
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
const tasks = [];
|
|
539
|
+
// // 主包
|
|
540
|
+
tasks.push((0, project_1.zipFactory)({
|
|
541
|
+
inputDir: mainEntry, // 输入的路径
|
|
542
|
+
type,
|
|
543
|
+
prefix: '',
|
|
544
|
+
meta: {
|
|
545
|
+
root: '/'
|
|
546
|
+
},
|
|
547
|
+
pattern: '**/*',
|
|
548
|
+
ignore: mainIgnore,
|
|
549
|
+
appendFile,
|
|
550
|
+
appendBuffer,
|
|
551
|
+
outputDir: path_1.default.resolve(buildDir, `${type}-main-pack.zip`) // 输出的路径
|
|
552
|
+
}));
|
|
553
|
+
(subPackages || []).forEach((item, index) => {
|
|
554
|
+
const pkgRoot = item.root.replace(/\//g, constant_config_1.PKG_ROOT_SEP);
|
|
555
|
+
tasks.push((0, project_1.zipFactory)({
|
|
556
|
+
inputDir: path_1.default.resolve(buildDir, type, item.root),
|
|
557
|
+
prefix: `${item.root}`,
|
|
558
|
+
appendFile: [],
|
|
559
|
+
appendBuffer: [],
|
|
560
|
+
pattern: '**/*',
|
|
561
|
+
ignore: subIgnore,
|
|
562
|
+
type,
|
|
563
|
+
meta: {
|
|
564
|
+
root: item.root
|
|
565
|
+
},
|
|
566
|
+
outputDir: path_1.default.resolve(buildDir, `${type}-sub-pack-${isRemoteDebug ? pkgRoot : index}.zip`)
|
|
567
|
+
}));
|
|
568
|
+
});
|
|
569
|
+
// sourcemap
|
|
570
|
+
if (excludeSourcemap) {
|
|
571
|
+
tasks.push((0, project_1.zipFactory)({
|
|
572
|
+
inputDir: mainEntry, // 输入的路径
|
|
573
|
+
pattern: '**/*.map',
|
|
574
|
+
ignore: [],
|
|
575
|
+
appendFile: [],
|
|
576
|
+
appendBuffer: [],
|
|
577
|
+
type,
|
|
578
|
+
prefix: '',
|
|
579
|
+
meta: {
|
|
580
|
+
root: '/'
|
|
581
|
+
},
|
|
582
|
+
outputDir: path_1.default.resolve(buildDir, `${type}-pack-sourcemap.zip`) // 输出的路径
|
|
583
|
+
}));
|
|
584
|
+
}
|
|
585
|
+
return Promise.all(tasks);
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
exports.ProjectCompilerManager = ProjectCompilerManager;
|
|
590
|
+
});
|