Skip to content

Commit

Permalink
Revert treeshake option, configure it in cc.config.json (#67)
Browse files Browse the repository at this point in the history
* Revert treeshake option, configure it in cc.config.json

* Update test

* Update version to 2.2.14
  • Loading branch information
dumganhar authored Sep 24, 2024
1 parent abd4370 commit 01cb556
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 393 deletions.
28 changes: 11 additions & 17 deletions .api/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,6 @@ 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 @@ -178,7 +162,6 @@ declare module "@cocos/ccbuild" {
* @note It's only avaiable when options.moduleFormat is 'system'.
*/
enableNamedRegisterForSystemJSModuleFormat?: boolean;
treeshake?: TreeshakingOptions;
}
export interface Result {
/**
Expand Down Expand Up @@ -231,6 +214,10 @@ declare module "@cocos/ccbuild" {
* Gets all optimzie decorators
*/
getOptimizeDecorators(): ConfigInterface.IOptimizeDecorators;
/**
* Gets TreeShake config
*/
getTreeShakeConfig(): ConfigInterface.ITreeShakeConfig | undefined;
/**
* Gets all features defined.
*/
Expand Down Expand Up @@ -375,6 +362,10 @@ declare module "@cocos/ccbuild" {
* The decorators to be optimize when build engine.
*/
optimizeDecorators: IOptimizeDecorators;
/**
* The TreeShake config
*/
treeShake?: ITreeShakeConfig;
}
export interface IndexConfig {
modules?: Record<string, {
Expand Down Expand Up @@ -481,6 +472,9 @@ declare module "@cocos/ccbuild" {
*/
editorDecorators: string[];
}
export interface ITreeShakeConfig {
noSideEffectFiles: string[];
}
}
export namespace Transformer {
export namespace babel {
Expand Down
19 changes: 16 additions & 3 deletions modules/build-engine/src/engine-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import removeDeprecatedFeatures from './rollup-plugins/remove-deprecated-feature
import type { buildEngine } from '../index';
import { externalWasmLoader } from './rollup-plugins/external-wasm-loader';
import { StatsQuery } from '@ccbuild/stats-query';
import { filePathToModuleRequest } from '@ccbuild/utils';
import { filePathToModuleRequest, formatPath } from '@ccbuild/utils';
import { rpNamedChunk } from './rollup-plugins/systemjs-named-register-plugin';
import { rpInlineEnum } from './rollup-plugins/inline-enum';

Expand Down Expand Up @@ -402,8 +402,21 @@ export async function buildJsEngine(options: Required<buildEngine.Options>): Pro
onwarn: rollupWarningHandler,
};

if (options.treeshake) {
rollupOptions.treeshake = options.treeshake;
const treeshakeConfig = statsQuery.getTreeShakeConfig();
const noSideEffectFiles = treeshakeConfig?.noSideEffectFiles;
if (noSideEffectFiles && noSideEffectFiles.length > 0) {
rollupOptions.treeshake = {
moduleSideEffects: (id: string): boolean => {
const relativePath = formatPath(ps.relative(engineRoot, id));
if (noSideEffectFiles.includes(relativePath)) {
console.info(`>>> Found no side-effect path: ${relativePath}`);
return false;
}
return true;
}
};
} else {
console.info(`>>> No treeshake config found!`);
}

const perf = true;
Expand Down
25 changes: 1 addition & 24 deletions modules/build-engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,7 @@ 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 @@ -258,8 +237,6 @@ export namespace buildEngine {
* @note It's only avaiable when options.moduleFormat is 'system'.
*/
enableNamedRegisterForSystemJSModuleFormat?: boolean;

treeshake?: TreeshakingOptions;
}

export interface Result {
Expand Down
9 changes: 9 additions & 0 deletions modules/stats-query/src/config-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export interface Config {
* The decorators to be optimize when build engine.
*/
optimizeDecorators: IOptimizeDecorators;

/**
* The TreeShake config
*/
treeShake?: ITreeShakeConfig;
}

export interface IndexConfig {
Expand Down Expand Up @@ -150,4 +155,8 @@ export interface IOptimizeDecorators {
* The decorators which should be removed directly when they only work in Cocos Creator editor.
*/
editorDecorators: string[],
}

export interface ITreeShakeConfig {
noSideEffectFiles: string[];
}
7 changes: 7 additions & 0 deletions modules/stats-query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ export class StatsQuery {
return this._config.optimizeDecorators;
}

/**
* Gets TreeShake config
*/
public getTreeShakeConfig (): ConfigInterface.ITreeShakeConfig | undefined {
return this._config.treeShake;
}

/**
* Gets all features defined.
*/
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.13",
"version": "2.2.14",
"description": "The next generation of build tool for Cocos engine.",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
12 changes: 1 addition & 11 deletions scripts/test-build-cocos.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const ps = require('path');
const { ensureDir, emptyDir } = require('fs-extra');
const { buildEngine } = require('../lib/src');
const { buildEngine, StatsQuery } = require('../lib/src');

const argv = process.argv;

Expand Down Expand Up @@ -47,16 +47,6 @@ const argv = process.argv;
"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

0 comments on commit 01cb556

Please sign in to comment.