@cocos/ccbuild 1.1.19 → 1.1.20

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 (46) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +37 -37
  3. package/lib/api-builder.js +1 -1
  4. package/lib/build-engine/engine-js/index.d.ts +2 -2
  5. package/lib/build-engine/engine-js/index.js +406 -406
  6. package/lib/build-engine/engine-js/rollup-plugins/external-wasm-loader.d.ts +57 -57
  7. package/lib/build-engine/engine-js/rollup-plugins/external-wasm-loader.js +326 -326
  8. package/lib/build-engine/engine-js/rollup-plugins/remove-deprecated-features.d.ts +2 -2
  9. package/lib/build-engine/engine-js/rollup-plugins/remove-deprecated-features.js +37 -37
  10. package/lib/build-engine/engine-js/rollup-plugins/ts-paths.d.ts +4 -4
  11. package/lib/build-engine/engine-js/rollup-plugins/ts-paths.js +42 -42
  12. package/lib/build-engine/engine-ts/engine-builder.d.ts +57 -57
  13. package/lib/build-engine/engine-ts/engine-builder.js +631 -631
  14. package/lib/build-engine/engine-ts/field-decorator-helper.d.ts +9 -9
  15. package/lib/build-engine/engine-ts/field-decorator-helper.js +54 -54
  16. package/lib/build-engine/engine-ts/index.d.ts +2 -2
  17. package/lib/build-engine/engine-ts/index.js +36 -36
  18. package/lib/build-engine/engine-ts/plugins/external-wasm-loader.d.ts +6 -6
  19. package/lib/build-engine/engine-ts/plugins/external-wasm-loader.js +65 -65
  20. package/lib/build-engine/engine-ts/plugins/interface.d.ts +20 -20
  21. package/lib/build-engine/engine-ts/plugins/interface.js +1 -1
  22. package/lib/build-engine/index.d.ts +147 -145
  23. package/lib/build-engine/index.d.ts.map +1 -1
  24. package/lib/build-engine/index.js +168 -167
  25. package/lib/build-engine/index.js.map +1 -1
  26. package/lib/index.d.ts +4 -4
  27. package/lib/index.js +33 -33
  28. package/lib/module-resolver.d.ts +7 -7
  29. package/lib/module-resolver.js +28 -28
  30. package/lib/stats-query/config-interface.d.ts +129 -129
  31. package/lib/stats-query/config-interface.js +3 -3
  32. package/lib/stats-query/index.d.ts +183 -183
  33. package/lib/stats-query/index.js +531 -531
  34. package/lib/stats-query/path-utils.d.ts +2 -2
  35. package/lib/stats-query/path-utils.js +35 -35
  36. package/lib/transformer/babel.d.ts +5 -5
  37. package/lib/transformer/babel.js +34 -34
  38. package/lib/transformer/index.d.ts +2 -2
  39. package/lib/transformer/index.js +28 -28
  40. package/lib/utils.d.ts +4 -4
  41. package/lib/utils.js +23 -23
  42. package/package.json +1 -1
  43. package/static/helper-dynamic-constants.txt +13 -13
  44. package/static/helper-file-decorator.ts +20 -20
  45. package/static/helper-global-exporter.txt +8 -8
  46. package/static/lib.dom.d.ts +20227 -20227
