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

Treeshake option #66

Merged
merged 4 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 17 additions & 0 deletions .api/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ declare module "@cocos/ccbuild" {
*/
function enumerateAllDependents(meta: buildEngine.Result, featureUnits: string[]): string[];
export type ModuleFormat = "esm" | "cjs" | "system" | "iife";
export type HasModuleSideEffects = (id: string, external: boolean) => boolean;
export type ModuleSideEffectsOption = boolean | "no-external" | string[] | HasModuleSideEffects;
export type TreeshakingPreset = "smallest" | "safest" | "recommended";
export interface NormalizedTreeshakingOptions {
annotations: boolean;
correctVarValueBeforeDeclaration: boolean;
manualPureFunctions: readonly string[];
moduleSideEffects: HasModuleSideEffects;
propertyReadSideEffects: boolean | "always";
tryCatchDeoptimization: boolean;
unknownGlobalSideEffects: boolean;
}
export interface TreeshakingOptions extends Partial<Omit<NormalizedTreeshakingOptions, "moduleSideEffects">> {
moduleSideEffects?: ModuleSideEffectsOption;
preset?: TreeshakingPreset;
}
export interface Options {
/**
* 引擎仓库目录。
Expand Down Expand Up @@ -162,6 +178,7 @@ declare module "@cocos/ccbuild" {
* @note It's only avaiable when options.moduleFormat is 'system'.
*/
enableNamedRegisterForSystemJSModuleFormat?: boolean;
treeshake?: TreeshakingOptions;
}
export interface Result {
/**
Expand Down
4 changes: 4 additions & 0 deletions modules/build-engine/src/engine-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,10 @@ export async function buildJsEngine(options: Required<buildEngine.Options>): Pro
onwarn: rollupWarningHandler,
};

if (options.treeshake) {
rollupOptions.treeshake = options.treeshake;
}

const perf = true;

if (perf) {
Expand Down
23 changes: 23 additions & 0 deletions modules/build-engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,27 @@ export async function buildEngine (options: buildEngine.Options): Promise<buildE
*/
export namespace buildEngine {
export type ModuleFormat = 'esm' | 'cjs' | 'system' | 'iife';

export type HasModuleSideEffects = (id: string, external: boolean) => boolean;
export type ModuleSideEffectsOption = boolean | 'no-external' | string[] | HasModuleSideEffects;

export type TreeshakingPreset = 'smallest' | 'safest' | 'recommended';

export interface NormalizedTreeshakingOptions {
annotations: boolean;
correctVarValueBeforeDeclaration: boolean;
manualPureFunctions: readonly string[];
moduleSideEffects: HasModuleSideEffects;
propertyReadSideEffects: boolean | 'always';
tryCatchDeoptimization: boolean;
unknownGlobalSideEffects: boolean;
}

// See https://rollupjs.org/configuration-options/#treeshake for more details.
export interface TreeshakingOptions extends Partial<Omit<NormalizedTreeshakingOptions, 'moduleSideEffects'>> {
moduleSideEffects?: ModuleSideEffectsOption;
preset?: TreeshakingPreset;
}

export interface Options {
/**
Expand Down Expand Up @@ -237,6 +258,8 @@ export namespace buildEngine {
* @note It's only avaiable when options.moduleFormat is 'system'.
*/
enableNamedRegisterForSystemJSModuleFormat?: boolean;

treeshake?: TreeshakingOptions;
}

export interface Result {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cocos/ccbuild",
"version": "2.2.12",
"version": "2.2.13",
"description": "The next generation of build tool for Cocos engine.",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
22 changes: 20 additions & 2 deletions scripts/test-build-cocos.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ const argv = process.argv;

const outDir = ps.join(__dirname, '..', 'build-cc-out');

const noSideEffectFiles = [
'instantiate-jit.ts',
'splash-screen.ts',
];

const options = {
"engine": enginePath,
"out": outDir,
Expand All @@ -27,18 +32,31 @@ const argv = process.argv;
"noDeprecatedFeatures": true,
"sourceMap": false,
// "features": ["2d", "3d", "animation", "audio", "base", "debug-renderer", "dragon-bones", "geometry-renderer", "gfx-webgl", "intersection-2d", "legacy-pipeline", "light-probe", "particle", "particle-2d", "physics-2d-builtin", "physics-cannon", "primitive", "profiler", "skeletal-animation", "spine", "terrain", "tiled-map", "tween", "ui", "video", "websocket", "webview"],
"features":["gfx-webgl2"],
// "features":["gfx-webgl2"],
"features": ["2d","audio","base","gfx-webgl2","legacy-pipeline","ui"], //,"animation","spine","tween"
// "features":["base", "audio", "2d", "ui", "gfx-webgl2"],
"loose": true,
"mode": "BUILD",
"flags": {
"DEBUG": false,
ONLY_2D: true,
"WEBGPU": false
},
// "metaFile": ps.join(outDir, "meta.json"),
// "incremental": ps.join(outDir, "watch-files.json"),
"wasmCompressionMode": false,
"wasmCompressionMode": 'brotli',
"visualize": true,
"inlineEnum": true,
treeshake: {
moduleSideEffects: (id, isExternal) => {
const fileName = ps.basename(id);
if (noSideEffectFiles.indexOf(fileName) >= 0) {
console.info(`--> Found fileName: ${fileName}`);
return false;
}
return true;
}
},
};

await ensureDir(outDir);
Expand Down
Loading
Loading