diff --git a/src/core/assets/asset-handler/assets/javascript.ts b/src/core/assets/asset-handler/assets/javascript.ts index 85bcd8585..280ce8ba0 100644 --- a/src/core/assets/asset-handler/assets/javascript.ts +++ b/src/core/assets/asset-handler/assets/javascript.ts @@ -1,6 +1,7 @@ import { Asset, VirtualAsset } from '@cocos/asset-db'; import { readFile } from 'fs-extra'; import { transformPluginScript } from './utils/script-compiler'; +import { resolveSimulatedGlobals } from './utils/plugin-script-globals'; import { openCode } from '../utils'; import { AssetHandlerBase } from '../../@types/protected'; import { JavaScriptAssetUserData, PluginScriptUserData } from '../../@types/userDatas'; @@ -102,7 +103,7 @@ async function _importPluginScript(asset: Asset) { return true; } - const simulateGlobalNames: string[] = simulateGlobals === undefined ? ['self', 'window', 'global', 'globalThis'] : simulateGlobals; + const simulateGlobalNames = resolveSimulatedGlobals(simulateGlobals); const transformed = await transformPluginScript(code, { simulateGlobals: simulateGlobalNames, diff --git a/src/core/assets/asset-handler/assets/utils/plugin-script-globals.ts b/src/core/assets/asset-handler/assets/utils/plugin-script-globals.ts new file mode 100644 index 000000000..540e3abae --- /dev/null +++ b/src/core/assets/asset-handler/assets/utils/plugin-script-globals.ts @@ -0,0 +1,17 @@ +/** Default aliases made available to enclosed plugin scripts. */ +export const DEFAULT_SIMULATED_GLOBALS = ['self', 'window', 'global', 'globalThis'] as const; + +/** + * Resolves persisted plugin aliases while tolerating metadata written by older PinK versions. + */ +export function resolveSimulatedGlobals(value: unknown): string[] { + if (value === false || (Array.isArray(value) && value.length === 0)) { + return []; + } + + const customGlobals = Array.isArray(value) ? value : []; + return Array.from(new Set([ + ...DEFAULT_SIMULATED_GLOBALS, + ...customGlobals, + ])); +} diff --git a/src/core/assets/test/plugin-script-globals.test.ts b/src/core/assets/test/plugin-script-globals.test.ts new file mode 100644 index 000000000..f4d4f3345 --- /dev/null +++ b/src/core/assets/test/plugin-script-globals.test.ts @@ -0,0 +1,33 @@ +import { DEFAULT_SIMULATED_GLOBALS, resolveSimulatedGlobals } from '../asset-handler/assets/utils/plugin-script-globals'; + +describe('plugin script globals', () => { + test('uses the default aliases when no custom aliases are stored', () => { + expect(resolveSimulatedGlobals(undefined)).toEqual(DEFAULT_SIMULATED_GLOBALS); + }); + + test('appends custom aliases after the defaults', () => { + expect(resolveSimulatedGlobals(['PINK_PLUGIN_ALIAS'])).toEqual([ + ...DEFAULT_SIMULATED_GLOBALS, + 'PINK_PLUGIN_ALIAS', + ]); + }); + + test('treats legacy boolean metadata as no custom aliases', () => { + expect(resolveSimulatedGlobals(true)).toEqual(DEFAULT_SIMULATED_GLOBALS); + }); + + test('preserves an empty alias array as disabled aliases', () => { + expect(resolveSimulatedGlobals([])).toEqual([]); + }); + + test('preserves legacy false metadata as disabled aliases', () => { + expect(resolveSimulatedGlobals(false)).toEqual([]); + }); + + test('deduplicates default and custom aliases', () => { + expect(resolveSimulatedGlobals(['window', 'PINK_PLUGIN_ALIAS', 'PINK_PLUGIN_ALIAS'])).toEqual([ + ...DEFAULT_SIMULATED_GLOBALS, + 'PINK_PLUGIN_ALIAS', + ]); + }); +});