From b4b078d48ca34d271bcdfcee23a467ebffd41a46 Mon Sep 17 00:00:00 2001 From: matteo Date: Sun, 8 Sep 2024 05:17:21 +0200 Subject: [PATCH] feat(rspack): add the ability to not get the default html build plugin --- packages/rspack/src/utils/with-react.ts | 6 +++-- packages/rspack/src/utils/with-web.ts | 31 ++++++++++++++++++------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/packages/rspack/src/utils/with-react.ts b/packages/rspack/src/utils/with-react.ts index 6afd0974c..b3d0dc9cd 100644 --- a/packages/rspack/src/utils/with-react.ts +++ b/packages/rspack/src/utils/with-react.ts @@ -1,8 +1,10 @@ import { Configuration } from '@rspack/core'; import { SharedConfigContext } from './model'; -import { withWeb } from './with-web'; +import { withWeb, WithWebOptions } from './with-web'; -export function withReact(opts = {}) { +export type WithReactOptions = Omit; + +export function withReact(opts:WithWebOptions = {}) { return function makeConfig( config: Configuration, { options, context }: SharedConfigContext diff --git a/packages/rspack/src/utils/with-web.ts b/packages/rspack/src/utils/with-web.ts index 244984ad4..dcb32f03c 100644 --- a/packages/rspack/src/utils/with-web.ts +++ b/packages/rspack/src/utils/with-web.ts @@ -7,6 +7,7 @@ export interface WithWebOptions { includePaths?: string[]; }; cssModules?: boolean; + htmlPlugins?: false | Configuration['plugins']; } export function withWeb(opts: WithWebOptions = {}) { @@ -51,9 +52,11 @@ export function withWeb(opts: WithWebOptions = {}) { { test: /\.css$/, type: 'css', - use: [{ - loader: require.resolve('postcss-loader'), - }] + use: [ + { + loader: require.resolve('postcss-loader'), + }, + ], }, { test: /\.scss$|\.sass$/, @@ -107,14 +110,24 @@ export function withWeb(opts: WithWebOptions = {}) { }, plugins: [ ...config.plugins, - new rspack.HtmlRspackPlugin({ - template: options.indexHtml - ? path.join(context.root, options.indexHtml) - : path.join(projectRoot, 'src/index.html'), - }), + ...(() => { + if (opts.htmlPlugins === false) { + return []; + } + if (opts.htmlPlugins) { + return opts.htmlPlugins; + } + return [ + new rspack.HtmlRspackPlugin({ + template: options.indexHtml + ? path.join(context.root, options.indexHtml) + : path.join(projectRoot, 'src/index.html'), + }), + ]; + })(), new rspack.DefinePlugin({ 'process.env.NODE_ENV': isProd ? "'production'" : "'development'", - }) + }), ], }; };