From db67c927c20cc7c4a5f969a7441fbd59dc9db960 Mon Sep 17 00:00:00 2001 From: neverland Date: Sun, 7 Apr 2024 13:10:39 +0800 Subject: [PATCH] fix: skip prenonnect when HTML is disabled (#2011) --- .../core/src/plugins/networkPerformance.ts | 47 +++++++++---------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/packages/core/src/plugins/networkPerformance.ts b/packages/core/src/plugins/networkPerformance.ts index 040e51f97b..af80b65bc1 100644 --- a/packages/core/src/plugins/networkPerformance.ts +++ b/packages/core/src/plugins/networkPerformance.ts @@ -1,36 +1,35 @@ +import { isHtmlDisabled } from '@rsbuild/shared'; import type { RsbuildPlugin } from '../types'; export const pluginNetworkPerformance = (): RsbuildPlugin => ({ name: 'rsbuild:network-performance', setup(api) { - api.modifyBundlerChain( - async (chain, { CHAIN_ID, isServer, isWebWorker, isServiceWorker }) => { - const config = api.getNormalizedConfig(); - const { - performance: { dnsPrefetch, preconnect }, - } = config; + api.modifyBundlerChain(async (chain, { CHAIN_ID, target }) => { + const config = api.getNormalizedConfig(); + const { + performance: { dnsPrefetch, preconnect }, + } = config; - if (isServer || isWebWorker || isServiceWorker) { - return; - } + if (isHtmlDisabled(config, target)) { + return; + } - const { HtmlNetworkPerformancePlugin } = await import( - '../rspack/HtmlNetworkPerformancePlugin' - ); + const { HtmlNetworkPerformancePlugin } = await import( + '../rspack/HtmlNetworkPerformancePlugin' + ); - if (dnsPrefetch) { - chain - .plugin(CHAIN_ID.PLUGIN.HTML_DNS_PREFETCH) - .use(HtmlNetworkPerformancePlugin, [dnsPrefetch, 'dnsPrefetch']); - } + if (dnsPrefetch) { + chain + .plugin(CHAIN_ID.PLUGIN.HTML_DNS_PREFETCH) + .use(HtmlNetworkPerformancePlugin, [dnsPrefetch, 'dnsPrefetch']); + } - if (preconnect) { - chain - .plugin(CHAIN_ID.PLUGIN.HTML_PRECONNECT) - .use(HtmlNetworkPerformancePlugin, [preconnect, 'preconnect']); - } - }, - ); + if (preconnect) { + chain + .plugin(CHAIN_ID.PLUGIN.HTML_PRECONNECT) + .use(HtmlNetworkPerformancePlugin, [preconnect, 'preconnect']); + } + }); }, });