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

Revert treeshake option, configure it in cc.config.json #67

Merged
merged 3 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
28 changes: 11 additions & 17 deletions .api/public.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare module "@cocos/ccbuild" {

Check warning on line 1 in .api/public.d.ts

View workflow job for this annotation

GitHub Actions / test

File ignored by default.
/**
* @group Merged Types
*/
Expand Down Expand Up @@ -27,22 +27,6 @@
*/
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 @@
* @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 @@
* 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 @@
* 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 @@
*/
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 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 @@ -175,7 +175,7 @@
presetEnvOptions.targets = options.targets;
}

const babelPlugins: any[] = [];

Check warning on line 178 in modules/build-engine/src/engine-js/index.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
if (!options.targets) {
babelPlugins.push([babelPluginTransformForOf, {
loose: true,
Expand Down Expand Up @@ -231,7 +231,7 @@
if (!process.env.ENGINE_PATH) {
throw new Error('ENGINE_PATH environment variable not set');
}
babelOptions.presets?.push([(): any => ({ plugins: [[decoratorRecorder]] })]);

Check warning on line 234 in modules/build-engine/src/engine-js/index.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
}

const rollupPlugins: rollup.Plugin[] = [];
Expand Down Expand Up @@ -260,7 +260,7 @@

{
name: '@cocos/ccbuild|module-overrides',
resolveId(source, importer): string | null {

Check warning on line 263 in modules/build-engine/src/engine-js/index.ts

View workflow job for this annotation

GitHub Actions / test

'importer' is defined but never used
if (moduleOverrides[source]) {
return source;
} else {
Expand Down Expand Up @@ -402,8 +402,21 @@
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 @@ -4,7 +4,7 @@
import fs from 'fs-extra';
import { buildTsEngine } from './engine-ts';

function verifyCache (options: buildEngine.Options): boolean {

Check warning on line 7 in modules/build-engine/src/index.ts

View workflow job for this annotation

GitHub Actions / test

'options' is defined but never used
// TODO
return false;
}
Expand Down Expand Up @@ -69,28 +69,7 @@
*/
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 @@
* @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
Loading