From 6f57c3ad8fbdc257fa222bf0a417feace6b6652a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leslie=20Leigh=20=28=E6=9D=8E=E7=9A=84=E5=BA=8F=29?= Date: Fri, 4 Dec 2020 15:06:54 +0800 Subject: [PATCH] Optimize build time constants implementation (#7882) --- @types/consts.d.ts | 112 --------------- @types/raw-constants.d.ts | 63 +++++++++ cocos/core/default-constants.ts | 62 --------- cocos/core/global-exports.ts | 128 +++-------------- cocos/core/utils/internal-constants.ts | 131 ++++++++++++++++++ .../build-engine/src/build-time-constants.ts | 51 ------- scripts/build-engine/src/index.ts | 23 +-- tests/init.ts | 4 +- tests/utils/testing-raw-constants.ts | 3 + tsconfig.json | 5 +- 10 files changed, 238 insertions(+), 344 deletions(-) delete mode 100644 @types/consts.d.ts create mode 100644 @types/raw-constants.d.ts delete mode 100644 cocos/core/default-constants.ts create mode 100644 cocos/core/utils/internal-constants.ts delete mode 100644 scripts/build-engine/src/build-time-constants.ts create mode 100644 tests/utils/testing-raw-constants.ts diff --git a/@types/consts.d.ts b/@types/consts.d.ts deleted file mode 100644 index 310b4c038e..0000000000 --- a/@types/consts.d.ts +++ /dev/null @@ -1,112 +0,0 @@ -declare module 'internal:constants' { - /** - * This constant is private and used for internal purpose only. - * - * If true, some of the constants of `internal:constants` would be also export to the global namespace. - * For example would set `CC_EDITOR` on `globalThis` as `EDITOR`. - * This is due to we at present inherits the way how Creator prior to 3.0 deploys the build time constants. - */ - export const EXPORT_TO_GLOBAL: boolean; - - /** - * Running in published project. - */ - export const BUILD: boolean; - - /** - * Running in the engine's unit test. - */ - export const TEST: boolean; - - /** - * Running in the editor. - */ - export const EDITOR: boolean; - - /** - * Preview in browser or simulator. - */ - export const PREVIEW: boolean; - - /** - * Running in the editor or preview. - */ - export const DEV: boolean; - - /** - * Running in the editor or preview, or build in debug mode. - */ - export const DEBUG: boolean; - - /** - * Running in native platform (mobile app, desktop app, or simulator). - */ - export const JSB: boolean; - - /** - * Running in the Wechat's mini game. - */ - export const WECHAT: boolean; - - /** - * Running in the alipay's mini game. - */ - export const ALIPAY: boolean; - - /** - * Running in the xiaomi's quick game. - */ - export const XIAOMI: boolean; - - /** - * Running in the ByteDance's quick game. - */ - export const BYTEDANCE: boolean; - - /** - * Running in the baidu's mini game. - */ - export const BAIDU: boolean; - - /** - * Running in the cocosplay. - */ - export const COCOSPLAY: boolean; - - /** - * Running in the huawei's quick game. - */ - export const HUAWEI: boolean; - - /** - * Running in the oppo's mini game. - */ - export const OPPO: boolean; - - /** - * Running in the vivo's mini game. - */ - export const VIVO: boolean; - - /** - * Running in the qtt's quick game. - */ - export const QTT: boolean; - - /** - * Running in the linksure's quick game. - */ - export const LINKSURE: boolean; - - /** - * Running in mini game. - */ - export const MINIGAME: boolean; - - /** - * Running in runtime environments. - */ - export const RUNTIME_BASED: boolean; - - export const SUPPORT_JIT: boolean; -} diff --git a/@types/raw-constants.d.ts b/@types/raw-constants.d.ts new file mode 100644 index 0000000000..d9f331533e --- /dev/null +++ b/@types/raw-constants.d.ts @@ -0,0 +1,63 @@ + +/* + Copyright (c) 2020 Xiamen Yaji Software Co., Ltd. + + https://www.cocos.com/ + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + */ + +/** + * Provides some raw constants to be consumed by `'internal:constants'`. + */ +declare module 'internal:raw-constants' { + const _: { + // These variables are 1-1 mapped to `'internal:constants'`. + // All default to `false`. + BUILD?: boolean; + TEST?: boolean; + EDITOR?: boolean; + PREVIEW?: boolean; + + // Would be set if it's debug build of production. + // Default to `false`. + DEBUG?: boolean; + + // Would be set if it's build for specified platform. + // If not specified, the platform is unknown. + platform?: + // Please sort this options in dict-order. + | 'ALIPAY' + | 'BAIDU' + | 'BYTEDANCE' + | 'COCOSPLAY' + | 'HUAWEI' + | 'LINKSURE' + | 'NATIVE' + | 'OPPO' + | 'QTT' + | 'VIVO' + | 'WECHAT' + | 'XIAOMI' + ; + }; + + export default _; +} diff --git a/cocos/core/default-constants.ts b/cocos/core/default-constants.ts deleted file mode 100644 index a54b6d314c..0000000000 --- a/cocos/core/default-constants.ts +++ /dev/null @@ -1,62 +0,0 @@ -/* - Copyright (c) 2020 Xiamen Yaji Software Co., Ltd. - - https://www.cocos.com/ - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated engine source code (the "Software"), a limited, - worldwide, royalty-free, non-assignable, revocable and non-exclusive license - to use Cocos Creator solely to develop games on your target platforms. You shall - not use Cocos Creator software for developing other software or tools that's - used for developing games. You are not granted to publish, distribute, - sublicense, and/or sell copies of Cocos Creator. - - The software or tools in this License Agreement are licensed, not sold. - Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - */ - -const _global = typeof window === 'undefined' ? global : window; - -function defined (name: string) { - return typeof _global[name] === 'object'; -} - -function tryDefineGlobal (name: string, value: boolean): boolean { - if (typeof _global[name] === 'undefined') { - return (_global[name] = value); - } else { - return _global[name]; - } -} - -// No export to global required since we have already done here. -export const EXPORT_TO_GLOBAL = false; -export const BUILD = tryDefineGlobal('CC_BUILD', false); -export const TEST = tryDefineGlobal('CC_TEST', defined('tap') || defined('QUnit')); -export const EDITOR = tryDefineGlobal('CC_EDITOR', defined('Editor') && defined('process') && ('electron' in process.versions)); -export const PREVIEW = tryDefineGlobal('CC_PREVIEW', !EDITOR); -export const DEV = tryDefineGlobal('CC_DEV', true); // (CC_EDITOR && !CC_BUILD) || CC_PREVIEW || CC_TEST -export const DEBUG = tryDefineGlobal('CC_DEBUG', true); // CC_DEV || Debug Build -export const JSB = tryDefineGlobal('CC_JSB', defined('jsb')); -// @ts-expect-error -export const WECHAT = tryDefineGlobal('CC_WECHAT', !!(defined('wx') && (wx.getSystemInfoSync || wx.getSharedCanvas))); -export const MINIGAME = tryDefineGlobal('CC_MINIGAME', false); -export const RUNTIME_BASED = tryDefineGlobal('CC_RUNTIME_BASED', false); -export const ALIPAY = tryDefineGlobal('CC_ALIPAY', false); -export const XIAOMI = tryDefineGlobal('CC_XIAOMI', false); -export const BYTEDANCE = tryDefineGlobal('CC_BYTEDANCE', false); -export const BAIDU = tryDefineGlobal('CC_BAIDU', false); -export const COCOSPLAY = tryDefineGlobal('CC_COCOSPLAY', false); -export const HUAWEI = tryDefineGlobal('CC_HUAWEI', false); -export const OPPO = tryDefineGlobal('CC_OPPO', false); -export const VIVO = tryDefineGlobal('CC_VIVO', false); -// @ts-expect-error -export const SUPPORT_JIT = tryDefineGlobal('CC_SUPPORT_JIT', ('function' === typeof loadRuntime)); diff --git a/cocos/core/global-exports.ts b/cocos/core/global-exports.ts index 0e75cfca96..09edb975cd 100644 --- a/cocos/core/global-exports.ts +++ b/cocos/core/global-exports.ts @@ -48,17 +48,13 @@ import { HUAWEI, OPPO, VIVO, - EXPORT_TO_GLOBAL, } from 'internal:constants'; const _global = typeof window === 'undefined' ? global : window; /** - * @en - * The main namespace of Cocos2d-JS, all engine core classes, functions, properties and constants are defined in this namespace. - * @zh - * Cocos 引擎的主要命名空间,引擎代码中所有的类,函数,属性和常量都在这个命名空间中定义。 - * @deprecated + * The internal variable to strike some circular reference problem. + * This also records the content of legacy global variable `cc`(prior to 3.0). */ export const legacyCC: Record & { _global: typeof globalThis; @@ -69,105 +65,27 @@ export const legacyCC: Record & { // For internal usage legacyCC.internal = {}; -if (EXPORT_TO_GLOBAL) { - // Supports dynamically access from external scripts such as adapters and debugger. - // So macros should still defined in global even if inlined in engine. - /** - * @en The pre-compilation constant for code tree shaking: CC_BUILD (Available for built package) - * @zh 预编译宏变量,通常用来做平台或环境相关自动代码剔除:CC_BUILD(在构建后生效) - */ - _global.CC_BUILD = BUILD; - /** - * @en The pre-compilation constant for code tree shaking: CC_TEST (Available for ci test environment) - * @zh 预编译宏变量,通常用来做平台或环境相关自动代码剔除:CC_TEST(在 CI 测试环境下生效) - */ - _global.CC_TEST = TEST; - /** - * @en The pre-compilation constant for code tree shaking: CC_EDITOR (Available for editor environment) - * @zh 预编译宏变量,通常用来做平台或环境相关自动代码剔除:CC_EDITOR(在编辑器环境下生效) - */ - _global.CC_EDITOR = EDITOR; - /** - * @en The pre-compilation constant for code tree shaking: CC_PREVIEW (Available for preview) - * @zh 预编译宏变量,通常用来做平台或环境相关自动代码剔除:CC_PREVIEW(预览时生效) - */ - _global.CC_PREVIEW = PREVIEW; - /** - * @en The pre-compilation constant for code tree shaking: CC_DEV (Internal) - * @zh 预编译宏变量,通常用来做平台或环境相关自动代码剔除:CC_DEV(内部使用) - */ - _global.CC_DEV = DEV; - /** - * @en The pre-compilation constant for code tree shaking: CC_DEBUG (Available for debug environment) - * @zh 预编译宏变量,通常用来做平台或环境相关自动代码剔除:CC_DEBUG(在调试模式下生效) - */ - _global.CC_DEBUG = DEBUG; - /** - * @en The pre-compilation constant for code tree shaking: CC_JSB (Available for native application environment) - * @zh 预编译宏变量,通常用来做平台或环境相关自动代码剔除:CC_JSB(在原生应用环境下生效) - */ - _global.CC_JSB = JSB; - /** - * @en The pre-compilation constant for code tree shaking: CC_BYTEDANCE (Available for Bytedance platform) - * @zh 预编译宏变量,通常用来做平台或环境相关自动代码剔除:CC_BYTEDANCE(在字节平台上生效) - */ - _global.CC_BYTEDANCE = BYTEDANCE; - /** - * @en The pre-compilation constant for code tree shaking: CC_WECHAT (Available for Wechat mini game platform) - * @zh 预编译宏变量,通常用来做平台或环境相关自动代码剔除:CC_WECHAT(在微信小游戏平台上生效) - */ - _global.CC_WECHAT = WECHAT; - /** - * @en The pre-compilation constant for code tree shaking: CC_ALIPAY (Available for Alipay mini game platform) - * @zh 预编译宏变量,通常用来做平台或环境相关自动代码剔除:CC_ALIPAY(在支付宝小游戏平台上生效) - */ - _global.CC_ALIPAY = ALIPAY; - /** - * @en The pre-compilation constant for code tree shaking: CC_XIAOMI (Available for MI mini game platform) - * @zh 预编译宏变量,通常用来做平台或环境相关自动代码剔除:CC_XIAOMI(在小米小游戏平台上生效) - */ - _global.CC_XIAOMI = XIAOMI; - /** - * @en The pre-compilation constant for code tree shaking: CC_BAIDU (Available for Baidu mini game platform) - * @zh 预编译宏变量,通常用来做平台或环境相关自动代码剔除:CC_BAIDU(在百度小游戏平台上生效) - */ - _global.CC_BAIDU = BAIDU; - /** - * @en The pre-compilation constant for code tree shaking: CC_COCOSPLAY (Available for Cocos Play platform) - * @zh 预编译宏变量,通常用来做平台或环境相关自动代码剔除:CC_COCOSPLAY(在 CocosPlay 小游戏平台上生效) - */ - _global.CC_COCOSPLAY = COCOSPLAY; - /** - * @en The pre-compilation constant for code tree shaking: CC_HUAWEI (Available for Huawei mini game platform) - * @zh 预编译宏变量,通常用来做平台或环境相关自动代码剔除:CC_HUAWEI(在华为快游戏平台上生效) - */ - _global.CC_HUAWEI = HUAWEI; - /** - * @en The pre-compilation constant for code tree shaking: CC_OPPO (Available for OPPO mini game platform) - * @zh 预编译宏变量,通常用来做平台或环境相关自动代码剔除:CC_OPPO(在 OPPO 小游戏平台上生效) - */ - _global.CC_OPPO = OPPO; - /** - * @en The pre-compilation constant for code tree shaking: CC_VIVO (Available for Vivo mini game platform) - * @zh 预编译宏变量,通常用来做平台或环境相关自动代码剔除:CC_VIVO(在 Vivo 小游戏平台上生效) - */ - _global.CC_VIVO = VIVO; - /** - * @en The pre-compilation constant for code tree shaking: CC_MINIGAME (Available for general mini game platforms) - * @zh 预编译宏变量,通常用来做平台或环境相关自动代码剔除:CC_MINIGAME(在各个小游戏平台上生效) - */ - _global.CC_MINIGAME = MINIGAME; - /** - * @en The pre-compilation constant for code tree shaking: CC_RUNTIME_BASED (Available for Huawei, OPPO, Vivo and Cocos Play) - * @zh 预编译宏变量,通常用来做平台或环境相关自动代码剔除:CC_RUNTIME_BASED(在华为、OPPO、Vivo 和 CocosPlay 平台上生效) - */ - _global.CC_RUNTIME_BASED = RUNTIME_BASED; - /** - * @en The pre-compilation constant for code tree shaking: CC_SUPPORT_JIT (Available for platforms support JIT) - * @zh 预编译宏变量,通常用来做平台或环境相关自动代码剔除:CC_SUPPORT_JIT(在支持 JIT 的平台上生效) - */ - _global.CC_SUPPORT_JIT = SUPPORT_JIT; -} +// These constants are also exported into global namespace for compatible with Creator prior to 3.0. +// Use `'cce.env'` from external, `'internal-constants'` from within internal instead. +_global.CC_BUILD = BUILD; +_global.CC_TEST = TEST; +_global.CC_EDITOR = EDITOR; +_global.CC_PREVIEW = PREVIEW; +_global.CC_DEV = DEV; +_global.CC_DEBUG = DEBUG; +_global.CC_JSB = JSB; +_global.CC_BYTEDANCE = BYTEDANCE; +_global.CC_WECHAT = WECHAT; +_global.CC_ALIPAY = ALIPAY; +_global.CC_XIAOMI = XIAOMI; +_global.CC_BAIDU = BAIDU; +_global.CC_COCOSPLAY = COCOSPLAY; +_global.CC_HUAWEI = HUAWEI; +_global.CC_OPPO = OPPO; +_global.CC_VIVO = VIVO; +_global.CC_MINIGAME = MINIGAME; +_global.CC_RUNTIME_BASED = RUNTIME_BASED; +_global.CC_SUPPORT_JIT = SUPPORT_JIT; if (DEV) { legacyCC._Test = {}; diff --git a/cocos/core/utils/internal-constants.ts b/cocos/core/utils/internal-constants.ts new file mode 100644 index 0000000000..0c71ca19fe --- /dev/null +++ b/cocos/core/utils/internal-constants.ts @@ -0,0 +1,131 @@ +/* + Copyright (c) 2020 Xiamen Yaji Software Co., Ltd. + + https://www.cocos.com/ + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + */ + +import raw from "internal:raw-constants"; + +/** + * Tell if is targeting production build. + */ +export const BUILD = raw.BUILD === true; + +/** + * Tell if is targeting engine's unit test. + */ +export const TEST = raw.TEST === true; + +/** + * Tell if is targeting the editor. + */ +export const EDITOR = raw.EDITOR === true; + +/** + * Tell if is targeting preview(in either browser or simulator). + */ +export const PREVIEW = raw.PREVIEW === true; + +/** + * Tell if is targeting editor or preview. + */ +export const DEV = EDITOR || PREVIEW || TEST; + +/** + * Tell if is targeting the editor or preview, or debug build of production. + */ +export const DEBUG = raw.DEBUG === true || DEV; + +/** + * Tell if is targeting native platforms (mobile app, desktop app, or simulator). + */ +export const JSB = raw.platform === 'NATIVE'; + +/** + * Tell if is targeting Wechat mini game. + */ +export const WECHAT = raw.platform === 'WECHAT'; + +/** + * Tell if is targeting Alipay mini game. + */ +export const ALIPAY = raw.platform === 'ALIPAY'; + +/** + * Tell if is targeting Xiaomi quick game. + */ +export const XIAOMI = raw.platform === 'XIAOMI'; + +/** + * Tell if is targeting ByteDance quick game. + */ +export const BYTEDANCE = raw.platform === 'BYTEDANCE'; + +/** + * Tell if is targeting Baidu mini game. + */ +export const BAIDU = raw.platform === 'BAIDU'; + +/** + * Tell if is targeting CocosPlay. + */ +export const COCOSPLAY = raw.platform === 'COCOSPLAY'; + +/** + * Tell if is targeting Huawei quick game. + */ +export const HUAWEI = raw.platform === 'HUAWEI'; + +/** + * Tell if is targeting OPPO mini game. + */ +export const OPPO = raw.platform === 'OPPO'; + +/** + * Tell if is targeting VIVO mini game. + */ +export const VIVO = raw.platform === 'VIVO'; + +/** + * Tell if is targeting QTT quick game. + */ +export const QTT = raw.platform === 'QTT'; + +/** + * Tell if is targeting linksure quick game. + */ +export const LINKSURE = raw.platform === 'LINKSURE'; + +/** + * Tell if is targeting mini games. + */ +export const MINIGAME = WECHAT || ALIPAY || XIAOMI || BYTEDANCE || BAIDU; + +/** + * Tell if the target platform is Cocos Runtime based. + */ +export const RUNTIME_BASED = OPPO || VIVO || HUAWEI || COCOSPLAY || LINKSURE || QTT; + +/** + * Tell if the target platform supports JIT. + */ +export const SUPPORT_JIT = !(MINIGAME || RUNTIME_BASED); diff --git a/scripts/build-engine/src/build-time-constants.ts b/scripts/build-engine/src/build-time-constants.ts deleted file mode 100644 index f25e8237ba..0000000000 --- a/scripts/build-engine/src/build-time-constants.ts +++ /dev/null @@ -1,51 +0,0 @@ - -export type IBuildTimeConstantValue = string | number | boolean; - -export type IBuildTimeConstants = Record; - -export function setupBuildTimeConstants ({ - mode, - platform, - flags, -}: { - mode?: string; - platform: string; - flags: Record; -}) { - const buildModeConstantNames = getBuildModeConstantNames(); - const platformConstantNames = getPlatformConstantNames(); - - const result: Record = {}; - platformConstantNames.forEach(name => result[name] = false); - buildModeConstantNames.forEach(name => result[name] = false); - - if (mode) { - if (!buildModeConstantNames.includes(mode)) { - console.warn(`Unknown build mode constant: ${mode}`); - } - result[mode] = true; - } - - if (!platformConstantNames.includes(platform)) { - console.warn(`Unknown platform constant: ${platform}`); - } - result[platform] = true; - - Object.assign(result, flags); - - result.DEV = result.EDITOR || result.PREVIEW || result.TEST; - result.DEBUG = result.DEBUG || result.DEV; - result.RUNTIME_BASED = result.OPPO || result.VIVO || result.HUAWEI || result.COCOSPLAY || result.LINKSURE || result.QTT; - result.MINIGAME = result.WECHAT || result.ALIPAY || result.XIAOMI || result.BYTEDANCE || result.BAIDU; - result.JSB = result.NATIVE; - result.SUPPORT_JIT = !(result.MINIGAME || result.RUNTIME_BASED); - return result; -} - -export function getPlatformConstantNames () { - return ['HTML5', 'WECHAT', 'ALIPAY', 'BAIDU', 'XIAOMI', 'BYTEDANCE', 'OPPO', 'VIVO', 'HUAWEI', 'NATIVE', 'COCOSPLAY', 'LINKSURE', 'QTT']; -} - -export function getBuildModeConstantNames () { - return ['EDITOR', 'PREVIEW', 'BUILD', 'TEST']; -} \ No newline at end of file diff --git a/scripts/build-engine/src/index.ts b/scripts/build-engine/src/index.ts index 40a8f5cf97..c8629aa8f4 100644 --- a/scripts/build-engine/src/index.ts +++ b/scripts/build-engine/src/index.ts @@ -22,7 +22,6 @@ import { ModuleOption, enumerateModuleOptionReps, parseModuleOption } from './mo import { generateCCSource } from './make-cc'; import { getModuleName } from './module-name'; import tsConfigPaths from './ts-paths'; -import { getPlatformConstantNames, IBuildTimeConstants } from './build-time-constants'; import removeDeprecatedFeatures from './remove-deprecated-features'; import realFs from 'fs'; @@ -141,7 +140,10 @@ namespace build { file?: string; }; - buildTimeConstants: IBuildTimeConstants; + /** + * The content of `'internal:raw-constants'`. + */ + rawConstants: Record; } export interface Result { @@ -232,10 +234,7 @@ async function _doBuild ({ ); const rpVirtualOptions: Record = {}; - const vmInternalConstants = getModuleSourceInternalConstants({ - EXPORT_TO_GLOBAL: true, - ...options.buildTimeConstants, - }); + const vmInternalConstants = getModuleSourceInternalConstants(options.rawConstants); console.debug(`Module source "internal-constants":\n${vmInternalConstants}`); rpVirtualOptions['internal:constants'] = vmInternalConstants; @@ -320,9 +319,9 @@ async function _doBuild ({ }; const moduleRedirects: Record = {}; - const platformConstant = getPlatformConstantNames().find((name) => options.buildTimeConstants[name] === true); - if (platformConstant) { - const moduleOverrides = ccConfig.platforms?.[platformConstant]?.moduleOverrides; + const platform = options.rawConstants.platform; + if (typeof platform === 'string') { + const moduleOverrides = ccConfig.platforms?.[platform]?.moduleOverrides; if (moduleOverrides) { for (const [source, override] of Object.entries(moduleOverrides)) { const normalizedSource = makePathEqualityKey(ps.resolve(engineRoot, source)); @@ -571,8 +570,10 @@ function filePathToModuleRequest (path: string) { return path.replace(/\\/g, '\\\\'); } -function getModuleSourceInternalConstants (buildTimeConstants: IBuildTimeConstants) { - return Object.entries(buildTimeConstants).map(([k, v]) => `export const ${k} = ${v};`).join('\n'); +function getModuleSourceInternalConstants (buildTimeConstants: build.Options['rawConstants']) { + return `export default { + ${Object.entries(buildTimeConstants).map(([k, v]) => ` ${k}: ${v},`).join('\n')} + }`; } function moduleOptionsToRollupFormat (moduleOptions: ModuleOption): rollup.ModuleFormat { diff --git a/tests/init.ts b/tests/init.ts index 95c76cf0dd..eb4185aa05 100644 --- a/tests/init.ts +++ b/tests/init.ts @@ -1,7 +1,7 @@ jest.mock( - 'internal:constants', - () => jest.requireActual('../cocos/core/default-constants'), + 'internal:raw-constants', + () => jest.requireActual('./utils/testing-raw-constants'), { virtual: true, }, ); diff --git a/tests/utils/testing-raw-constants.ts b/tests/utils/testing-raw-constants.ts new file mode 100644 index 0000000000..0361624c8e --- /dev/null +++ b/tests/utils/testing-raw-constants.ts @@ -0,0 +1,3 @@ +export default { + TEST: true, +} as typeof import('internal:raw-constants').default; diff --git a/tsconfig.json b/tsconfig.json index 3ac3005f83..3f0f445a30 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,13 +14,16 @@ "isolatedModules": true, "baseUrl": "./", "paths": { + "internal:constants": [ + "cocos/core/utils/internal-constants.ts" + ], "cc.decorator": [ "cocos/core/data/decorators/index.ts" ] }, "types": [ "./@types/globals", - "./@types/consts", + "./@types/raw-constants", "./@types/editor-extends", "./@types/wx", "./@types/global-adapter",