diff --git a/modules/build-engine/src/index.ts b/modules/build-engine/src/index.ts index c8f7e6a..67e901d 100644 --- a/modules/build-engine/src/index.ts +++ b/modules/build-engine/src/index.ts @@ -85,6 +85,7 @@ export namespace buildEngine { unknownGlobalSideEffects: boolean; } + // See https://rollupjs.org/configuration-options/#treeshake for more details. export interface TreeshakingOptions extends Partial> { moduleSideEffects?: ModuleSideEffectsOption; preset?: TreeshakingPreset; diff --git a/scripts/test-build-cocos.js b/scripts/test-build-cocos.js index 9d1e3ca..d89b7f2 100644 --- a/scripts/test-build-cocos.js +++ b/scripts/test-build-cocos.js @@ -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, @@ -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); diff --git a/test/build-engine/__snapshots__/engine-js.test.ts.snap b/test/build-engine/__snapshots__/engine-js.test.ts.snap index 2089c91..58f7172 100644 --- a/test/build-engine/__snapshots__/engine-js.test.ts.snap +++ b/test/build-engine/__snapshots__/engine-js.test.ts.snap @@ -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 = []; @@ -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 = []; @@ -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 = []; diff --git a/test/test-engine-source/cocos/serialization/instantiate-jit.ts b/test/test-engine-source/cocos/serialization/instantiate-jit.ts index 713dadb..2427f58 100644 --- a/test/test-engine-source/cocos/serialization/instantiate-jit.ts +++ b/test/test-engine-source/cocos/serialization/instantiate-jit.ts @@ -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 {