Skip to content

Commit

Permalink
✨ import global variables from https://github.com/sindresorhus/global…
Browse files Browse the repository at this point in the history
  • Loading branch information
kuitos authored Oct 5, 2022
1 parent edd19b1 commit e5a175c
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 28 deletions.
9 changes: 9 additions & 0 deletions .fatherrc.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 6 additions & 28 deletions src/sandbox/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,14 +22,17 @@ 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 {
/*
仅绑定 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);
Expand Down Expand Up @@ -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 changes: 69 additions & 0 deletions src/sandbox/globals.ts
Original file line number Diff line number Diff line change
@@ -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',
];

1 comment on commit e5a175c

@vercel
Copy link

@vercel vercel bot commented on e5a175c Oct 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.