1818
1919import type { Config } from "../types"
2020
21+ /**
22+ * Common interface for bundler plugin constructors.
23+ * Works with both webpack and rspack plugins.
24+ */
25+ export interface PluginConstructor {
26+ new ( ...args : unknown [ ] ) : unknown
27+ }
28+
29+ /**
30+ * Options for CSS extraction plugins (MiniCssExtractPlugin / CssExtractRspackPlugin).
31+ */
32+ export interface CssExtractPluginOptions {
33+ filename ?: string
34+ chunkFilename ?: string
35+ ignoreOrder ?: boolean
36+ insert ?: string | ( ( linkTag : unknown ) => void )
37+ attributes ?: Record < string , string >
38+ linkType ?: string | false
39+ runtime ?: boolean
40+ experimentalUseImportModule ?: boolean
41+ }
42+
43+ /**
44+ * CSS extraction plugin constructor interface.
45+ */
46+ export interface CssExtractPluginConstructor {
47+ new ( options ?: CssExtractPluginOptions ) : unknown
48+ loader : string
49+ }
50+
51+ /**
52+ * Common interface for the bundler module (webpack or @rspack/core).
53+ * Contains the commonly used plugins that exist in both bundlers.
54+ */
55+ export interface BundlerModule {
56+ DefinePlugin : PluginConstructor
57+ EnvironmentPlugin : PluginConstructor
58+ ProvidePlugin : PluginConstructor
59+ HotModuleReplacementPlugin : PluginConstructor
60+ ProgressPlugin : PluginConstructor
61+ [ key : string ] : unknown
62+ }
63+
2164const config = require ( "../config" ) as Config
2265const { requireOrError } = require ( "./requireOrError" )
2366
@@ -46,7 +89,7 @@ const isWebpack: boolean = config.assets_bundler !== "rspack"
4689/**
4790 * Get the current bundler module (webpack or @rspack/core).
4891 *
49- * @returns The bundler module
92+ * @returns The bundler module with common plugin constructors
5093 * @throws {Error } If the required bundler package is not installed
5194 *
5295 * @example
@@ -57,8 +100,10 @@ const isWebpack: boolean = config.assets_bundler !== "rspack"
57100 * new bundler.DefinePlugin({ VERSION: JSON.stringify('1.0.0') })
58101 * new bundler.EnvironmentPlugin(['NODE_ENV'])
59102 */
60- const getBundler = ( ) : unknown =>
61- isRspack ? requireOrError ( "@rspack/core" ) : requireOrError ( "webpack" )
103+ const getBundler = ( ) : BundlerModule =>
104+ ( isRspack
105+ ? requireOrError ( "@rspack/core" )
106+ : requireOrError ( "webpack" ) ) as BundlerModule
62107
63108/**
64109 * Get the CSS extraction plugin for the current bundler.
@@ -81,12 +126,16 @@ const getBundler = (): unknown =>
81126 * ]
82127 * }
83128 */
84- const getCssExtractPlugin = ( ) : unknown => {
129+ const getCssExtractPlugin = ( ) : CssExtractPluginConstructor => {
85130 if ( isRspack ) {
86- const rspack = requireOrError ( "@rspack/core" )
131+ const rspack = requireOrError ( "@rspack/core" ) as {
132+ CssExtractRspackPlugin : CssExtractPluginConstructor
133+ }
87134 return rspack . CssExtractRspackPlugin
88135 }
89- return requireOrError ( "mini-css-extract-plugin" )
136+ return requireOrError (
137+ "mini-css-extract-plugin"
138+ ) as CssExtractPluginConstructor
90139}
91140
92141/**
@@ -136,10 +185,7 @@ const getCssExtractPluginLoader = (): string => {
136185 * ]
137186 * }
138187 */
139- const getDefinePlugin = ( ) : unknown => {
140- const bundler = getBundler ( ) as { DefinePlugin : unknown }
141- return bundler . DefinePlugin
142- }
188+ const getDefinePlugin = ( ) : PluginConstructor => getBundler ( ) . DefinePlugin
143189
144190/**
145191 * Get the EnvironmentPlugin for the current bundler.
@@ -157,10 +203,8 @@ const getDefinePlugin = (): unknown => {
157203 * ]
158204 * }
159205 */
160- const getEnvironmentPlugin = ( ) : unknown => {
161- const bundler = getBundler ( ) as { EnvironmentPlugin : unknown }
162- return bundler . EnvironmentPlugin
163- }
206+ const getEnvironmentPlugin = ( ) : PluginConstructor =>
207+ getBundler ( ) . EnvironmentPlugin
164208
165209/**
166210 * Get the ProvidePlugin for the current bundler.
@@ -181,10 +225,7 @@ const getEnvironmentPlugin = (): unknown => {
181225 * ]
182226 * }
183227 */
184- const getProvidePlugin = ( ) : unknown => {
185- const bundler = getBundler ( ) as { ProvidePlugin : unknown }
186- return bundler . ProvidePlugin
187- }
228+ const getProvidePlugin = ( ) : PluginConstructor => getBundler ( ) . ProvidePlugin
188229
189230export {
190231 isRspack ,
0 commit comments