@@ -1,532 +1,532 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
- return new (P || (P = Promise))(function (resolve, reject) {
28
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
- step((generator = generator.apply(thisArg, _arguments || [])).next());
32
- });
33
- };
34
- var __importDefault = (this && this.__importDefault) || function (mod) {
35
- return (mod && mod.__esModule) ? mod : { "default": mod };
36
- };
37
- Object.defineProperty(exports, "__esModule", { value: true });
38
- exports.StatsQuery = exports.ConfigInterface = void 0;
39
- const path_1 = __importDefault(require("path"));
40
- const fs_extra_1 = __importDefault(require("fs-extra"));
41
- const json5_1 = __importDefault(require("json5"));
42
- const dedent_1 = __importDefault(require("dedent"));
43
- const ConfigInterface = __importStar(require("./config-interface"));
44
- exports.ConfigInterface = ConfigInterface;
45
- /**
46
- * Query any any stats of the engine.
47
- */
48
- class StatsQuery {
49
- /**
50
- * @param engine Path to the engine root.
51
- */
52
- static create(engine) {
53
- return __awaiter(this, void 0, void 0, function* () {
54
- const configFile = path_1.default.join(engine, 'cc.config.json');
55
- const config = json5_1.default.parse(yield fs_extra_1.default.readFile(configFile, 'utf8'));
56
- // @ts-ignore
57
- delete config['$schema'];
58
- const query = new StatsQuery(engine, config);
59
- yield query._initialize();
60
- return query;
61
- });
62
- }
63
- /**
64
- * Gets the path to the engine root.
65
- */
66
- get path() {
67
- return this._engine;
68
- }
69
- /**
70
- * Gets the path to tsconfig.
71
- */
72
- get tsConfigPath() {
73
- return path_1.default.join(this._engine, 'tsconfig.json');
74
- }
75
- /**
76
- * Gets all optimzie decorators
77
- */
78
- getOptimizeDecorators() {
79
- return this._config.optimizeDecorators;
80
- }
81
- /**
82
- * Gets all features defined.
83
- */
84
- getFeatures() {
85
- return Object.keys(this._features);
86
- }
87
- /**
88
- * Returns if the specified feature is defined.
89
- * @param feature Feature ID.
90
- */
91
- hasFeature(feature) {
92
- return !!this._features[feature];
93
- }
94
- // TODO: it seems we don't need this interface for now.
95
- // public isNativeOnlyFeature (feature: string) {
96
- // return !!this._features[feature].isNativeOnly;
97
- // }
98
- /**
99
- * Gets all feature units included in specified features.
100
- * @param featureIds Feature ID.
101
- */
102
- getUnitsOfFeatures(featureIds) {
103
- var _a;
104
- const units = new Set();
105
- for (const featureId of featureIds) {
106
- (_a = this._features[featureId]) === null || _a === void 0 ? void 0 : _a.modules.forEach((entry) => units.add(entry));
107
- }
108
- return Array.from(units);
109
- }
110
- getIntrinsicFlagsOfFeatures(featureIds) {
111
- var _a;
112
- const flags = {};
113
- for (const featureId of featureIds) {
114
- const featureFlags = (_a = this._features[featureId]) === null || _a === void 0 ? void 0 : _a.intrinsicFlags;
115
- if (featureFlags) {
116
- Object.assign(flags, featureFlags);
117
- }
118
- }
119
- return flags;
120
- }
121
- /**
122
- * Gets all feature units in their names.
123
- */
124
- getFeatureUnits() {
125
- return Object.keys(this._featureUnits);
126
- }
127
- /**
128
- * Gets the path to source file of the feature unit.
129
- * @param moduleId Name of the feature unit.
130
- */
131
- getFeatureUnitFile(featureUnit) {
132
- return this._featureUnits[featureUnit];
133
- }
134
- /**
135
- * Gets all editor public modules in their names.
136
- */
137
- getEditorPublicModules() {
138
- return Object.keys(this._editorPublicModules);
139
- }
140
- /**
141
- * Gets the path to source file of the editor-public module.
142
- * @param moduleName Name of the public module.
143
- */
144
- getEditorPublicModuleFile(moduleName) {
145
- return this._editorPublicModules[moduleName];
146
- }
147
- /**
148
- * Gets the source of `'cc'`.
149
- * @param featureUnits Involved feature units.
150
- * @param mapper If exists, map the feature unit name into another module request.
151
- */
152
- evaluateIndexModuleSource(featureUnits, mapper) {
153
- return featureUnits.map((featureUnit) => {
154
- var _a, _b;
155
- const indexInfo = this._index.modules[featureUnit];
156
- const ns = indexInfo === null || indexInfo === void 0 ? void 0 : indexInfo.ns;
157
- if (ns) {
158
- return (0, dedent_1.default) `
159
- import * as ${ns} from '${(_a = mapper === null || mapper === void 0 ? void 0 : mapper(featureUnit)) !== null && _a !== void 0 ? _a : featureUnit}';
160
- export { ${ns} };
161
- `;
162
- }
163
- return `export * from '${(_b = mapper === null || mapper === void 0 ? void 0 : mapper(featureUnit)) !== null && _b !== void 0 ? _b : featureUnit}';`;
164
- }).join('\n');
165
- }
166
- /**
167
- * Evaluates the source of `'internal-constants'`(`'cc/env'`),
168
- * @param context
169
- */
170
- evaluateEnvModuleSourceFromRecord(record) {
171
- return Object.entries(record).map(([k, v]) => `export const ${k} = ${v};`).join('\n');
172
- }
173
- /**
174
- * Evaluates module overrides under specified context.
175
- * @param context
176
- */
177
- evaluateModuleOverrides(context) {
178
- var _a;
179
- const overrides = {};
180
- const addModuleOverrides = (moduleOverrides, isVirtualModule) => {
181
- for (let [source, override] of Object.entries(moduleOverrides)) {
182
- const normalizedSource = isVirtualModule ? source : path_1.default.resolve(this._engine, source);
183
- override = this._evalPathTemplate(override, context);
184
- const normalizedOverride = path_1.default.resolve(this._engine, override);
185
- overrides[normalizedSource] = normalizedOverride;
186
- }
187
- };
188
- (_a = this._config.moduleOverrides) === null || _a === void 0 ? void 0 : _a.forEach(({ test, overrides, isVirtualModule }) => {
189
- if (this._evalTest(test, context)) {
190
- addModuleOverrides(overrides, isVirtualModule);
191
- }
192
- });
193
- return overrides;
194
- }
195
- static _readModulesInDir(exportsDir, mapper) {
196
- return __awaiter(this, void 0, void 0, function* () {
197
- const result = {};
198
- for (const entryFileName of yield fs_extra_1.default.readdir(exportsDir)) {
199
- const entryExtName = path_1.default.extname(entryFileName);
200
- if (!entryExtName.toLowerCase().endsWith('.ts')) {
201
- continue;
202
- }
203
- const baseName = path_1.default.basename(entryFileName, entryExtName);
204
- const moduleName = mapper(baseName);
205
- const entryFile = path_1.default.join(exportsDir, entryFileName);
206
- result[moduleName] = entryFile;
207
- }
208
- return result;
209
- });
210
- }
211
- static _baseNameToFeatureUnitName(baseName) {
212
- return `${baseName}`;
213
- }
214
- static _editorBaseNameToModuleName(baseName) {
215
- return `cc/editor/${baseName}`;
216
- }
217
- constructor(engine, config) {
218
- this._index = { modules: {} };
219
- this._features = {};
220
- this._featureUnits = {};
221
- this._editorPublicModules = {};
222
- this._config = config;
223
- this._engine = engine;
224
- this.constantManager = new StatsQuery.ConstantManager(engine);
225
- }
226
- _evalTest(test, context) {
227
- // eslint-disable-next-line @typescript-eslint/no-implied-eval,no-new-func
228
- const result = new Function('context', `return ${test}`)(context);
229
- // console.debug(`Eval "${test}" to ${result}`);
230
- return result;
231
- }
232
- _evalPathTemplate(pathTemplate, context) {
233
- let resultPath = pathTemplate;
234
- const regExp = /\{\{(.*?)\}\}/g;
235
- let exeResult;
236
- while (exeResult = regExp.exec(pathTemplate)) {
237
- const templateItem = exeResult[0];
238
- const exp = exeResult[1];
239
- const evalResult = (new Function('context', `return ${exp}`)(context));
240
- resultPath = pathTemplate.replace(templateItem, evalResult);
241
- }
242
- return resultPath;
243
- }
244
- _initialize() {
245
- return __awaiter(this, void 0, void 0, function* () {
246
- const { _config: config, _engine: engine } = this;
247
- const featureUnits = this._featureUnits = yield StatsQuery._readModulesInDir(path_1.default.join(engine, 'exports'), StatsQuery._baseNameToFeatureUnitName);
248
- for (const [featureName, feature] of Object.entries(config.features)) {
249
- const parsedFeature = this._features[featureName] = { modules: [] };
250
- for (const moduleFileBaseName of feature.modules) {
251
- const featureUnitName = StatsQuery._baseNameToFeatureUnitName(moduleFileBaseName);
252
- if (!featureUnits[featureUnitName]) {
253
- throw new Error(`Invalid config file: '${moduleFileBaseName}' is not a valid module.`);
254
- }
255
- parsedFeature.modules.push(featureUnitName);
256
- }
257
- parsedFeature.intrinsicFlags = feature.intrinsicFlags;
258
- }
259
- if (config.index) {
260
- if (config.index.modules) {
261
- for (const [k, v] of Object.entries(config.index.modules)) {
262
- this._index.modules[StatsQuery._baseNameToFeatureUnitName(k)] = v;
263
- }
264
- }
265
- this._index = Object.assign(Object.assign({}, config.index), { modules: this._index.modules });
266
- }
267
- this._editorPublicModules = yield StatsQuery._readModulesInDir(path_1.default.join(engine, 'editor', 'exports'), StatsQuery._editorBaseNameToModuleName);
268
- });
269
- }
270
- }
271
- exports.StatsQuery = StatsQuery;
272
- (function (StatsQuery) {
273
- class ConstantManager {
274
- constructor(engineRoot) {
275
- this._engineRoot = engineRoot;
276
- }
277
- //#region export string
278
- exportDynamicConstants({ mode, platform, flags, }) {
279
- const config = this._getConfig();
280
- // init helper
281
- let result = '';
282
- if (this._hasCCGlobal(config)) {
283
- result += fs_extra_1.default.readFileSync(path_1.default.join(__dirname, '../../static/helper-global-exporter.txt'), 'utf8') + '\n';
284
- }
285
- if (this._hasDynamic(config)) {
286
- result += fs_extra_1.default.readFileSync(path_1.default.join(__dirname, '../../static/helper-dynamic-constants.txt'), 'utf8') + '\n';
287
- }
288
- // update value
289
- if (config[mode]) {
290
- config[mode].value = true;
291
- }
292
- else {
293
- console.warn(`Unknown mode: ${mode}`);
294
- }
295
- if (config[platform]) {
296
- config[platform].value = true;
297
- }
298
- else {
299
- console.warn(`Unknown platform: ${platform}`);
300
- }
301
- for (const key in flags) {
302
- const value = flags[key];
303
- if (config[key]) {
304
- config[key].value = value;
305
- }
306
- else {
307
- console.warn(`Unknown flag: ${key}`);
308
- }
309
- }
310
- // eval value
311
- for (const key in config) {
312
- const info = config[key];
313
- if (typeof info.value === 'string') {
314
- info.value = this._evalExpression(info.value, config);
315
- }
316
- }
317
- // generate export content
318
- for (const key in config) {
319
- const info = config[key];
320
- const value = info.value;
321
- if (info.dynamic) {
322
- continue;
323
- }
324
- result += `export const ${key} = ${value};\n`;
325
- if (info.ccGlobal) {
326
- result += `tryDefineGlobal('CC_${key}', ${value});\n`;
327
- }
328
- result += '\n';
329
- }
330
- return result;
331
- }
332
- genBuildTimeConstants(options) {
333
- const config = this._getConfig();
334
- this._applyOptionsToConfig(config, options);
335
- // generate json object
336
- const jsonObj = {};
337
- for (const key in config) {
338
- const info = config[key];
339
- jsonObj[key] = info.value;
340
- }
341
- return jsonObj;
342
- }
343
- genCCEnvConstants(options) {
344
- const config = this._getConfig();
345
- this._applyOptionsToConfig(config, options);
346
- // generate json object
347
- const jsonObj = {};
348
- for (const key in config) {
349
- const info = config[key];
350
- if (!info.internal) {
351
- jsonObj[key] = info.value;
352
- }
353
- }
354
- return jsonObj;
355
- }
356
- exportStaticConstants({ mode, platform, flags, }) {
357
- const config = this._getConfig();
358
- // init helper
359
- let result = '';
360
- if (this._hasCCGlobal(config)) {
361
- result += fs_extra_1.default.readFileSync(path_1.default.join(__dirname, '../../static/helper-global-exporter.txt'), 'utf8') + '\n';
362
- }
363
- // update value
364
- if (config[mode]) {
365
- config[mode].value = true;
366
- }
367
- else {
368
- console.warn(`Unknown mode: ${mode}`);
369
- }
370
- if (config[platform]) {
371
- config[platform].value = true;
372
- }
373
- else {
374
- console.warn(`Unknown platform: ${platform}`);
375
- }
376
- for (const key in flags) {
377
- const value = flags[key];
378
- if (config[key]) {
379
- config[key].value = value;
380
- }
381
- else {
382
- console.warn(`Unknown flag: ${key}`);
383
- }
384
- }
385
- // eval value
386
- for (const key in config) {
387
- const info = config[key];
388
- if (typeof info.value === 'string') {
389
- info.value = this._evalExpression(info.value, config);
390
- }
391
- }
392
- // generate export content
393
- for (const key in config) {
394
- const info = config[key];
395
- const value = info.value;
396
- let declarationKind = 'const';
397
- if (platform === 'OPEN_HARMONY' && key === 'WASM_SUPPORT_MODE') {
398
- declarationKind = 'let'; // HACK: on OH platform, we cannot compile successfully when declarationKind is const.
399
- }
400
- result += `export ${declarationKind} ${key} = ${value};\n`;
401
- if (info.ccGlobal) {
402
- result += `tryDefineGlobal('CC_${key}', ${value});\n`;
403
- }
404
- result += '\n';
405
- }
406
- return result;
407
- }
408
- //#endregion export string
409
- //#region declaration
410
- genInternalConstants() {
411
- const config = this._getConfig();
412
- let result = `declare module 'internal:constants'{\n`;
413
- for (const name in config) {
414
- const info = config[name];
415
- result += this._genConstantDeclaration(name, info);
416
- }
417
- result += '}\n';
418
- return result;
419
- }
420
- genCCEnv() {
421
- const config = this._getConfig();
422
- let result = `declare module 'cc/env'{\n`;
423
- for (const name in config) {
424
- const info = config[name];
425
- if (info.internal) {
426
- continue;
427
- }
428
- result += this._genConstantDeclaration(name, info);
429
- }
430
- result += '}\n';
431
- return result;
432
- }
433
- _genConstantDeclaration(name, info) {
434
- let result = '\t/**\n';
435
- let comments = info.comment.split('\n');
436
- for (const comment of comments) {
437
- result += `\t * ${comment}\n`;
438
- }
439
- result += '\t */\n';
440
- result += `\texport const ${name}: ${info.type};\n\n`;
441
- return result;
442
- }
443
- //#endregion declaration
444
- //#region utils
445
- _getConfig() {
446
- const engineConfig = fs_extra_1.default.readJsonSync(path_1.default.join(this._engineRoot, './cc.config.json').replace(/\\/g, '/'));
447
- const config = engineConfig.constants;
448
- // init default value
449
- for (const key in config) {
450
- const info = config[key];
451
- if (typeof info.ccGlobal === 'undefined') {
452
- info.ccGlobal = false;
453
- }
454
- if (typeof info.dynamic === 'undefined') {
455
- info.dynamic = false;
456
- }
457
- }
458
- return config;
459
- }
460
- _hasCCGlobal(config) {
461
- for (let key in config) {
462
- const info = config[key];
463
- if (info.ccGlobal) {
464
- return true;
465
- }
466
- }
467
- return false;
468
- }
469
- _hasDynamic(config) {
470
- for (let key in config) {
471
- const info = config[key];
472
- if (info.dynamic) {
473
- return true;
474
- }
475
- }
476
- return false;
477
- }
478
- _evalExpression(expression, config) {
479
- // eval sub expression
480
- const matchResult = expression.match(/(?<=\$)\w+/g);
481
- if (matchResult) {
482
- for (let name of matchResult) {
483
- const value = config[name].value;
484
- if (typeof value === 'string') {
485
- config[name].value = this._evalExpression(value, config);
486
- }
487
- }
488
- }
489
- // $EDITOR to $EDITOR.value
490
- expression = expression.replace(/(?<=\$)(\w+)/g, '$1.value');
491
- // $EDITOR to $.EDITOR.value
492
- expression = expression.replace(/\$/g, '$.');
493
- // do eval
494
- const evalFn = new Function('$', `return ${expression}`);
495
- return evalFn(config);
496
- }
497
- _applyOptionsToConfig(config, options) {
498
- const { mode, platform, flags } = options;
499
- // update value
500
- if (config[mode]) {
501
- config[mode].value = true;
502
- }
503
- else {
504
- console.warn(`Unknown mode: ${mode}`);
505
- }
506
- if (config[platform]) {
507
- config[platform].value = true;
508
- }
509
- else {
510
- console.warn(`Unknown platform: ${platform}`);
511
- }
512
- for (const key in flags) {
513
- const value = flags[key];
514
- if (config[key]) {
515
- config[key].value = value;
516
- }
517
- else {
518
- console.warn(`Unknown flag: ${key}`);
519
- }
520
- }
521
- // eval value
522
- for (const key in config) {
523
- const info = config[key];
524
- if (typeof info.value === 'string') {
525
- info.value = this._evalExpression(info.value, config);
526
- }
527
- }
528
- }
529
- }
530
- StatsQuery.ConstantManager = ConstantManager;
531
- })(StatsQuery = exports.StatsQuery || (exports.StatsQuery = {}));
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.StatsQuery = exports.ConfigInterface = void 0;
39
+ const path_1 = __importDefault(require("path"));
40
+ const fs_extra_1 = __importDefault(require("fs-extra"));
41
+ const json5_1 = __importDefault(require("json5"));
42
+ const dedent_1 = __importDefault(require("dedent"));
43
+ const ConfigInterface = __importStar(require("./config-interface"));
44
+ exports.ConfigInterface = ConfigInterface;
45
+ /**
46
+ * Query any any stats of the engine.
47
+ */
48
+ class StatsQuery {
49
+ /**
50
+ * @param engine Path to the engine root.
51
+ */
52
+ static create(engine) {
53
+ return __awaiter(this, void 0, void 0, function* () {
54
+ const configFile = path_1.default.join(engine, 'cc.config.json');
55
+ const config = json5_1.default.parse(yield fs_extra_1.default.readFile(configFile, 'utf8'));
56
+ // @ts-ignore
57
+ delete config['$schema'];
58
+ const query = new StatsQuery(engine, config);
59
+ yield query._initialize();
60
+ return query;
61
+ });
62
+ }
63
+ /**
64
+ * Gets the path to the engine root.
65
+ */
66
+ get path() {
67
+ return this._engine;
68
+ }
69
+ /**
70
+ * Gets the path to tsconfig.
71
+ */
72
+ get tsConfigPath() {
73
+ return path_1.default.join(this._engine, 'tsconfig.json');
74
+ }
75
+ /**
76
+ * Gets all optimzie decorators
77
+ */
78
+ getOptimizeDecorators() {
79
+ return this._config.optimizeDecorators;
80
+ }
81
+ /**
82
+ * Gets all features defined.
83
+ */
84
+ getFeatures() {
85
+ return Object.keys(this._features);
86
+ }
87
+ /**
88
+ * Returns if the specified feature is defined.
89
+ * @param feature Feature ID.
90
+ */
91
+ hasFeature(feature) {
92
+ return !!this._features[feature];
93
+ }
94
+ // TODO: it seems we don't need this interface for now.
95
+ // public isNativeOnlyFeature (feature: string) {
96
+ // return !!this._features[feature].isNativeOnly;
97
+ // }
98
+ /**
99
+ * Gets all feature units included in specified features.
100
+ * @param featureIds Feature ID.
101
+ */
102
+ getUnitsOfFeatures(featureIds) {
103
+ var _a;
104
+ const units = new Set();
105
+ for (const featureId of featureIds) {
106
+ (_a = this._features[featureId]) === null || _a === void 0 ? void 0 : _a.modules.forEach((entry) => units.add(entry));
107
+ }
108
+ return Array.from(units);
109
+ }
110
+ getIntrinsicFlagsOfFeatures(featureIds) {
111
+ var _a;
112
+ const flags = {};
113
+ for (const featureId of featureIds) {
114
+ const featureFlags = (_a = this._features[featureId]) === null || _a === void 0 ? void 0 : _a.intrinsicFlags;
115
+ if (featureFlags) {
116
+ Object.assign(flags, featureFlags);
117
+ }
118
+ }
119
+ return flags;
120
+ }
121
+ /**
122
+ * Gets all feature units in their names.
123
+ */
124
+ getFeatureUnits() {
125
+ return Object.keys(this._featureUnits);
126
+ }
127
+ /**
128
+ * Gets the path to source file of the feature unit.
129
+ * @param moduleId Name of the feature unit.
130
+ */
131
+ getFeatureUnitFile(featureUnit) {
132
+ return this._featureUnits[featureUnit];
133
+ }
134
+ /**
135
+ * Gets all editor public modules in their names.
136
+ */
137
+ getEditorPublicModules() {
138
+ return Object.keys(this._editorPublicModules);
139
+ }
140
+ /**
141
+ * Gets the path to source file of the editor-public module.
142
+ * @param moduleName Name of the public module.
143
+ */
144
+ getEditorPublicModuleFile(moduleName) {
145
+ return this._editorPublicModules[moduleName];
146
+ }
147
+ /**
148
+ * Gets the source of `'cc'`.
149
+ * @param featureUnits Involved feature units.
150
+ * @param mapper If exists, map the feature unit name into another module request.
151
+ */
152
+ evaluateIndexModuleSource(featureUnits, mapper) {
153
+ return featureUnits.map((featureUnit) => {
154
+ var _a, _b;
155
+ const indexInfo = this._index.modules[featureUnit];
156
+ const ns = indexInfo === null || indexInfo === void 0 ? void 0 : indexInfo.ns;
157
+ if (ns) {
158
+ return (0, dedent_1.default) `
159
+ import * as ${ns} from '${(_a = mapper === null || mapper === void 0 ? void 0 : mapper(featureUnit)) !== null && _a !== void 0 ? _a : featureUnit}';
160
+ export { ${ns} };
161
+ `;
162
+ }
163
+ return `export * from '${(_b = mapper === null || mapper === void 0 ? void 0 : mapper(featureUnit)) !== null && _b !== void 0 ? _b : featureUnit}';`;
164
+ }).join('\n');
165
+ }
166
+ /**
167
+ * Evaluates the source of `'internal-constants'`(`'cc/env'`),
168
+ * @param context
169
+ */
170
+ evaluateEnvModuleSourceFromRecord(record) {
171
+ return Object.entries(record).map(([k, v]) => `export const ${k} = ${v};`).join('\n');
172
+ }
173
+ /**
174
+ * Evaluates module overrides under specified context.
175
+ * @param context
176
+ */
177
+ evaluateModuleOverrides(context) {
178
+ var _a;
179
+ const overrides = {};
180
+ const addModuleOverrides = (moduleOverrides, isVirtualModule) => {
181
+ for (let [source, override] of Object.entries(moduleOverrides)) {
182
+ const normalizedSource = isVirtualModule ? source : path_1.default.resolve(this._engine, source);
183
+ override = this._evalPathTemplate(override, context);
184
+ const normalizedOverride = path_1.default.resolve(this._engine, override);
185
+ overrides[normalizedSource] = normalizedOverride;
186
+ }
187
+ };
188
+ (_a = this._config.moduleOverrides) === null || _a === void 0 ? void 0 : _a.forEach(({ test, overrides, isVirtualModule }) => {
189
+ if (this._evalTest(test, context)) {
190
+ addModuleOverrides(overrides, isVirtualModule);
191
+ }
192
+ });
193
+ return overrides;
194
+ }
195
+ static _readModulesInDir(exportsDir, mapper) {
196
+ return __awaiter(this, void 0, void 0, function* () {
197
+ const result = {};
198
+ for (const entryFileName of yield fs_extra_1.default.readdir(exportsDir)) {
199
+ const entryExtName = path_1.default.extname(entryFileName);
200
+ if (!entryExtName.toLowerCase().endsWith('.ts')) {
201
+ continue;
202
+ }
203
+ const baseName = path_1.default.basename(entryFileName, entryExtName);
204
+ const moduleName = mapper(baseName);
205
+ const entryFile = path_1.default.join(exportsDir, entryFileName);
206
+ result[moduleName] = entryFile;
207
+ }
208
+ return result;
209
+ });
210
+ }
211
+ static _baseNameToFeatureUnitName(baseName) {
212
+ return `${baseName}`;
213
+ }
214
+ static _editorBaseNameToModuleName(baseName) {
215
+ return `cc/editor/${baseName}`;
216
+ }
217
+ constructor(engine, config) {
218
+ this._index = { modules: {} };
219
+ this._features = {};
220
+ this._featureUnits = {};
221
+ this._editorPublicModules = {};
222
+ this._config = config;
223
+ this._engine = engine;
224
+ this.constantManager = new StatsQuery.ConstantManager(engine);
225
+ }
226
+ _evalTest(test, context) {
227
+ // eslint-disable-next-line @typescript-eslint/no-implied-eval,no-new-func
228
+ const result = new Function('context', `return ${test}`)(context);
229
+ // console.debug(`Eval "${test}" to ${result}`);
230
+ return result;
231
+ }
232
+ _evalPathTemplate(pathTemplate, context) {
233
+ let resultPath = pathTemplate;
234
+ const regExp = /\{\{(.*?)\}\}/g;
235
+ let exeResult;
236
+ while (exeResult = regExp.exec(pathTemplate)) {
237
+ const templateItem = exeResult[0];
238
+ const exp = exeResult[1];
239
+ const evalResult = (new Function('context', `return ${exp}`)(context));
240
+ resultPath = pathTemplate.replace(templateItem, evalResult);
241
+ }
242
+ return resultPath;
243
+ }
244
+ _initialize() {
245
+ return __awaiter(this, void 0, void 0, function* () {
246
+ const { _config: config, _engine: engine } = this;
247
+ const featureUnits = this._featureUnits = yield StatsQuery._readModulesInDir(path_1.default.join(engine, 'exports'), StatsQuery._baseNameToFeatureUnitName);
248
+ for (const [featureName, feature] of Object.entries(config.features)) {
249
+ const parsedFeature = this._features[featureName] = { modules: [] };
250
+ for (const moduleFileBaseName of feature.modules) {
251
+ const featureUnitName = StatsQuery._baseNameToFeatureUnitName(moduleFileBaseName);
252
+ if (!featureUnits[featureUnitName]) {
253
+ throw new Error(`Invalid config file: '${moduleFileBaseName}' is not a valid module.`);
254
+ }
255
+ parsedFeature.modules.push(featureUnitName);
256
+ }
257
+ parsedFeature.intrinsicFlags = feature.intrinsicFlags;
258
+ }
259
+ if (config.index) {
260
+ if (config.index.modules) {
261
+ for (const [k, v] of Object.entries(config.index.modules)) {
262
+ this._index.modules[StatsQuery._baseNameToFeatureUnitName(k)] = v;
263
+ }
264
+ }
265
+ this._index = Object.assign(Object.assign({}, config.index), { modules: this._index.modules });
266
+ }
267
+ this._editorPublicModules = yield StatsQuery._readModulesInDir(path_1.default.join(engine, 'editor', 'exports'), StatsQuery._editorBaseNameToModuleName);
268
+ });
269
+ }
270
+ }
271
+ exports.StatsQuery = StatsQuery;
272
+ (function (StatsQuery) {
273
+ class ConstantManager {
274
+ constructor(engineRoot) {
275
+ this._engineRoot = engineRoot;
276
+ }
277
+ //#region export string
278
+ exportDynamicConstants({ mode, platform, flags, }) {
279
+ const config = this._getConfig();
280
+ // init helper
281
+ let result = '';
282
+ if (this._hasCCGlobal(config)) {
283
+ result += fs_extra_1.default.readFileSync(path_1.default.join(__dirname, '../../static/helper-global-exporter.txt'), 'utf8') + '\n';
284
+ }
285
+ if (this._hasDynamic(config)) {
286
+ result += fs_extra_1.default.readFileSync(path_1.default.join(__dirname, '../../static/helper-dynamic-constants.txt'), 'utf8') + '\n';
287
+ }
288
+ // update value
289
+ if (config[mode]) {
290
+ config[mode].value = true;
291
+ }
292
+ else {
293
+ console.warn(`Unknown mode: ${mode}`);
294
+ }
295
+ if (config[platform]) {
296
+ config[platform].value = true;
297
+ }
298
+ else {
299
+ console.warn(`Unknown platform: ${platform}`);
300
+ }
301
+ for (const key in flags) {
302
+ const value = flags[key];
303
+ if (config[key]) {
304
+ config[key].value = value;
305
+ }
306
+ else {
307
+ console.warn(`Unknown flag: ${key}`);
308
+ }
309
+ }
310
+ // eval value
311
+ for (const key in config) {
312
+ const info = config[key];
313
+ if (typeof info.value === 'string') {
314
+ info.value = this._evalExpression(info.value, config);
315
+ }
316
+ }
317
+ // generate export content
318
+ for (const key in config) {
319
+ const info = config[key];
320
+ const value = info.value;
321
+ if (info.dynamic) {
322
+ continue;
323
+ }
324
+ result += `export const ${key} = ${value};\n`;
325
+ if (info.ccGlobal) {
326
+ result += `tryDefineGlobal('CC_${key}', ${value});\n`;
327
+ }
328
+ result += '\n';
329
+ }
330
+ return result;
331
+ }
332
+ genBuildTimeConstants(options) {
333
+ const config = this._getConfig();
334
+ this._applyOptionsToConfig(config, options);
335
+ // generate json object
336
+ const jsonObj = {};
337
+ for (const key in config) {
338
+ const info = config[key];
339
+ jsonObj[key] = info.value;
340
+ }
341
+ return jsonObj;
342
+ }
343
+ genCCEnvConstants(options) {
344
+ const config = this._getConfig();
345
+ this._applyOptionsToConfig(config, options);
346
+ // generate json object
347
+ const jsonObj = {};
348
+ for (const key in config) {
349
+ const info = config[key];
350
+ if (!info.internal) {
351
+ jsonObj[key] = info.value;
352
+ }
353
+ }
354
+ return jsonObj;
355
+ }
356
+ exportStaticConstants({ mode, platform, flags, }) {
357
+ const config = this._getConfig();
358
+ // init helper
359
+ let result = '';
360
+ if (this._hasCCGlobal(config)) {
361
+ result += fs_extra_1.default.readFileSync(path_1.default.join(__dirname, '../../static/helper-global-exporter.txt'), 'utf8') + '\n';
362
+ }
363
+ // update value
364
+ if (config[mode]) {
365
+ config[mode].value = true;
366
+ }
367
+ else {
368
+ console.warn(`Unknown mode: ${mode}`);
369
+ }
370
+ if (config[platform]) {
371
+ config[platform].value = true;
372
+ }
373
+ else {
374
+ console.warn(`Unknown platform: ${platform}`);
375
+ }
376
+ for (const key in flags) {
377
+ const value = flags[key];
378
+ if (config[key]) {
379
+ config[key].value = value;
380
+ }
381
+ else {
382
+ console.warn(`Unknown flag: ${key}`);
383
+ }
384
+ }
385
+ // eval value
386
+ for (const key in config) {
387
+ const info = config[key];
388
+ if (typeof info.value === 'string') {
389
+ info.value = this._evalExpression(info.value, config);
390
+ }
391
+ }
392
+ // generate export content
393
+ for (const key in config) {
394
+ const info = config[key];
395
+ const value = info.value;
396
+ let declarationKind = 'const';
397
+ if (platform === 'OPEN_HARMONY' && key === 'WASM_SUPPORT_MODE') {
398
+ declarationKind = 'let'; // HACK: on OH platform, we cannot compile successfully when declarationKind is const.
399
+ }
400
+ result += `export ${declarationKind} ${key} = ${value};\n`;
401
+ if (info.ccGlobal) {
402
+ result += `tryDefineGlobal('CC_${key}', ${value});\n`;
403
+ }
404
+ result += '\n';
405
+ }
406
+ return result;
407
+ }
408
+ //#endregion export string
409
+ //#region declaration
410
+ genInternalConstants() {
411
+ const config = this._getConfig();
412
+ let result = `declare module 'internal:constants'{\n`;
413
+ for (const name in config) {
414
+ const info = config[name];
415
+ result += this._genConstantDeclaration(name, info);
416
+ }
417
+ result += '}\n';
418
+ return result;
419
+ }
420
+ genCCEnv() {
421
+ const config = this._getConfig();
422
+ let result = `declare module 'cc/env'{\n`;
423
+ for (const name in config) {
424
+ const info = config[name];
425
+ if (info.internal) {
426
+ continue;
427
+ }
428
+ result += this._genConstantDeclaration(name, info);
429
+ }
430
+ result += '}\n';
431
+ return result;
432
+ }
433
+ _genConstantDeclaration(name, info) {
434
+ let result = '\t/**\n';
435
+ let comments = info.comment.split('\n');
436
+ for (const comment of comments) {
437
+ result += `\t * ${comment}\n`;
438
+ }
439
+ result += '\t */\n';
440
+ result += `\texport const ${name}: ${info.type};\n\n`;
441
+ return result;
442
+ }
443
+ //#endregion declaration
444
+ //#region utils
445
+ _getConfig() {
446
+ const engineConfig = fs_extra_1.default.readJsonSync(path_1.default.join(this._engineRoot, './cc.config.json').replace(/\\/g, '/'));
447
+ const config = engineConfig.constants;
448
+ // init default value
449
+ for (const key in config) {
450
+ const info = config[key];
451
+ if (typeof info.ccGlobal === 'undefined') {
452
+ info.ccGlobal = false;
453
+ }
454
+ if (typeof info.dynamic === 'undefined') {
455
+ info.dynamic = false;
456
+ }
457
+ }
458
+ return config;
459
+ }
460
+ _hasCCGlobal(config) {
461
+ for (let key in config) {
462
+ const info = config[key];
463
+ if (info.ccGlobal) {
464
+ return true;
465
+ }
466
+ }
467
+ return false;
468
+ }
469
+ _hasDynamic(config) {
470
+ for (let key in config) {
471
+ const info = config[key];
472
+ if (info.dynamic) {
473
+ return true;
474
+ }
475
+ }
476
+ return false;
477
+ }
478
+ _evalExpression(expression, config) {
479
+ // eval sub expression
480
+ const matchResult = expression.match(/(?<=\$)\w+/g);
481
+ if (matchResult) {
482
+ for (let name of matchResult) {
483
+ const value = config[name].value;
484
+ if (typeof value === 'string') {
485
+ config[name].value = this._evalExpression(value, config);
486
+ }
487
+ }
488
+ }
489
+ // $EDITOR to $EDITOR.value
490
+ expression = expression.replace(/(?<=\$)(\w+)/g, '$1.value');
491
+ // $EDITOR to $.EDITOR.value
492
+ expression = expression.replace(/\$/g, '$.');
493
+ // do eval
494
+ const evalFn = new Function('$', `return ${expression}`);
495
+ return evalFn(config);
496
+ }
497
+ _applyOptionsToConfig(config, options) {
498
+ const { mode, platform, flags } = options;
499
+ // update value
500
+ if (config[mode]) {
501
+ config[mode].value = true;
502
+ }
503
+ else {
504
+ console.warn(`Unknown mode: ${mode}`);
505
+ }
506
+ if (config[platform]) {
507
+ config[platform].value = true;
508
+ }
509
+ else {
510
+ console.warn(`Unknown platform: ${platform}`);
511
+ }
512
+ for (const key in flags) {
513
+ const value = flags[key];
514
+ if (config[key]) {
515
+ config[key].value = value;
516
+ }
517
+ else {
518
+ console.warn(`Unknown flag: ${key}`);
519
+ }
520
+ }
521
+ // eval value
522
+ for (const key in config) {
523
+ const info = config[key];
524
+ if (typeof info.value === 'string') {
525
+ info.value = this._evalExpression(info.value, config);
526
+ }
527
+ }
528
+ }
529
+ }
530
+ StatsQuery.ConstantManager = ConstantManager;
531
+ })(StatsQuery = exports.StatsQuery || (exports.StatsQuery = {}));
532
532
  //# sourceMappingURL=index.js.map