🐛 use const instead of function parameters to cache global variables while speedy mode running (#2447)

This commit is contained in:
Kuitos 2023-03-29 18:37:17 +08:00 committed by GitHub
parent bb40e63089
commit 601696ad7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 12 deletions

View File

@ -102,7 +102,7 @@
}, },
"dependencies": { "dependencies": {
"@babel/runtime": "^7.10.5", "@babel/runtime": "^7.10.5",
"import-html-entry": "^1.14.3", "import-html-entry": "^1.14.5",
"lodash": "^4.17.11", "lodash": "^4.17.11",
"single-spa": "^5.9.2" "single-spa": "^5.9.2"
}, },

View File

@ -12,7 +12,7 @@ import type {
import type { ParcelConfigObjectGetter } from './loader'; import type { ParcelConfigObjectGetter } from './loader';
import { loadApp } from './loader'; import { loadApp } from './loader';
import { doPrefetchStrategy } from './prefetch'; import { doPrefetchStrategy } from './prefetch';
import { Deferred, getContainerXPath, toArray } from './utils'; import { Deferred, getContainerXPath, isConstDestructAssignmentSupported, toArray } from './utils';
let microApps: Array<RegistrableApp<Record<string, unknown>>> = []; let microApps: Array<RegistrableApp<Record<string, unknown>>> = [];
@ -24,10 +24,10 @@ const defaultUrlRerouteOnly = true;
const frameworkStartedDefer = new Deferred<void>(); const frameworkStartedDefer = new Deferred<void>();
const autoDowngradeForLowVersionBrowser = (configuration: FrameworkConfiguration): FrameworkConfiguration => { const autoDowngradeForLowVersionBrowser = (configuration: FrameworkConfiguration): FrameworkConfiguration => {
const { sandbox, singular } = configuration; const { sandbox = true, singular } = configuration;
if (sandbox) { if (sandbox) {
if (!window.Proxy) { if (!window.Proxy) {
console.warn('[qiankun] Miss window.Proxy, proxySandbox will degenerate into snapshotSandbox'); console.warn('[qiankun] Missing window.Proxy, proxySandbox will degenerate into snapshotSandbox');
if (singular === false) { if (singular === false) {
console.warn( console.warn(
@ -37,6 +37,20 @@ const autoDowngradeForLowVersionBrowser = (configuration: FrameworkConfiguration
return { ...configuration, sandbox: typeof sandbox === 'object' ? { ...sandbox, loose: true } : { loose: true } }; return { ...configuration, sandbox: typeof sandbox === 'object' ? { ...sandbox, loose: true } : { loose: true } };
} }
if (
!isConstDestructAssignmentSupported() &&
(sandbox === true || (typeof sandbox === 'object' && sandbox.speedy !== false))
) {
console.warn(
'[qiankun] Speedy mode will turn off as const destruct assignment not supported in current browser!',
);
return {
...configuration,
sandbox: typeof sandbox === 'object' ? { ...sandbox, speedy: false } : { speedy: false },
};
}
} }
return configuration; return configuration;
@ -195,13 +209,7 @@ export function loadMicroApp<T extends ObjectType>(
export function start(opts: FrameworkConfiguration = {}) { export function start(opts: FrameworkConfiguration = {}) {
frameworkConfiguration = { prefetch: true, singular: true, sandbox: true, ...opts }; frameworkConfiguration = { prefetch: true, singular: true, sandbox: true, ...opts };
const { const { prefetch, urlRerouteOnly = defaultUrlRerouteOnly, ...importEntryOpts } = frameworkConfiguration;
prefetch,
sandbox,
singular,
urlRerouteOnly = defaultUrlRerouteOnly,
...importEntryOpts
} = frameworkConfiguration;
if (prefetch) { if (prefetch) {
doPrefetchStrategy(microApps, prefetch, importEntryOpts); doPrefetchStrategy(microApps, prefetch, importEntryOpts);

View File

@ -3,7 +3,7 @@
* @since 2019-05-15 * @since 2019-05-15
*/ */
import { isFunction, once, snakeCase } from 'lodash'; import { isFunction, once, snakeCase, memoize } from 'lodash';
import type { FrameworkConfiguration } from './interfaces'; import type { FrameworkConfiguration } from './interfaces';
import { version } from './version'; import { version } from './version';
@ -131,6 +131,15 @@ export function isBoundedFunction(fn: CallableFunction) {
return bounded; return bounded;
} }
export const isConstDestructAssignmentSupported = memoize(() => {
try {
new Function('const { a } = { a: 1 }')();
return true;
} catch (e) {
return false;
}
});
export const qiankunHeadTagName = 'qiankun-head'; export const qiankunHeadTagName = 'qiankun-head';
export function getDefaultTplWrapper(name: string, sandboxOpts: FrameworkConfiguration['sandbox']) { export function getDefaultTplWrapper(name: string, sandboxOpts: FrameworkConfiguration['sandbox']) {