Skip to content

Commit

Permalink
[v3.8.6] Support mangling private properties. (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
dumganhar authored Dec 27, 2024
1 parent 6526853 commit 2e7da44
Show file tree
Hide file tree
Showing 29 changed files with 4,199 additions and 616 deletions.
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 @@ -317,7 +317,7 @@ function cvtPropDecorators(className: string, ctx: ClassDecoratorContext) {
};
}

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

0 comments on commit 2e7da44

Please sign in to comment.