Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v3.8.6] Support mangling private properties. #74

Merged
merged 38 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
49c2152
[v3.8.6] Support mangling private properties.
dumganhar Dec 19, 2024
0b4e122
Use ts transformer to transform ENUM.
dumganhar Dec 20, 2024
8240073
Update
dumganhar Dec 20, 2024
312d10e
Support BYTES_PER_ELEMENT for ts transformer.
dumganhar Dec 23, 2024
90f0133
Support to mangle properties with @mangle jsdoc comment
dumganhar Dec 23, 2024
5e7c1d0
Update test case.
dumganhar Dec 23, 2024
b95f1ff
Support mangleList and dontMangleList
dumganhar Dec 23, 2024
07879f4
Update test case.
dumganhar Dec 23, 2024
edfd416
Support mangleList and dontMangleList 2
dumganhar Dec 23, 2024
1828318
update test.
dumganhar Dec 23, 2024
b60a01a
Update snapshot
dumganhar Dec 24, 2024
e516bcb
Update snapshot
dumganhar Dec 24, 2024
af051bb
Update test.
dumganhar Dec 24, 2024
7693bf5
Support parent mangle config.
dumganhar Dec 24, 2024
4175c0d
inline enum ts transform should be first.
dumganhar Dec 24, 2024
24545e0
update
dumganhar Dec 24, 2024
ac2b71f
Refactor a little.
dumganhar Dec 24, 2024
a16e537
update snapshot.
dumganhar Dec 24, 2024
997ba7a
support mangleGetterSetter.
dumganhar Dec 24, 2024
0f014f5
update snapshot.
dumganhar Dec 24, 2024
b3afa17
dont/mangleList supports the whole class or interface.
dumganhar Dec 24, 2024
db4372c
update test case.
dumganhar Dec 24, 2024
6030c15
Update test
dumganhar Dec 24, 2024
e1548c6
Scan Enum each time
dumganhar Dec 24, 2024
3fff90a
Disable sourcemap for ts transformer.
dumganhar Dec 25, 2024
89941f7
Support interface binary expression mangle.
dumganhar Dec 25, 2024
a2d14c7
update snapshot.
dumganhar Dec 25, 2024
df181ad
fix shorthand.
dumganhar Dec 25, 2024
176548f
update snapshot.
dumganhar Dec 25, 2024
3932f92
add ignoreJsDocTag option
dumganhar Dec 25, 2024
7358392
Update test and snapshot. Refactor properties-minifiler a little.
dumganhar Dec 26, 2024
2026bf1
Update package name.
dumganhar Dec 26, 2024
d168d6b
Cleanup
dumganhar Dec 26, 2024
7e43f83
update snapshot.
dumganhar Dec 26, 2024
c38d0ee
Update @cocos/typescript version.
dumganhar Dec 26, 2024
3397be3
cleanup
dumganhar Dec 26, 2024
69fa6da
fix this.
dumganhar Dec 26, 2024
d203fb7
Change version to 2.3.0
dumganhar Dec 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
467 changes: 257 additions & 210 deletions .api/public.d.ts

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions modules/build-engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
"@babel/parser": "^7.20.13",
"@babel/traverse": "^7.20.13",

"magic-string": "^0.30.10",
"@rollup/plugin-replace": "5.0.7",
"@rollup/pluginutils": "5.1.0",
"@cocos/rollup-plugin-typescript": "^12.1.2-cocos.2",
"@cocos/typescript": "^4.9.5-cocos.3",
"ast-kit": "1.0.0",
"fast-glob": "3.3.2",

Expand All @@ -55,6 +55,6 @@
"@types/glob": "~7.2.0"
},
"optionalDependencies": {
"rollup-plugin-visualizer": "5.9.2"
"rollup-plugin-visualizer": "^5.12.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
/// type definitions varies by versions
// import * as t from '@babel/types';
declare namespace t {
type Identifier = any;

Check warning on line 18 in modules/build-engine/src/engine-js/babel-plugins/decorator-parser.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
type Import = any;

Check warning on line 19 in modules/build-engine/src/engine-js/babel-plugins/decorator-parser.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
type StringLiteral = any;

Check warning on line 20 in modules/build-engine/src/engine-js/babel-plugins/decorator-parser.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
type SequenceExpression = any;

Check warning on line 21 in modules/build-engine/src/engine-js/babel-plugins/decorator-parser.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
type ClassDeclaration = any;

Check warning on line 22 in modules/build-engine/src/engine-js/babel-plugins/decorator-parser.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
type ObjectExpression = any;
type ObjectMethod = any;
type ObjectProperty = any;
Expand Down Expand Up @@ -317,7 +317,7 @@
};
}

