feat(kit): allow vite and webpack plugins to be prepended (#20403)

Co-authored-by: Eduardo San Martin Morote <posva13@gmail.com>
This commit is contained in:
Daniel Roe 2023-04-29 23:39:08 +01:00 committed by GitHub
parent 6ef2e1caea
commit b2a1f9f339
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -27,10 +27,13 @@ export interface ExtendConfigOptions {
* @default true * @default true
*/ */
client?: boolean client?: boolean
/**
* Prepends the plugin to the array with `unshit()` instead of `push()`.
*/
prepend?: boolean
} }
export interface ExtendWebpackConfigOptions extends ExtendConfigOptions { export interface ExtendWebpackConfigOptions extends ExtendConfigOptions {}
}
export interface ExtendViteConfigOptions extends ExtendConfigOptions {} export interface ExtendViteConfigOptions extends ExtendConfigOptions {}
@ -104,12 +107,13 @@ export function extendViteConfig (
* Append webpack plugin to the config. * Append webpack plugin to the config.
*/ */
export function addWebpackPlugin (plugin: WebpackPluginInstance | WebpackPluginInstance[], options?: ExtendWebpackConfigOptions) { export function addWebpackPlugin (plugin: WebpackPluginInstance | WebpackPluginInstance[], options?: ExtendWebpackConfigOptions) {
const method: 'push' | 'unshift' = options?.prepend ? 'unshift' : 'push'
extendWebpackConfig((config) => { extendWebpackConfig((config) => {
config.plugins = config.plugins || [] config.plugins = config.plugins || []
if (Array.isArray(plugin)) { if (Array.isArray(plugin)) {
config.plugins.push(...plugin) config.plugins[method](...plugin)
} else { } else {
config.plugins.push(plugin) config.plugins[method](plugin)
} }
}, options) }, options)
} }
@ -118,12 +122,13 @@ export function addWebpackPlugin (plugin: WebpackPluginInstance | WebpackPluginI
* Append Vite plugin to the config. * Append Vite plugin to the config.
*/ */
export function addVitePlugin (plugin: VitePlugin | VitePlugin[], options?: ExtendViteConfigOptions) { export function addVitePlugin (plugin: VitePlugin | VitePlugin[], options?: ExtendViteConfigOptions) {
const method: 'push' | 'unshift' = options?.prepend ? 'unshift' : 'push'
extendViteConfig((config) => { extendViteConfig((config) => {
config.plugins = config.plugins || [] config.plugins = config.plugins || []
if (Array.isArray(plugin)) { if (Array.isArray(plugin)) {
config.plugins.push(...plugin) config.plugins[method](...plugin)
} else { } else {
config.plugins.push(plugin) config.plugins[method](plugin)
} }
}, options) }, options)
} }