From 80841f0f2e007c09ded586a975ee4480e5998d73 Mon Sep 17 00:00:00 2001 From: neverland Date: Sun, 5 Jan 2025 17:21:16 +0800 Subject: [PATCH] perf(assets-retry): reduce nullish coalescing operator --- .../src/runtime/asyncChunkRetry.ts | 35 ++++++++----------- .../src/runtime/initialChunkRetry.ts | 20 +++++------ 2 files changed, 25 insertions(+), 30 deletions(-) diff --git a/packages/plugin-assets-retry/src/runtime/asyncChunkRetry.ts b/packages/plugin-assets-retry/src/runtime/asyncChunkRetry.ts index 07aa4d4197..2c9419911d 100644 --- a/packages/plugin-assets-retry/src/runtime/asyncChunkRetry.ts +++ b/packages/plugin-assets-retry/src/runtime/asyncChunkRetry.ts @@ -60,11 +60,11 @@ const retryCollector: RetryCollector = {}; const retryCssCollector: RetryCollector = {}; function findCurrentDomain(url: string) { - const domainList = config.domain ?? []; + const domains = config.domain || []; let domain = ''; - for (let i = 0; i < domainList.length; i++) { - if (url.indexOf(domainList[i]) !== -1) { - domain = domainList[i]; + for (let i = 0; i < domains.length; i++) { + if (url.indexOf(domains[i]) !== -1) { + domain = domains[i]; break; } } @@ -72,10 +72,10 @@ function findCurrentDomain(url: string) { } function findNextDomain(url: string) { - const domainList = config.domain ?? []; + const domains = config.domain || []; const currentDomain = findCurrentDomain(url); - const index = domainList.indexOf(currentDomain); - return domainList[(index + 1) % domainList.length] || url; + const index = domains.indexOf(currentDomain); + return domains[(index + 1) % domains.length] || url; } const postfixRE = /[?#].*$/; @@ -146,7 +146,7 @@ function initRetry(chunkId: string, isCssAsyncChunk: boolean): Retry { const originalQuery = getQueryFromUrl(originalSrcUrl); const existRetryTimes = 0; - const nextDomain = config.domain?.[0] ?? window.origin; + const nextDomain = config.domain?.[0] || window.origin; return { nextDomain, @@ -215,8 +215,8 @@ const originalEnsureChunk = __RUNTIME_GLOBALS_ENSURE_CHUNK__; const originalGetChunkScriptFilename = __RUNTIME_GLOBALS_GET_CHUNK_SCRIPT_FILENAME__; const originalGetCssFilename = - __RUNTIME_GLOBALS_GET_MINI_CSS_EXTRACT_FILENAME__ ?? - __RUNTIME_GLOBALS_GET_CSS_FILENAME__ ?? + __RUNTIME_GLOBALS_GET_MINI_CSS_EXTRACT_FILENAME__ || + __RUNTIME_GLOBALS_GET_CSS_FILENAME__ || (() => null); const originalLoadScript = __RUNTIME_GLOBALS_LOAD_SCRIPT__; @@ -335,11 +335,7 @@ function ensureChunk(chunkId: string): Promise { } } - if ( - config.domain && - config.domain.length > 0 && - config.domain.indexOf(nextDomain) === -1 - ) { + if (config.domain && config.domain.indexOf(nextDomain) === -1) { throw error; } @@ -376,11 +372,10 @@ function loadScript() { function loadStyleSheet(href: string, chunkId: ChunkId): string { const retry = globalCurrRetryingCss[chunkId]; - if (retry?.nextRetryUrl) { - return retry.nextRetryUrl; - } - - return __RUNTIME_GLOBALS_PUBLIC_PATH__ + href; + return ( + // biome-ignore lint/complexity/useOptionalChain: for less code + (retry && retry.nextRetryUrl) || __RUNTIME_GLOBALS_PUBLIC_PATH__ + href + ); } function registerAsyncChunkRetry() { diff --git a/packages/plugin-assets-retry/src/runtime/initialChunkRetry.ts b/packages/plugin-assets-retry/src/runtime/initialChunkRetry.ts index c89dfd2ef6..0c02ab1375 100644 --- a/packages/plugin-assets-retry/src/runtime/initialChunkRetry.ts +++ b/packages/plugin-assets-retry/src/runtime/initialChunkRetry.ts @@ -20,11 +20,11 @@ declare global { } // this function is the same as async chunk retry -function findCurrentDomain(url: string, domainList: string[]) { +function findCurrentDomain(url: string, domains: string[]) { let domain = ''; - for (let i = 0; i < domainList.length; i++) { - if (url.indexOf(domainList[i]) !== -1) { - domain = domainList[i]; + for (let i = 0; i < domains.length; i++) { + if (url.indexOf(domains[i]) !== -1) { + domain = domains[i]; break; } } @@ -32,10 +32,10 @@ function findCurrentDomain(url: string, domainList: string[]) { } // this function is the same as async chunk retry -function findNextDomain(url: string, domainList: string[]) { - const currentDomain = findCurrentDomain(url, domainList); - const index = domainList.indexOf(currentDomain); - return domainList[(index + 1) % domainList.length] || url; +function findNextDomain(url: string, domains: string[]) { + const currentDomain = findCurrentDomain(url, domains); + const index = domains.indexOf(currentDomain); + return domains[(index + 1) % domains.length] || url; } function getRequestUrl(element: HTMLElement) { @@ -56,7 +56,7 @@ function validateTargetInfo( e: Event, ): { target: HTMLElement; tagName: string; url: string } | false { const target: HTMLElement = e.target as HTMLElement; - const tagName = target.tagName?.toLocaleLowerCase(); + const tagName = target.tagName.toLocaleLowerCase(); const allowTags = config.type!; const url = getRequestUrl(target); if ( @@ -237,7 +237,7 @@ function retry(config: RuntimeRetryOptions, e: Event) { // if the initial request is "/static/js/async/src_Hello_tsx.js?q=1", retry url would be "/static/js/async/src_Hello_tsx.js?q=1&retry=1" const originalQuery = - target.dataset.rsbuildOriginalQuery ?? getQueryFromUrl(url); + target.dataset.rsbuildOriginalQuery || getQueryFromUrl(url); // this function is the same as async chunk retry function getUrlRetryQuery(existRetryTimes: number): string {