From e5a175caa44cdf51826b1252a227590c38f1fd14 Mon Sep 17 00:00:00 2001 From: Kuitos Date: Wed, 5 Oct 2022 15:50:57 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20import=20global=20variables=20from?= =?UTF-8?q?=20https://github.com/sindresorhus/globals/blob/main/globals.js?= =?UTF-8?q?on=20(#2288)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .fatherrc.js | 9 ++++++ package.json | 1 + src/sandbox/common.ts | 34 ++++----------------- src/sandbox/globals.ts | 69 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 28 deletions(-) create mode 100644 src/sandbox/globals.ts diff --git a/.fatherrc.js b/.fatherrc.js index c869c21..d9663d3 100644 --- a/.fatherrc.js +++ b/.fatherrc.js @@ -1,11 +1,20 @@ import { writeFileSync } from 'fs'; import { join } from 'path'; import { version } from './package.json'; +import globals from 'globals'; // generate version.ts const versionFilePath = join(__dirname, './src/version.ts'); writeFileSync(versionFilePath, `export const version = '${version}';`); +// generate globals.ts +const globalsFilePath = join(__dirname, './src/sandbox/globals.ts'); +writeFileSync( + globalsFilePath, + `// generated from https://github.com/sindresorhus/globals/blob/main/globals.json builtin part +export const globals = ${JSON.stringify(Object.keys(globals.builtin), null, 2)};`, +); + export default { target: 'browser', esm: 'babel', diff --git a/package.json b/package.json index f02ceca..a9d6dfd 100644 --- a/package.json +++ b/package.json @@ -115,6 +115,7 @@ "cross-env": "^7.0.2", "dumi": "^1.1.0-beta.24", "father-build": "^1.7.0", + "globals": "^13.17.0", "husky": "^2.3.0", "jest": "^25.2.2", "levenary": "^1.1.1", diff --git a/src/sandbox/common.ts b/src/sandbox/common.ts index bacf44b..1e29e67 100644 --- a/src/sandbox/common.ts +++ b/src/sandbox/common.ts @@ -4,6 +4,8 @@ */ import { isBoundedFunction, isCallable, isConstructable } from '../utils'; +import { globals } from './globals'; +import { without } from 'lodash'; type AppInstance = { name: string; window: WindowProxy }; let currentRunningApp: AppInstance | null = null; @@ -20,6 +22,10 @@ export function setCurrentRunningApp(appInstance: { name: string; window: Window currentRunningApp = appInstance; } +const scopedGlobals = ['window', 'self', 'globalThis', 'top', 'parent', 'hasOwnProperty', 'document', 'eval']; +export const unscopedGlobals = [...without(globals, ...scopedGlobals), 'requestAnimationFrame']; +export const lexicalGlobals = [...unscopedGlobals, ...scopedGlobals]; + const functionBoundedValueMap = new WeakMap(); export function getTargetValue(target: any, value: any): any { @@ -27,7 +33,6 @@ export function getTargetValue(target: any, value: any): any { 仅绑定 isCallable && !isBoundedFunction && !isConstructable 的函数对象,如 window.console、window.atob 这类,不然微应用中调用时会抛出 Illegal invocation 异常 目前没有完美的检测方式,这里通过 prototype 中是否还有可枚举的拓展方法的方式来判断 @warning 这里不要随意替换成别的判断方式,因为可能触发一些 edge case(比如在 lodash.isFunction 在 iframe 上下文中可能由于调用了 top window 对象触发的安全异常) - @warning 对于configurable及writable都为false的readonly属性,proxy必须返回原值 */ if (isCallable(value) && !isBoundedFunction(value) && !isConstructable(value)) { const cachedBoundFunction = functionBoundedValueMap.get(value); @@ -79,30 +84,3 @@ export function getTargetValue(target: any, value: any): any { return value; } - -export const unscopedGlobals = [ - 'undefined', - 'Array', - 'Object', - 'String', - 'Boolean', - 'Math', - 'Number', - 'Symbol', - 'parseFloat', - 'Float32Array', - 'isNaN', - 'Infinity', - 'Reflect', - 'Float64Array', - 'Function', - 'Map', - 'NaN', - 'Promise', - 'Proxy', - 'Set', - 'parseInt', - 'requestAnimationFrame', -]; - -export const lexicalGlobals = [...unscopedGlobals, 'globalThis', 'window', 'self']; diff --git a/src/sandbox/globals.ts b/src/sandbox/globals.ts new file mode 100644 index 0000000..0ed743c --- /dev/null +++ b/src/sandbox/globals.ts @@ -0,0 +1,69 @@ +// generated from https://github.com/sindresorhus/globals/blob/main/globals.json builtin part +export const globals = [ + 'AggregateError', + 'Array', + 'ArrayBuffer', + 'Atomics', + 'BigInt', + 'BigInt64Array', + 'BigUint64Array', + 'Boolean', + 'constructor', + 'DataView', + 'Date', + 'decodeURI', + 'decodeURIComponent', + 'encodeURI', + 'encodeURIComponent', + 'Error', + 'escape', + 'eval', + 'EvalError', + 'FinalizationRegistry', + 'Float32Array', + 'Float64Array', + 'Function', + 'globalThis', + 'hasOwnProperty', + 'Infinity', + 'Int16Array', + 'Int32Array', + 'Int8Array', + 'isFinite', + 'isNaN', + 'isPrototypeOf', + 'JSON', + 'Map', + 'Math', + 'NaN', + 'Number', + 'Object', + 'parseFloat', + 'parseInt', + 'Promise', + 'propertyIsEnumerable', + 'Proxy', + 'RangeError', + 'ReferenceError', + 'Reflect', + 'RegExp', + 'Set', + 'SharedArrayBuffer', + 'String', + 'Symbol', + 'SyntaxError', + 'toLocaleString', + 'toString', + 'TypeError', + 'Uint16Array', + 'Uint32Array', + 'Uint8Array', + 'Uint8ClampedArray', + 'undefined', + 'unescape', + 'URIError', + 'valueOf', + 'WeakMap', + 'WeakRef', + 'WeakSet', +];