Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dumganhar committed Dec 26, 2024
1 parent c38d0ee commit 3397be3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 37 deletions.
6 changes: 0 additions & 6 deletions modules/build-engine/src/engine-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ export async function buildJsEngine(options: Required<buildEngine.Options>): Pro
}

const babelPlugins: babel.PluginItem[] = [];

if (!options.targets) {
babelPlugins.push([babelPluginTransformForOf, {
loose: true,
Expand Down Expand Up @@ -221,13 +220,11 @@ export async function buildJsEngine(options: Required<buildEngine.Options>): Pro
plugins: babelPlugins,
presets: [
[babelPresetEnv, presetEnvOptions],

[babelPresetCC, {
allowDeclareFields: true,
ccDecoratorHelpers: 'external',
fieldDecorators,
editorDecorators,
// onlyRemoveTypeImports: true,
} as babelPresetCC.Options],
],
};
Expand All @@ -239,10 +236,7 @@ export async function buildJsEngine(options: Required<buildEngine.Options>): Pro
babelOptions.presets?.push([(): babel.PluginItem => ({ plugins: [[recordDecorators]] })]);
}

// babelOptions.presets?.push([(): babel.PluginItem => ({ plugins: [[transformPrivateProperties]] })]);

const rollupPlugins: rollup.Plugin[] = [];

if (options.noDeprecatedFeatures) {
rollupPlugins.push(removeDeprecatedFeatures(
typeof options.noDeprecatedFeatures === 'string' ? options.noDeprecatedFeatures : undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import * as ts from '@cocos/typescript';

// decorators and modifiers-related api added in ts 4.8
interface BreakingTypeScriptApi {
canHaveDecorators(node: ts.Node): boolean;
getDecorators(node: ts.Node): readonly ts.Decorator[] | undefined;
canHaveModifiers(node: ts.Node): boolean;
getModifiers(node: ts.Node): readonly ts.Modifier[] | undefined;
canHaveDecorators(node: ts.Node): boolean;
getDecorators(node: ts.Node): readonly ts.Decorator[] | undefined;
canHaveModifiers(node: ts.Node): boolean;
getModifiers(node: ts.Node): readonly ts.Modifier[] | undefined;
}

interface JSDoc extends ts.Node {
Expand All @@ -23,10 +23,10 @@ interface JSDocContainer {
type SymbolWithParent = ts.Symbol & { parent?: ts.Symbol };

export interface IManglePropertiesOptions {
/**
* Prefix of generated names (e.g. '_ccprivate$')
*/
prefix: string;
/**
* Prefix of generated names (e.g. '_ccprivate$')
*/
prefix: string;
mangleList: string[];
dontMangleList: string[];
mangleGetterSetter: boolean;
Expand Down Expand Up @@ -89,13 +89,13 @@ export class PropertiesMinifier {
} else if (this.isConstructorParameterReference(node, program)) {
return this.createNewNode(program, node, this._context.factory.createIdentifier);
} else if (this.isPropertyInInterfaceNotShorthand(node.parent) && (
this.isIdentifierInVariableDeclaration(node, program) ||
this.isIdentifierInVariableDeclaration(node, program) ||
this.isIdentifierInBinaryExpression(node, program) ||
this.isIdentifierInArrayLiteralExpression(node, program))
) {
return this.createNewNode(program, node, this._context.factory.createIdentifier);
} else if (ts.isShorthandPropertyAssignment(node)) {
if (this.isIdentifierInVariableDeclaration(node.name, program) ||
if (this.isIdentifierInVariableDeclaration(node.name, program) ||
this.isIdentifierInBinaryExpression(node.name, program) ||
this.isIdentifierInArrayLiteralExpression(node.name, program)
) {
Expand Down Expand Up @@ -241,13 +241,13 @@ export class PropertiesMinifier {
}
return false;
}

private isPrivate(node: ClassMember | ts.ParameterDeclaration | InterfaceMember | ts.PropertyAssignment, parentSymbol: ts.Symbol | undefined): boolean {
let isPrivate = this.hasModifier(node, ts.SyntaxKind.PrivateKeyword);
if (!isPrivate && !this._options.ignoreJsDocTag) {
isPrivate = this.isMangledInJsDoc(node as JSDocContainer);
}

const parentName = (node.parent as any).name?.escapedText;

Check warning on line 251 in modules/build-engine/src/engine-js/ts-plugins/properties-minifier/index.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
if (!parentName) return isPrivate;

Expand All @@ -259,7 +259,7 @@ export class PropertiesMinifier {
} else {
isPrivate = this.updatePrivateByOptions(isPrivate, parentName, name);
}

return isPrivate;
}

Expand All @@ -275,7 +275,7 @@ export class PropertiesMinifier {
isPrivate = true;
}
}

// Check the dontMangleList option
if (isPrivate) {
if (this._options.dontMangleList.includes(fullName) || this._options.dontMangleList.includes(parentName)) {
Expand Down Expand Up @@ -338,40 +338,40 @@ export class PropertiesMinifier {
}
return undefined;
}

private hasModifier(node: ts.Node, modifier: ts.SyntaxKind): boolean {
return this.getModifiers(node).some((mod: ts.Modifier) => mod.kind === modifier);
}

private isAccessExpression(node: ts.Node): node is AccessExpression {
return ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node);
}

private isClassMember(node: ts.Node): node is ClassMember {
let ret = ts.isMethodDeclaration(node) || ts.isPropertyDeclaration(node);
if (!ret && this._options.mangleGetterSetter) {
ret = ts.isGetAccessor(node) || ts.isSetAccessor(node);
}
return ret;
}

private isInterfaceMember(node: ts.Node): node is InterfaceMember {
let ret = ts.isMethodSignature(node) || ts.isPropertySignature(node);
if (!ret && this._options.mangleGetterSetter) {
ret = ts.isGetAccessor(node) || ts.isSetAccessor(node);
}
return ret;
}

private isConstructorParameter(node: ts.Node): node is ts.ParameterDeclaration {
return ts.isParameter(node) && ts.isConstructorDeclaration(node.parent as ts.Node);
}

private isConstructorParameterReference(node: ts.Node, program: ts.Program): node is ts.Identifier {
if (!ts.isIdentifier(node)) {
return false;
}

const typeChecker = program.getTypeChecker();
const symbol = typeChecker.getSymbolAtLocation(node) as SymbolWithParent;
return this.isPrivateNonStaticClassMember(symbol);
Expand Down Expand Up @@ -453,7 +453,7 @@ export class PropertiesMinifier {
if (!ts.isIdentifier(node)) {
return false;
}

const parent = node.parent as PropertyInInterface;
if (!parent) {
return false;
Expand Down Expand Up @@ -486,7 +486,7 @@ export class PropertiesMinifier {
if (!ts.isIdentifier(node)) {
return false;
}

const parent = node.parent as PropertyInInterface;
if (!parent) {
return false;
Expand Down Expand Up @@ -524,7 +524,7 @@ export class PropertiesMinifier {
if (!ts.isIdentifier(node)) {
return false;
}

const parent = node.parent as PropertyInInterface;
if (!parent) {
return false;
Expand Down Expand Up @@ -555,7 +555,7 @@ export class PropertiesMinifier {
private isPropertyAssignment(node: ts.Declaration): node is ts.PropertyAssignment {
return ts.isPropertyAssignment(node);
}

private isPrivateNonStaticClassMember(symbol: SymbolWithParent | undefined): boolean {
// for some reason ts.Symbol.declarations can be undefined (for example in order to accessing to proto member)
if (symbol === undefined || symbol.declarations === undefined) {
Expand All @@ -569,31 +569,31 @@ export class PropertiesMinifier {
});
return ret;
}

private hasDecorators(node: ts.Node): boolean {
if (this.isBreakingTypeScriptApi(ts)) {
return ts.canHaveDecorators(node) && !!ts.getDecorators(node);
}

return !!node.decorators;
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
private getModifiers(node: ts.Node): readonly ts.Modifier[] {
if (this.isBreakingTypeScriptApi(ts)) {
if (!ts.canHaveModifiers(node)) {
return [];
}

return ts.getModifiers(node) || [];
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return node.modifiers || [];
}

private isBreakingTypeScriptApi(compiler: unknown): compiler is BreakingTypeScriptApi {
return 'canHaveDecorators' in ts;
}
Expand Down

0 comments on commit 3397be3

Please sign in to comment.