✨ import global variables from https://github.com/sindresorhus/globals/blob/main/globals.json (#2288)
This commit is contained in:
parent
edd19b11b9
commit
e5a175caa4
|
|
@ -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',
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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<CallableFunction, CallableFunction>();
|
||||
|
||||
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'];
|
||||
|
|
|
|||
69
src/sandbox/globals.ts
Normal file
69
src/sandbox/globals.ts
Normal file
|
|
@ -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',
|
||||
];
|
||||
Loading…
Reference in New Issue
Block a user