export default function recordDecorators(): babel.PluginObj<any> {
export function recordDecorators(): babel.PluginObj<any> {
return {
name: 'decorator-collector',
visitor: {
Expand Down
55 changes: 45 additions & 10 deletions modules/build-engine/src/engine-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import { externalWasmLoader } from './rollup-plugins/external-wasm-loader';
import { StatsQuery } from '@ccbuild/stats-query';
import { filePathToModuleRequest, formatPath } from '@ccbuild/utils';
import { rpNamedChunk } from './rollup-plugins/systemjs-named-register-plugin';
import { rpInlineEnum } from './rollup-plugins/inline-enum';
import { getEnumData, rpEnumScanner } from './rollup-plugins/enum-scanner';
import rpTypescript from '@cocos/rollup-plugin-typescript';
import { minifyPrivatePropertiesTransformer } from './ts-plugins/properties-minifier';
import { inlineEnumTransformer } from './ts-plugins/inline-enum';

// import babel
import babel = Transformer.core;
Expand All @@ -35,7 +38,8 @@ import rpVirtual = Bundler.plugins.virtual;
import { ModuleQuery } from '@ccbuild/modularize';
// import rpProgress = Bundler.plugins.progress;

import * as decoratorRecorder from './babel-plugins/decorator-parser';
import { recordDecorators } from './babel-plugins/decorator-parser';
import ts from '@cocos/typescript';

const realPath = (function (): (file: string) => Promise<string> {
const realpath = typeof realFs.realpath.native === 'function' ? realFs.realpath.native : realFs.realpath;
Expand Down Expand Up @@ -173,7 +177,7 @@ export async function buildJsEngine(options: Required<buildEngine.Options>): Pro
presetEnvOptions.targets = options.targets;
}

const babelPlugins: any[] = [];
const babelPlugins: babel.PluginItem[] = [];
if (!options.targets) {
babelPlugins.push([babelPluginTransformForOf, {
loose: true,
Expand Down Expand Up @@ -229,22 +233,19 @@ export async function buildJsEngine(options: Required<buildEngine.Options>): Pro
if (!process.env.ENGINE_PATH) {
throw new Error('ENGINE_PATH environment variable not set');
}
babelOptions.presets?.push([(): any => ({ plugins: [[decoratorRecorder]] })]);
babelOptions.presets?.push([(): babel.PluginItem => ({ plugins: [[recordDecorators]] })]);
}

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

if (options.noDeprecatedFeatures) {
rollupPlugins.push(removeDeprecatedFeatures(
typeof options.noDeprecatedFeatures === 'string' ? options.noDeprecatedFeatures : undefined,
));
}

const inlineEnumPlugins = await rpInlineEnum({
const rpEnumScannerPlugin = await rpEnumScanner({
scanDir: ps.join(engineRoot, 'cocos'),
moduleOverrides,
// exclude: ['*.jsb.ts'],
// scanPattern: '**/*.{cts,mts,ts,tsx}'
});

rollupPlugins.push(
Expand Down Expand Up @@ -305,7 +306,41 @@ export async function buildJsEngine(options: Required<buildEngine.Options>): Pro
);

if (options.inlineEnum) {
rollupPlugins.push(...inlineEnumPlugins);
rollupPlugins.push(...rpEnumScannerPlugin);
}

if (options.mangleProperties || options.inlineEnum) {
rollupPlugins.push(rpTypescript({
tsconfig: ps.join(engineRoot, 'tsconfig.json'),
compilerOptions: {
noEmit: false,
target: undefined,
sourceMap: undefined,
outDir: undefined,
module: 'NodeNext',
skipBuiltinTransformers: true,
},
transformers: (program) => {
const tsTransformers: Array<ts.TransformerFactory<ts.SourceFile>> = [];

if (options.inlineEnum) {
const enumData = getEnumData();
if (enumData) {
tsTransformers.push(inlineEnumTransformer(program, enumData));
} else {
console.error(`Enum data is not available for inline enum.`);
}
}

if (options.mangleProperties) {
tsTransformers.push(minifyPrivatePropertiesTransformer(program, typeof options.mangleProperties === 'object' ? options.mangleProperties : undefined));
}

return {
before: tsTransformers,
};
}
}));
}

rollupPlugins.push(
Expand Down Expand Up @@ -338,7 +373,7 @@ export async function buildJsEngine(options: Required<buildEngine.Options>): Pro
},
mangle: {
properties: options.mangleProperties ? {
regex: /^[a-zA-Z_][a-zA-Z0-9_]{3,}\$$/,
regex: /^_ccprivate\$/,
} : false,
},
keep_fnames: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/* eslint-disable no-inner-declarations */

// Inspired by https://github.com/unplugin/unplugin-inline-enum

import path from 'node:path';
import assert from 'node:assert';
import { readFileSync } from 'node:fs';
Expand Down Expand Up @@ -52,7 +55,7 @@ export interface EnumData {
readonly declarations: {
readonly [file: string]: ReadonlyArray<EnumDeclaration>;
};
readonly defines: IDefines;
readonly defines: IDefines;
}

type EnumKey<
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { type Options, resolveOptions } from './core/options';
import { EnumData, scanEnums } from './core/enum';
import { rollup as Bundler } from '@ccbuild/bundler';
import rollup = Bundler.core;

let enumData: EnumData | undefined;

export function getEnumData(): EnumData | undefined {
return enumData;
}

/**
* The main unplugin instance.
*/
export async function rpEnumScanner(rawOptions: Options): Promise<rollup.Plugin[]> {
const options = resolveOptions(rawOptions);

enumData = await scanEnums(options);
const { defines } = enumData;

defines['Float32Array.BYTES_PER_ELEMENT'] = 4;
defines['Float64Array.BYTES_PER_ELEMENT'] = 8;
defines['Uint8Array.BYTES_PER_ELEMENT'] = 1;
defines['Uint8ClampedArray.BYTES_PER_ELEMENT'] = 1;
defines['Uint16Array.BYTES_PER_ELEMENT'] = 2;
defines['Uint32Array.BYTES_PER_ELEMENT'] = 4;
defines['Int8Array.BYTES_PER_ELEMENT'] = 1;
defines['Int16Array.BYTES_PER_ELEMENT'] = 2;
defines['Int32Array.BYTES_PER_ELEMENT'] = 4;

return [];
}
126 changes: 0 additions & 126 deletions modules/build-engine/src/engine-js/rollup-plugins/inline-enum/index.ts

This file was deleted.

Loading
Loading