Skip to content

Commit

Permalink
Fix transform id while checking moduleOverrides. Update version to 2.…
Browse files Browse the repository at this point in the history
…2.16 (#69)
  • Loading branch information
dumganhar authored Oct 17, 2024
1 parent ac2b20a commit b62659b
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 12 deletions.
15 changes: 10 additions & 5 deletions modules/build-engine/src/engine-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs-extra';
import ps from 'path';
import { babel as Transformer } from '@ccbuild/transformer';
import { rollup as Bundler } from '@ccbuild/bundler';
import { ps as pathUtils } from '@ccbuild/utils';
import realFs from 'fs';
import tsConfigPaths from './rollup-plugins/ts-paths';
import moduleQueryPlugin from './rollup-plugins/module-query-plugin';
Expand Down Expand Up @@ -49,9 +50,6 @@ const realPath = (function (): (file: string) => Promise<string> {
});
})();

function makePathEqualityKey(path: string): string {
return process.platform === 'win32' ? path.toLocaleLowerCase() : path;
}

export async function buildJsEngine(options: Required<buildEngine.Options>): Promise<buildEngine.Result> {
const doUglify = !!options.compress;
Expand Down Expand Up @@ -116,7 +114,7 @@ export async function buildJsEngine(options: Required<buildEngine.Options>): Pro
platform: options.platform,
buildTimeConstants,
})).reduce((result, [k, v]) => {
result[makePathEqualityKey(k)] = v;
result[pathUtils.makePathEqualityKey(k)] = v;
return result;
}, {} as Record<string, string>);

