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

Fix transform id while checking moduleOverrides. Update version to 2.2.16 #69

Merged
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
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 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 @@
});
})();

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 @@
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 @@ -175,7 +173,7 @@
presetEnvOptions.targets = options.targets;
}

const babelPlugins: any[] = [];

Check warning on line 176 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 +229,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 232 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 @@ -261,7 +259,7 @@

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

Check warning on line 262 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 All @@ -269,7 +267,7 @@
}
},
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 @@
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 { 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 All @@ -23,7 +24,7 @@
/**
* The main unplugin instance.
*/
export async function rpInlineEnum(rawOptions: Options, meta?: any): Promise<rollup.Plugin[]> {

Check warning on line 27 in modules/build-engine/src/engine-js/rollup-plugins/inline-enum/index.ts

View workflow job for this annotation

GitHub Actions / test

'meta' is defined but never used

Check warning on line 27 in modules/build-engine/src/engine-js/rollup-plugins/inline-enum/index.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
const options = resolveOptions(rawOptions);
const filter = createFilter(options.include, options.exclude);

Expand Down Expand Up @@ -55,18 +56,20 @@
);

const name = 'cc-inline-enum';

return [
{
name,
// enforce: options.enforce,

async resolveId (this, source, importer): Promise<string | { id: string; external: true; } | null> {

Check warning on line 65 in modules/build-engine/src/engine-js/rollup-plugins/inline-enum/index.ts

View workflow job for this annotation

GitHub Actions / test

'importer' is defined but never used
return filter(source) ? source : null;
},

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 All @@ -90,7 +93,7 @@
`${prefix} const ${id} = {${members
.flatMap(({ name, value }) => {
const forwardMapping = `${JSON.stringify(name)}: ${JSON.stringify(value)}`;
const reverseMapping = `${JSON.stringify(value.toString())}: ${JSON.stringify(name)}`;

Check warning on line 96 in modules/build-engine/src/engine-js/rollup-plugins/inline-enum/index.ts

View workflow job for this annotation

GitHub Actions / test

'reverseMapping' is assigned a value but never used

// see https://www.typescriptlang.org/docs/handbook/enums.html#reverse-mappings
return typeof value === 'string'
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);

Loading