@cocos/ccbuild 2.0.0 → 2.0.2
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/CHANGELOG.md +16 -2
- package/modules/build-engine/lib/engine-ts/engine-builder.d.ts +2 -5
- package/modules/build-engine/lib/engine-ts/engine-builder.d.ts.map +1 -1
- package/modules/build-engine/lib/engine-ts/engine-builder.js +271 -271
- package/modules/build-engine/lib/engine-ts/engine-builder.js.map +1 -1
- package/modules/build-engine/lib/engine-ts/field-decorator-helper.d.ts +1 -1
- package/modules/build-engine/lib/engine-ts/field-decorator-helper.d.ts.map +1 -1
- package/modules/build-engine/lib/engine-ts/field-decorator-helper.js +6 -5
- package/modules/build-engine/lib/engine-ts/field-decorator-helper.js.map +1 -1
- package/modules/build-engine/lib/engine-ts/plugins/external-wasm-loader.d.ts +1 -0
- package/modules/build-engine/lib/engine-ts/plugins/external-wasm-loader.d.ts.map +1 -1
- package/modules/build-engine/lib/engine-ts/plugins/external-wasm-loader.js +141 -22
- package/modules/build-engine/lib/engine-ts/plugins/external-wasm-loader.js.map +1 -1
- package/modules/build-engine/lib/engine-ts/plugins/interface.d.ts +11 -7
- package/modules/build-engine/lib/engine-ts/plugins/interface.d.ts.map +1 -1
- package/modules/build-engine/lib/engine-ts/plugins/node-module-loader.d.ts +2 -0
- package/modules/build-engine/lib/engine-ts/plugins/node-module-loader.d.ts.map +1 -0
- package/modules/build-engine/lib/engine-ts/plugins/node-module-loader.js +91 -0
- package/modules/build-engine/lib/engine-ts/plugins/node-module-loader.js.map +1 -0
- package/modules/transformer/lib/babel/helper.d.ts +1 -0
- package/modules/transformer/lib/babel/helper.d.ts.map +1 -0
- package/modules/transformer/lib/babel/helper.js +25 -0
- package/modules/transformer/lib/babel/helper.js.map +1 -0
- package/modules/transformer/lib/babel/index.d.ts +2 -1
- package/modules/transformer/lib/babel/index.d.ts.map +1 -1
- package/modules/transformer/lib/babel/index.js +4 -2
- package/modules/transformer/lib/babel/index.js.map +1 -1
- package/package.json +2 -1
|
@@ -78,6 +78,7 @@ const dedent_1 = __importDefault(require("dedent"));
|
|
|
78
78
|
const glob_1 = require("glob");
|
|
79
79
|
const field_decorator_helper_1 = require("./field-decorator-helper");
|
|
80
80
|
const external_wasm_loader_1 = require("./plugins/external-wasm-loader");
|
|
81
|
+
const node_module_loader_1 = require("./plugins/node-module-loader");
|
|
81
82
|
var babel = transformer_1.babel.core;
|
|
82
83
|
var t = babel.types;
|
|
83
84
|
var traverse = babel.traverse;
|
|
@@ -88,14 +89,6 @@ class EngineBuilder {
|
|
|
88
89
|
this._entries = [];
|
|
89
90
|
this._entriesForPass2 = new Set();
|
|
90
91
|
this._virtual2code = {};
|
|
91
|
-
this._feature2NodeModule = {
|
|
92
|
-
'dragon-bones': '@cocos/dragonbones-js',
|
|
93
|
-
'physics-2d-box2d': '@cocos/box2d',
|
|
94
|
-
'physics-cannon': '@cocos/cannon',
|
|
95
|
-
'physics-physx': '@cocos/physx',
|
|
96
|
-
'physics-ammo': '@cocos/bullet'
|
|
97
|
-
};
|
|
98
|
-
this._nodeModules = [];
|
|
99
92
|
this._virtualOverrides = {};
|
|
100
93
|
this._buildResult = {};
|
|
101
94
|
this._resolveExtension = ['.ts', '.js', '.json']; // not an option
|
|
@@ -106,9 +99,9 @@ class EngineBuilder {
|
|
|
106
99
|
Path: 'PathAlias',
|
|
107
100
|
struct: 'structAlias'
|
|
108
101
|
};
|
|
109
|
-
this.
|
|
102
|
+
this._fieldDecoratorHelper = new field_decorator_helper_1.FieldDecoratorHelper();
|
|
110
103
|
this._plugins = [];
|
|
111
|
-
this._excludeTransform = [/external
|
|
104
|
+
this._excludeTransform = [/native\/external\//];
|
|
112
105
|
}
|
|
113
106
|
build(options) {
|
|
114
107
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -116,18 +109,23 @@ class EngineBuilder {
|
|
|
116
109
|
root
|
|
117
110
|
} = options;
|
|
118
111
|
this._buildResult = {};
|
|
119
|
-
const handleIdList = idList => {
|
|
112
|
+
const handleIdList = idList => __awaiter(this, void 0, void 0, function* () {
|
|
120
113
|
for (const id of idList) {
|
|
121
|
-
const handleResult = this._handleId(id);
|
|
114
|
+
const handleResult = yield this._handleId(id);
|
|
122
115
|
this._buildResult[handleResult.file] = handleResult;
|
|
123
116
|
}
|
|
124
|
-
};
|
|
117
|
+
});
|
|
118
|
+
this._initPlugins(options);
|
|
119
|
+
yield this._initOptions(options);
|
|
120
|
+
for (const plugin of this._plugins) {
|
|
121
|
+
if (plugin.buildStart) {
|
|
122
|
+
yield plugin.buildStart();
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
125
|
// pass1: build ts for native engine
|
|
126
126
|
console.log('[Build Engine]: pass1 - traverse and compile modules');
|
|
127
127
|
console.time('pass1');
|
|
128
|
-
this.
|
|
129
|
-
yield this._initOptions(options);
|
|
130
|
-
handleIdList(this._entries);
|
|
128
|
+
yield handleIdList(this._entries);
|
|
131
129
|
console.timeEnd('pass1');
|
|
132
130
|
// pass2: build web version for jsb type declarations
|
|
133
131
|
console.log('[Build Engine]: pass2 - apply jsb interface info');
|
|
@@ -157,15 +155,19 @@ class EngineBuilder {
|
|
|
157
155
|
console.timeEnd('pass3');
|
|
158
156
|
this._buildIndex();
|
|
159
157
|
yield this._copyTypes();
|
|
160
|
-
// this._addNodeModulesDeps(); // TODO: support node modules building
|
|
161
158
|
}
|
|
162
|
-
|
|
159
|
+
for (const plugin of this._plugins) {
|
|
160
|
+
if (plugin.buildEnd) {
|
|
161
|
+
yield plugin.buildEnd();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
163
164
|
return this._buildResult;
|
|
164
165
|
});
|
|
165
166
|
}
|
|
166
167
|
_initPlugins(options) {
|
|
167
|
-
this._plugins.push((0, external_wasm_loader_1.externalWasmLoaderFactory)({
|
|
168
|
-
engineRoot: options.root
|
|
168
|
+
this._plugins.push((0, node_module_loader_1.nodeModuleLoaderFactory)(), (0, external_wasm_loader_1.externalWasmLoaderFactory)({
|
|
169
|
+
engineRoot: options.root,
|
|
170
|
+
outDir: options.outDir
|
|
169
171
|
}));
|
|
170
172
|
}
|
|
171
173
|
_initOptions(options) {
|
|
@@ -183,10 +185,6 @@ class EngineBuilder {
|
|
|
183
185
|
const features = (_a = options.features) !== null && _a !== void 0 ? _a : statsQuery.getFeatures();
|
|
184
186
|
const featureUnits = statsQuery.getUnitsOfFeatures(features);
|
|
185
187
|
this._entries = featureUnits.map(fu => (0, utils_1.formatPath)(statsQuery.getFeatureUnitFile(fu)));
|
|
186
|
-
features.forEach(feature => {
|
|
187
|
-
const nodeModule = this._feature2NodeModule[feature];
|
|
188
|
-
nodeModule && this._nodeModules.push(nodeModule);
|
|
189
|
-
});
|
|
190
188
|
this._buildTimeConstants = constantManager.genBuildTimeConstants({
|
|
191
189
|
platform,
|
|
192
190
|
mode,
|
|
@@ -220,48 +218,47 @@ class EngineBuilder {
|
|
|
220
218
|
mode,
|
|
221
219
|
flags: flagConfig
|
|
222
220
|
});
|
|
223
|
-
this._virtual2code[this.
|
|
224
|
-
// TODO: resolve node modules
|
|
225
|
-
this._virtual2code['@cocos/box2d'] = 'export {}';
|
|
226
|
-
this._virtual2code['@cocos/bullet'] = 'export {}';
|
|
227
|
-
this._virtual2code['@cocos/cannon'] = 'export {}';
|
|
221
|
+
this._virtual2code[this._fieldDecoratorHelper.getModuleName()] = this._fieldDecoratorHelper.genModuleSource();
|
|
228
222
|
for (const virtualName in this._virtual2code) {
|
|
229
223
|
this._virtualOverrides[virtualName] = (0, utils_1.formatPath)(ps.join(root, '__virtual__', virtualName.replace(/:/g, '_'))) + '.ts';
|
|
230
224
|
}
|
|
231
225
|
});
|
|
232
226
|
}
|
|
233
227
|
_handleId(id, importer) {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
228
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
229
|
+
const resolvedId = yield this._resolve(id, importer);
|
|
230
|
+
if (typeof resolvedId === 'undefined') {
|
|
231
|
+
throw new Error(`Cannot resolve module id: ${id} ${importer ? `in file ${importer}` : ''}`);
|
|
232
|
+
}
|
|
233
|
+
const code = yield this._load(resolvedId);
|
|
234
|
+
if (typeof code === 'undefined') {
|
|
235
|
+
throw new Error(`Cannot load module: ${resolvedId} ${importer ? `in file ${importer}` : ''}`);
|
|
236
|
+
}
|
|
237
|
+
const overrideId = this._getOverrideId(id, importer);
|
|
238
|
+
// handle output file
|
|
239
|
+
let file = overrideId || resolvedId;
|
|
240
|
+
if (file.endsWith('.json')) {
|
|
241
|
+
file = file.slice(0, -5) + '.ts';
|
|
242
|
+
}
|
|
243
|
+
if (this._buildResult[file]) {
|
|
244
|
+
// skip cached file
|
|
245
|
+
return this._buildResult[file];
|
|
246
|
+
}
|
|
247
|
+
const depIdList = this._getDepIdList(file, code);
|
|
248
|
+
// we handle the dep module first, because we need to get the resolved dep id to transform the import specifier in current module.
|
|
249
|
+
for (const depId of depIdList) {
|
|
250
|
+
yield this._handleId(depId, file);
|
|
251
|
+
}
|
|
252
|
+
const transformResult = yield this._transform(resolvedId, code);
|
|
253
|
+
const handleResult = this._buildResult[file] = {
|
|
254
|
+
code: transformResult.code,
|
|
255
|
+
file,
|
|
256
|
+
originalId: id,
|
|
257
|
+
resolvedId,
|
|
258
|
+
map: transformResult.map
|
|
259
|
+
};
|
|
260
|
+
return handleResult;
|
|
263
261
|
});
|
|
264
|
-
return handleResult;
|
|
265
262
|
}
|
|
266
263
|
_getOverrideId(id, importer) {
|
|
267
264
|
var _a;
|
|
@@ -290,28 +287,28 @@ class EngineBuilder {
|
|
|
290
287
|
}
|
|
291
288
|
_resolve(id, importer) {
|
|
292
289
|
var _a, _b;
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
290
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
291
|
+
for (const p of this._plugins) {
|
|
292
|
+
const resolvedId = yield (_a = p.resolve) === null || _a === void 0 ? void 0 : _a.call(p, id, importer);
|
|
293
|
+
if (resolvedId) {
|
|
294
|
+
return resolvedId;
|
|
295
|
+
}
|
|
297
296
|
}
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
if (resolved) {
|
|
312
|
-
return (_b = this._moduleOverrides[resolved]) !== null && _b !== void 0 ? _b : resolved;
|
|
297
|
+
if (!importer) {
|
|
298
|
+
return id; // entry
|
|
299
|
+
} else if (id in this._virtualOverrides) {
|
|
300
|
+
return id; // virtual module does not have real fs path
|
|
301
|
+
} else if (id in this._moduleOverrides) {
|
|
302
|
+
return this._moduleOverrides[id];
|
|
303
|
+
} else if (ps.isAbsolute(id)) {
|
|
304
|
+
return id;
|
|
305
|
+
} else {
|
|
306
|
+
const resolved = this._resolveRelative(id, importer);
|
|
307
|
+
if (resolved) {
|
|
308
|
+
return (_b = this._moduleOverrides[resolved]) !== null && _b !== void 0 ? _b : resolved;
|
|
309
|
+
}
|
|
313
310
|
}
|
|
314
|
-
}
|
|
311
|
+
});
|
|
315
312
|
}
|
|
316
313
|
_resolveRelative(id, importer) {
|
|
317
314
|
const file = (0, utils_1.formatPath)(ps.join(ps.dirname(importer), id));
|
|
@@ -331,33 +328,32 @@ class EngineBuilder {
|
|
|
331
328
|
}
|
|
332
329
|
_load(id) {
|
|
333
330
|
var _a;
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
331
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
332
|
+
for (const p of this._plugins) {
|
|
333
|
+
const loadedCode = yield (_a = p.load) === null || _a === void 0 ? void 0 : _a.call(p, id);
|
|
334
|
+
if (loadedCode) {
|
|
335
|
+
return loadedCode;
|
|
336
|
+
}
|
|
338
337
|
}
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
338
|
+
if (fs.existsSync(id)) {
|
|
339
|
+
let code = fs.readFileSync(id, 'utf8');
|
|
340
|
+
if (id.endsWith('.json')) {
|
|
341
|
+
code = `export default ${code};`;
|
|
342
|
+
}
|
|
343
|
+
return code;
|
|
344
|
+
} else if (this._virtualOverrides[id]) {
|
|
345
|
+
return this._virtual2code[id];
|
|
344
346
|
}
|
|
345
|
-
|
|
346
|
-
} else if (this._virtualOverrides[id]) {
|
|
347
|
-
return this._virtual2code[id];
|
|
348
|
-
}
|
|
347
|
+
});
|
|
349
348
|
}
|
|
350
|
-
|
|
351
|
-
|
|
349
|
+
_getDepIdList(file, code) {
|
|
350
|
+
const depIdList = [];
|
|
352
351
|
for (const ex of this._excludeTransform) {
|
|
353
352
|
if (ex.test(file)) {
|
|
354
|
-
return
|
|
355
|
-
code,
|
|
356
|
-
depIdList: []
|
|
357
|
-
};
|
|
353
|
+
return depIdList;
|
|
358
354
|
}
|
|
359
355
|
}
|
|
360
|
-
|
|
356
|
+
// eg. zlib.js dependent on zlib.d.ts
|
|
361
357
|
if (ps.extname(file) === '.js') {
|
|
362
358
|
const dtsFile = (0, utils_1.toExtensionLess)(file) + '.d.ts';
|
|
363
359
|
if (fs.existsSync(dtsFile)) {
|
|
@@ -371,171 +367,203 @@ class EngineBuilder {
|
|
|
371
367
|
if (source) {
|
|
372
368
|
const specifier = source.value;
|
|
373
369
|
// add dependency
|
|
374
|
-
|
|
375
|
-
// don't load node modules, we post install the modules in OH project
|
|
376
|
-
depIdList.push(specifier);
|
|
377
|
-
}
|
|
378
|
-
// transform import/export declaration if needed
|
|
379
|
-
const overrideId = this._getOverrideId(specifier, file);
|
|
380
|
-
if (overrideId) {
|
|
381
|
-
let relativePath = (0, utils_1.formatPath)(ps.relative(ps.dirname(file), overrideId));
|
|
382
|
-
if (!relativePath.startsWith('.')) {
|
|
383
|
-
relativePath = './' + relativePath;
|
|
384
|
-
}
|
|
385
|
-
if (ps.extname(relativePath) === '.ts') {
|
|
386
|
-
relativePath = relativePath.slice(0, -3); // remove '.ts'
|
|
387
|
-
}
|
|
388
|
-
// traverse to transform specifier
|
|
389
|
-
traverse(path.node, {
|
|
390
|
-
StringLiteral(path) {
|
|
391
|
-
path.replaceWith(babel.types.stringLiteral(relativePath));
|
|
392
|
-
path.skip();
|
|
393
|
-
}
|
|
394
|
-
}, path.scope);
|
|
395
|
-
}
|
|
370
|
+
depIdList.push(specifier);
|
|
396
371
|
}
|
|
397
|
-
const importExportSpecifier = path => {
|
|
398
|
-
const name = path.node.local.name;
|
|
399
|
-
const alias = this._renameMap[name];
|
|
400
|
-
if (alias) {
|
|
401
|
-
path.replaceWith(babel.types.exportSpecifier(babel.types.identifier(alias), babel.types.identifier(alias)));
|
|
402
|
-
}
|
|
403
|
-
};
|
|
404
|
-
path.traverse({
|
|
405
|
-
ExportSpecifier: importExportSpecifier,
|
|
406
|
-
ImportSpecifier: importExportSpecifier
|
|
407
|
-
});
|
|
408
372
|
};
|
|
409
|
-
|
|
410
|
-
const self = this;
|
|
411
|
-
const transformResult = babel.transformSync(code, {
|
|
373
|
+
const res = babel.parseSync(code, {
|
|
412
374
|
configFile: false,
|
|
413
375
|
plugins: [[pluginSyntaxTS], [syntaxDecorators, {
|
|
414
376
|
version: '2018-09',
|
|
415
377
|
decoratorsBeforeExport: true
|
|
416
|
-
}]
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
378
|
+
}]]
|
|
379
|
+
});
|
|
380
|
+
babel.traverse(res, {
|
|
381
|
+
ImportDeclaration: importExportVisitor,
|
|
382
|
+
ExportDeclaration: importExportVisitor
|
|
383
|
+
});
|
|
384
|
+
return depIdList;
|
|
385
|
+
}
|
|
386
|
+
_transform(file, code) {
|
|
387
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
388
|
+
file = (0, utils_1.formatPath)(file);
|
|
389
|
+
for (const ex of this._excludeTransform) {
|
|
390
|
+
if (ex.test(file)) {
|
|
391
|
+
return {
|
|
392
|
+
code
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
let needFieldHelperModule = false;
|
|
397
|
+
const importExportVisitor = path => {
|
|
398
|
+
// @ts-expect-error TODO: fix type
|
|
399
|
+
const source = path.node.source;
|
|
400
|
+
if (source) {
|
|
401
|
+
const specifier = source.value;
|
|
402
|
+
// transform import/export declaration if needed
|
|
403
|
+
const overrideId = this._getOverrideId(specifier, file);
|
|
404
|
+
if (overrideId) {
|
|
405
|
+
let relativePath = (0, utils_1.formatPath)(ps.relative(ps.dirname(file), overrideId));
|
|
406
|
+
if (!relativePath.startsWith('.')) {
|
|
407
|
+
relativePath = './' + relativePath;
|
|
408
|
+
}
|
|
409
|
+
if (ps.extname(relativePath) === '.ts') {
|
|
410
|
+
relativePath = relativePath.slice(0, -3); // remove '.ts'
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
traverse(path.node, {
|
|
414
|
+
StringLiteral(path) {
|
|
415
|
+
path.replaceWith(babel.types.stringLiteral(relativePath));
|
|
416
|
+
path.skip();
|
|
441
417
|
}
|
|
442
|
-
});
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
418
|
+
}, path.scope);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
const importExportSpecifier = path => {
|
|
422
|
+
const name = path.node.local.name;
|
|
423
|
+
const alias = this._renameMap[name];
|
|
424
|
+
if (alias) {
|
|
425
|
+
path.replaceWith(babel.types.exportSpecifier(babel.types.identifier(alias), babel.types.identifier(alias)));
|
|
426
|
+
}
|
|
427
|
+
};
|
|
428
|
+
path.traverse({
|
|
429
|
+
ExportSpecifier: importExportSpecifier,
|
|
430
|
+
ImportSpecifier: importExportSpecifier
|
|
431
|
+
});
|
|
432
|
+
};
|
|
433
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
434
|
+
const self = this;
|
|
435
|
+
const transformResult = babel.transformSync(code, {
|
|
436
|
+
configFile: false,
|
|
437
|
+
plugins: [[pluginSyntaxTS], [syntaxDecorators, {
|
|
438
|
+
version: '2018-09',
|
|
439
|
+
decoratorsBeforeExport: true
|
|
440
|
+
}], [() => {
|
|
441
|
+
return {
|
|
442
|
+
name: 'custom-transform',
|
|
443
|
+
pre(file) {
|
|
444
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
445
|
+
const pluginPass = this;
|
|
446
|
+
traverse(file.ast, {
|
|
447
|
+
ClassProperty(path) {
|
|
448
|
+
const decoratorsPath = path.get('decorators');
|
|
449
|
+
if (Array.isArray(decoratorsPath)) {
|
|
450
|
+
const propertyValuePath = path.get('value');
|
|
451
|
+
const helperIdentifier = self._fieldDecoratorHelper.addHelper(pluginPass.file);
|
|
452
|
+
needFieldHelperModule = true;
|
|
453
|
+
decoratorsPath.forEach(decPath => {
|
|
454
|
+
const expPath = decPath.get('expression');
|
|
455
|
+
const type = expPath.node.type;
|
|
456
|
+
if (type === 'CallExpression') {
|
|
457
|
+
const decName = expPath.node.callee.name;
|
|
458
|
+
const args = expPath.node.arguments;
|
|
459
|
+
decPath.replaceWith(t.decorator(t.callExpression(helperIdentifier, [t.identifier(decName), propertyValuePath.node ? t.arrowFunctionExpression([], propertyValuePath.node) : t.nullLiteral(), ...args])));
|
|
460
|
+
} else if (type === 'Identifier') {
|
|
461
|
+
const decName = expPath.node.name;
|
|
462
|
+
decPath.replaceWith(t.decorator(t.callExpression(helperIdentifier, [t.identifier(decName), propertyValuePath.node ? t.arrowFunctionExpression([], propertyValuePath.node) : t.nullLiteral()])));
|
|
463
|
+
}
|
|
464
|
+
});
|
|
465
|
+
}
|
|
464
466
|
}
|
|
465
|
-
}
|
|
466
|
-
// TODO: for now, we transform `import('./xxx/xxx.js')` into `window.__cc_module_context__.import('./xxx/xxx.js')`
|
|
467
|
-
// we need to support import(`project://xxx`) in the future.
|
|
468
|
-
path.replaceWith(t.callExpression(t.memberExpression(t.memberExpression(t.identifier('window'), t.identifier('__cc_module_context__')), t.identifier('import')), path.node.arguments));
|
|
469
|
-
}
|
|
470
|
-
},
|
|
471
|
-
ClassDeclaration(path) {
|
|
472
|
-
const idPath = path.get('id');
|
|
473
|
-
const name = idPath.node.name;
|
|
474
|
-
const alias = self._renameMap[name];
|
|
475
|
-
if (typeof alias === 'string') {
|
|
476
|
-
idPath.replaceWith(t.identifier(alias));
|
|
477
|
-
}
|
|
467
|
+
});
|
|
478
468
|
},
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
469
|
+
visitor: {
|
|
470
|
+
ImportDeclaration: importExportVisitor,
|
|
471
|
+
ExportDeclaration: importExportVisitor,
|
|
472
|
+
// TODO: here we rename class Rect and Path
|
|
473
|
+
CallExpression(path) {
|
|
474
|
+
if (path.node.callee.type === 'MemberExpression') {
|
|
475
|
+
const memberExpressionPath = path.get('callee');
|
|
476
|
+
const objectPath = memberExpressionPath.get('object');
|
|
477
|
+
const name = objectPath.node.name;
|
|
478
|
+
const alias = self._renameMap[name];
|
|
479
|
+
if (typeof alias === 'string' && path.node.callee.object.type === 'Identifier') {
|
|
480
|
+
objectPath.replaceWith(t.identifier(alias));
|
|
481
|
+
}
|
|
482
|
+
// TODO: for now, OH doesn't support standard console interface,
|
|
483
|
+
// so we need to ignore the type checking for console call expressions.
|
|
484
|
+
else if (name === 'console') {
|
|
485
|
+
path.node.leadingComments = [{
|
|
486
|
+
type: 'CommentLine',
|
|
487
|
+
value: ' @ts-ignore'
|
|
488
|
+
}];
|
|
489
|
+
}
|
|
490
|
+
} else if (path.node.callee.type === 'Import') {
|
|
491
|
+
// TODO: for now, we transform `import('./xxx/xxx.js')` into `window.__cc_module_context__.import('./xxx/xxx.js')`
|
|
492
|
+
// we need to support import(`project://xxx`) in the future.
|
|
493
|
+
path.replaceWith(t.callExpression(t.memberExpression(t.memberExpression(t.identifier('window'), t.identifier('__cc_module_context__')), t.identifier('import')), path.node.arguments));
|
|
487
494
|
}
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
const typeName = path.node.typeAnnotation.typeName;
|
|
493
|
-
const childPath = path.get('typeAnnotation');
|
|
494
|
-
if (typeName) {
|
|
495
|
-
const name = typeName.name;
|
|
495
|
+
},
|
|
496
|
+
ClassDeclaration(path) {
|
|
497
|
+
const idPath = path.get('id');
|
|
498
|
+
const name = idPath.node.name;
|
|
496
499
|
const alias = self._renameMap[name];
|
|
497
500
|
if (typeof alias === 'string') {
|
|
498
|
-
|
|
499
|
-
type: 'TSExpressionWithTypeArguments',
|
|
500
|
-
expression: t.identifier(alias)
|
|
501
|
-
}));
|
|
501
|
+
idPath.replaceWith(t.identifier(alias));
|
|
502
502
|
}
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
503
|
+
},
|
|
504
|
+
NewExpression(path) {
|
|
505
|
+
const calleePath = path.get('callee');
|
|
506
|
+
// @ts-expect-error TODO: fix type
|
|
507
|
+
const name = calleePath.node.name;
|
|
508
|
+
if (name) {
|
|
509
|
+
const alias = self._renameMap[name];
|
|
510
|
+
if (typeof alias === 'string') {
|
|
511
|
+
calleePath.replaceWith(t.identifier(alias));
|
|
512
|
+
}
|
|
507
513
|
}
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
514
|
+
},
|
|
515
|
+
TSTypeAnnotation(path) {
|
|
516
|
+
// @ts-expect-error TODO: fix type
|
|
517
|
+
const typeName = path.node.typeAnnotation.typeName;
|
|
518
|
+
const childPath = path.get('typeAnnotation');
|
|
519
|
+
if (typeName) {
|
|
520
|
+
const name = typeName.name;
|
|
521
|
+
const alias = self._renameMap[name];
|
|
522
|
+
if (typeof alias === 'string') {
|
|
523
|
+
path.replaceWith(t.tsTypeAnnotation({
|
|
524
|
+
type: 'TSExpressionWithTypeArguments',
|
|
525
|
+
expression: t.identifier(alias)
|
|
526
|
+
}));
|
|
517
527
|
}
|
|
518
|
-
} else if (
|
|
519
|
-
|
|
520
|
-
|
|
528
|
+
} else if (childPath.type === 'TSLiteralType') {
|
|
529
|
+
const literalPath = childPath.get('literal');
|
|
530
|
+
if (literalPath.type === 'TemplateLiteral') {
|
|
531
|
+
path.replaceWith(t.tsTypeAnnotation(t.tsStringKeyword()));
|
|
521
532
|
}
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
533
|
+
}
|
|
534
|
+
},
|
|
535
|
+
Identifier(path) {
|
|
536
|
+
const name = path.node.name;
|
|
537
|
+
const alias = self._renameMap[name];
|
|
538
|
+
if (typeof alias === 'string') {
|
|
539
|
+
if (path.parent.type === 'ObjectProperty' || path.parent.type === 'TSPropertySignature') {
|
|
540
|
+
if (path.parent.key !== path.node) {
|
|
541
|
+
path.replaceWith(t.identifier(alias));
|
|
542
|
+
}
|
|
543
|
+
} else if (path.parent.type === 'MemberExpression' || path.parent.type === 'OptionalMemberExpression') {
|
|
544
|
+
if (path.parent.property !== path.node) {
|
|
545
|
+
path.replaceWith(t.identifier(alias));
|
|
546
|
+
}
|
|
547
|
+
} else if (!(path.parent.type === 'ClassMethod' && (path.parent.kind === 'get' || path.parent.kind === 'set' || path.parent.key === path.node)) && path.parent.type !== 'ClassProperty') {
|
|
548
|
+
const newIdentifier = t.identifier(alias);
|
|
549
|
+
if (path.node.typeAnnotation) {
|
|
550
|
+
newIdentifier.typeAnnotation = path.node.typeAnnotation;
|
|
551
|
+
}
|
|
552
|
+
path.replaceWith(newIdentifier);
|
|
526
553
|
}
|
|
527
|
-
path.replaceWith(newIdentifier);
|
|
528
554
|
}
|
|
529
555
|
}
|
|
530
556
|
}
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
}
|
|
557
|
+
};
|
|
558
|
+
}]]
|
|
559
|
+
});
|
|
560
|
+
if (needFieldHelperModule) {
|
|
561
|
+
yield this._handleId(this._fieldDecoratorHelper.getModuleName(), file);
|
|
562
|
+
}
|
|
563
|
+
return {
|
|
564
|
+
code: transformResult === null || transformResult === void 0 ? void 0 : transformResult.code
|
|
565
|
+
};
|
|
534
566
|
});
|
|
535
|
-
return {
|
|
536
|
-
code: transformResult === null || transformResult === void 0 ? void 0 : transformResult.code,
|
|
537
|
-
depIdList
|
|
538
|
-
};
|
|
539
567
|
}
|
|
540
568
|
_lintImport(lintFiles, verbose = false) {
|
|
541
569
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -618,33 +646,5 @@ class EngineBuilder {
|
|
|
618
646
|
fs.outputFileSync(targetDomDts, code, 'utf8');
|
|
619
647
|
});
|
|
620
648
|
}
|
|
621
|
-
_addNodeModulesDeps() {
|
|
622
|
-
const {
|
|
623
|
-
outDir
|
|
624
|
-
} = this._options;
|
|
625
|
-
if (!outDir) {
|
|
626
|
-
return;
|
|
627
|
-
}
|
|
628
|
-
const pkgFile = (0, utils_1.formatPath)(ps.join(outDir, '..',
|
|
629
|
-
// src
|
|
630
|
-
'..',
|
|
631
|
-
// cocos
|
|
632
|
-
'..',
|
|
633
|
-
// ets
|
|
634
|
-
'..',
|
|
635
|
-
// main
|
|
636
|
-
'..',
|
|
637
|
-
// src
|
|
638
|
-
'..',
|
|
639
|
-
// entry
|
|
640
|
-
'package.json'));
|
|
641
|
-
if (!fs.existsSync(pkgFile)) {
|
|
642
|
-
return;
|
|
643
|
-
}
|
|
644
|
-
const jsonObj = fs.readJSONSync(pkgFile);
|
|
645
|
-
console.log(jsonObj);
|
|
646
|
-
// TODO
|
|
647
|
-
}
|
|
648
649
|
}
|
|
649
|
-
|
|
650
650
|
exports.EngineBuilder = EngineBuilder;
|