Expand Down Expand Up @@ -269,7 +267,7 @@ export async function buildJsEngine(options: Required<buildEngine.Options>): Pro
}
},
load(this, id: string): string | null {
const key = makePathEqualityKey(id);
const key = pathUtils.makePathEqualityKey(id);
if (!(key in moduleOverrides)) {
return null;
}
Expand Down Expand Up @@ -406,6 +404,13 @@ export async function buildJsEngine(options: Required<buildEngine.Options>): Pro
const treeshakeConfig = statsQuery.getTreeShakeConfig();
const noSideEffectFiles = treeshakeConfig?.noSideEffectFiles;
if (noSideEffectFiles && noSideEffectFiles.length > 0) {
for (const noSideEffectFile of noSideEffectFiles) {
const absolutePath = ps.join(engineRoot, noSideEffectFile);
if (!fs.pathExistsSync(absolutePath)) {
console.error(`>>> ERROR: noSideEffectFile: ( ${noSideEffectFile} ) doesn't exist!`);
}
}

rollupOptions.treeshake = {
moduleSideEffects: (id: string): boolean => {
const relativePath = formatPath(ps.relative(engineRoot, id));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ReplacePlugin from '@rollup/plugin-replace';
import { type Options, resolveOptions } from './core/options';
import { IDefines, scanEnums } from './core/enum';
import { rollup as Bundler } from '@ccbuild/bundler';
import { ps as pathUtils } from '@ccbuild/utils';
import rollup = Bundler.core;


Expand Down Expand Up @@ -55,6 +56,7 @@ export async function rpInlineEnum(rawOptions: Options, meta?: any): Promise<rol
);

const name = 'cc-inline-enum';

return [
{
name,
Expand All @@ -66,7 +68,8 @@ export async function rpInlineEnum(rawOptions: Options, meta?: any): Promise<rol

transform(this, code: string, moduleId: string): rollup.TransformResult {
// Don't transform a module that is overrode
if (options.moduleOverrides && (moduleId in options.moduleOverrides)) {
const cacheKey = pathUtils.makePathEqualityKey(moduleId);
if (options.moduleOverrides && (cacheKey in options.moduleOverrides)) {
return;
}

Expand Down
4 changes: 4 additions & 0 deletions modules/utils/src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ export async function readdirR (item: string, reduceOutput: string[]): Promise<v
reduceOutput.push(item);
}
}

export function makePathEqualityKey(path: string): string {
return process.platform === 'win32' ? path.toLocaleLowerCase() : path;
}
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.15",
"version": "2.2.16",
"description": "The next generation of build tool for Cocos engine.",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
22 changes: 22 additions & 0 deletions test/build-engine/__snapshots__/engine-js.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1940,6 +1940,28 @@ exports[`engine-js inline enum 2`] = `
console.log(4026531840);
console.log(268435455);
console.log("Testing enum override .......");
console.log("Testing enum override .......");
console.log("Testing enum override .......");
console.log("Testing enum override .......");
console.log("Testing enum override .......");
console.log("Testing enum override .......");
console.log("Testing enum override .......");
console.log("Testing enum override .......");
console.log("Testing enum override .......");
console.log("Testing enum override .......");
console.log("Testing enum override .......");
var TestOverrideEnum;
(function (TestOverrideEnum) {
TestOverrideEnum["HAHA"] = "hello_haha";
TestOverrideEnum["WOWO"] = "hello_wowo";
TestOverrideEnum["HEIHEI"] = "hello_heihei";
})(TestOverrideEnum || (TestOverrideEnum = {}));
console.log(2);
console.log(0);
console.log(1);
var MyEnum2 = exports("MyEnum2", {
"AAABBB": 0,
"Haha": 0,
Expand Down
14 changes: 11 additions & 3 deletions test/test-engine-source/cc.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,19 @@
"internal:native": "./audio/internal-native.ts"
}
},
{
"test": "true",
"isVirtualModule": false,
"overrides": {
"./cocos/enums/TestOverride.ts": "./cocos/enums/TestOverride.jsb.ts",
"./cocos/enums/test-override-entry.ts": "./cocos/enums/test-override-entry.jsb.ts"
}
},
{
"test": "context.mode === 'BUILD'",
"isVirtualModule": false,
"overrides": {}
"overrides": {
}
},
{
"test": "context.buildTimeConstants.NATIVE",
Expand Down Expand Up @@ -428,8 +437,7 @@

"treeShake": {
"noSideEffectFiles": [
"cocos/serialization/instantiate-jit.ts",
"cocos/game/splash-screen.ts"
"cocos/serialization/instantiate-jit.ts"
]
}
}
17 changes: 17 additions & 0 deletions test/test-engine-source/cocos/enums/TestOverride.jsb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
console.log(`Testing enum override .......`);
console.log(`Testing enum override .......`);
console.log(`Testing enum override .......`);
console.log(`Testing enum override .......`);
console.log(`Testing enum override .......`);
console.log(`Testing enum override .......`);
console.log(`Testing enum override .......`);
console.log(`Testing enum override .......`);
console.log(`Testing enum override .......`);
console.log(`Testing enum override .......`);
console.log(`Testing enum override .......`);
export enum TestOverrideEnum {
HAHA = 'hello_haha',
WOWO = 'hello_wowo',
HEIHEI = 'hello_heihei'
}

14 changes: 14 additions & 0 deletions test/test-engine-source/cocos/enums/TestOverride.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { MyEnum4 } from './my-enum-4';

export enum TestOverrideEnum {
HAHA,
WOWO,
HEIHEI
}

console.log(TestOverrideEnum.WOWO);
console.log(TestOverrideEnum.HAHA);
console.log(TestOverrideEnum.HEIHEI);


console.log(MyEnum4.AAABBB, MyEnum4.Haha, MyEnum4.Hi);
1 change: 1 addition & 0 deletions test/test-engine-source/cocos/enums/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as gfx from './define';

import { PixelFormat } from './define';

import './test-override-entry';

export enum MyEnum2 {
AAABBB,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as Override from './TestOverride';
console.log(Override.TestOverrideEnum.HEIHEI);
console.log(Override.TestOverrideEnum.HAHA);
console.log(Override.TestOverrideEnum.WOWO);
8 changes: 8 additions & 0 deletions test/test-engine-source/cocos/enums/test-override-entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@


import * as Override from './TestOverride';

console.log(Override.TestOverrideEnum.WOWO);
console.log(Override.TestOverrideEnum.HAHA);
console.log(Override.TestOverrideEnum.HEIHEI);

0 comments on commit b62659b

Please sign in to comment.