Skip to content

Commit

Permalink
Update treeshake demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
dumganhar committed Sep 24, 2024
1 parent d7f48c8 commit 60909b0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions modules/build-engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export namespace buildEngine {
unknownGlobalSideEffects: boolean;
}

// See https://rollupjs.org/configuration-options/#treeshake for more details.
export interface TreeshakingOptions extends Partial<Omit<NormalizedTreeshakingOptions, 'moduleSideEffects'>> {
moduleSideEffects?: ModuleSideEffectsOption;
preset?: TreeshakingPreset;
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
15 changes: 15 additions & 0 deletions test/build-engine/__snapshots__/engine-js.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ exports[`engine-js HTML5 with treeshake option 2`] = `
}();
console.log("I'm instantiate-jit.ts");
console.log('side-effect');
var Impure = function Impure() {
console.log('side-effect');
};
new Impure();
var Assignments = function () {
function Assignments(targetExpression) {
this._exps = [];
Expand Down Expand Up @@ -217,6 +222,11 @@ exports[`engine-js HTML5 without treeshake option 2`] = `
}();
console.log("I'm instantiate-jit.ts");
console.log('side-effect');
var Impure = function Impure() {
console.log('side-effect');
};
new Impure();
var Assignments = function () {
function Assignments(targetExpression) {
this._exps = [];
Expand Down Expand Up @@ -408,6 +418,11 @@ exports[`engine-js WECHAT without treeshake option 2`] = `
}();
console.log("I'm instantiate-jit.ts");
console.log('side-effect');
var Impure = function Impure() {
console.log('side-effect');
};
new Impure();
var Assignments = function () {
function Assignments(targetExpression) {
this._exps = [];
Expand Down
10 changes: 10 additions & 0 deletions test/test-engine-source/cocos/serialization/instantiate-jit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ import * as js from '../core/utils/pool';

console.log(`I'm instantiate-jit.ts`);

/*@__PURE__*/ console.log('side-effect');

class Impure {
constructor() {
console.log('side-effect');
}
}

/*@__PURE__ There may be additional text in the comment */ new Impure();

const VAR = 'var ';

class Declaration {
Expand Down

0 comments on commit 60909b0

Please sign in to comment.