From 601696ad7fd84a46738457e3beb757bed18c662b Mon Sep 17 00:00:00 2001 From: Kuitos Date: Wed, 29 Mar 2023 18:37:17 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20use=20const=20instead=20of=20fun?= =?UTF-8?q?ction=20parameters=20to=20cache=20global=20variables=20while=20?= =?UTF-8?q?speedy=20mode=20running=20(#2447)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/apis.ts | 28 ++++++++++++++++++---------- src/utils.ts | 11 ++++++++++- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index da10be1..e62a605 100644 --- a/package.json +++ b/package.json @@ -102,7 +102,7 @@ }, "dependencies": { "@babel/runtime": "^7.10.5", - "import-html-entry": "^1.14.3", + "import-html-entry": "^1.14.5", "lodash": "^4.17.11", "single-spa": "^5.9.2" }, diff --git a/src/apis.ts b/src/apis.ts index e3758ba..f4cdf44 100644 --- a/src/apis.ts +++ b/src/apis.ts @@ -12,7 +12,7 @@ import type { import type { ParcelConfigObjectGetter } from './loader'; import { loadApp } from './loader'; import { doPrefetchStrategy } from './prefetch'; -import { Deferred, getContainerXPath, toArray } from './utils'; +import { Deferred, getContainerXPath, isConstDestructAssignmentSupported, toArray } from './utils'; let microApps: Array>> = []; @@ -24,10 +24,10 @@ const defaultUrlRerouteOnly = true; const frameworkStartedDefer = new Deferred(); const autoDowngradeForLowVersionBrowser = (configuration: FrameworkConfiguration): FrameworkConfiguration => { - const { sandbox, singular } = configuration; + const { sandbox = true, singular } = configuration; if (sandbox) { 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) { console.warn( @@ -37,6 +37,20 @@ const autoDowngradeForLowVersionBrowser = (configuration: FrameworkConfiguration 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; @@ -195,13 +209,7 @@ export function loadMicroApp( export function start(opts: FrameworkConfiguration = {}) { frameworkConfiguration = { prefetch: true, singular: true, sandbox: true, ...opts }; - const { - prefetch, - sandbox, - singular, - urlRerouteOnly = defaultUrlRerouteOnly, - ...importEntryOpts - } = frameworkConfiguration; + const { prefetch, urlRerouteOnly = defaultUrlRerouteOnly, ...importEntryOpts } = frameworkConfiguration; if (prefetch) { doPrefetchStrategy(microApps, prefetch, importEntryOpts); diff --git a/src/utils.ts b/src/utils.ts index a8da78c..0da6841 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -3,7 +3,7 @@ * @since 2019-05-15 */ -import { isFunction, once, snakeCase } from 'lodash'; +import { isFunction, once, snakeCase, memoize } from 'lodash'; import type { FrameworkConfiguration } from './interfaces'; import { version } from './version'; @@ -131,6 +131,15 @@ export function isBoundedFunction(fn: CallableFunction) { 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 function getDefaultTplWrapper(name: string, sandboxOpts: FrameworkConfiguration['sandbox']) {