2023-04-07 16:02:47 +00:00
|
|
|
import type { Configuration as WebpackConfig, WebpackPluginInstance } from 'webpack'
|
|
|
|
import type { UserConfig as ViteConfig, Plugin as VitePlugin } from 'vite'
|
2021-11-21 16:14:46 +00:00
|
|
|
import { useNuxt } from './context'
|
|
|
|
|
|
|
|
export interface ExtendConfigOptions {
|
|
|
|
/**
|
|
|
|
* Install plugin on dev
|
|
|
|
*
|
|
|
|
* @default true
|
|
|
|
*/
|
2023-03-29 10:59:57 +00:00
|
|
|
dev?: boolean
|
|
|
|
/**
|
|
|
|
* Install plugin on build
|
|
|
|
*
|
|
|
|
* @default true
|
|
|
|
*/
|
|
|
|
build?: boolean
|
2021-11-21 16:14:46 +00:00
|
|
|
/**
|
|
|
|
* Install plugin on server side
|
|
|
|
*
|
|
|
|
* @default true
|
|
|
|
*/
|
|
|
|
server?: boolean
|
|
|
|
/**
|
|
|
|
* Install plugin on client side
|
|
|
|
*
|
|
|
|
* @default true
|
|
|
|
*/
|
|
|
|
client?: boolean
|
2023-04-29 22:39:08 +00:00
|
|
|
/**
|
|
|
|
* Prepends the plugin to the array with `unshit()` instead of `push()`.
|
|
|
|
*/
|
|
|
|
prepend?: boolean
|
2022-06-22 17:29:51 +00:00
|
|
|
}
|
|
|
|
|
2023-04-29 22:39:08 +00:00
|
|
|
export interface ExtendWebpackConfigOptions extends ExtendConfigOptions {}
|
2021-11-21 16:14:46 +00:00
|
|
|
|
|
|
|
export interface ExtendViteConfigOptions extends ExtendConfigOptions {}
|
|
|
|
|
|
|
|
/**
|
2023-01-13 15:00:57 +00:00
|
|
|
* Extend webpack config
|
2021-11-21 16:14:46 +00:00
|
|
|
*
|
|
|
|
* The fallback function might be called multiple times
|
|
|
|
* when applying to both client and server builds.
|
|
|
|
*/
|
|
|
|
export function extendWebpackConfig (
|
2023-03-29 10:59:57 +00:00
|
|
|
fn: ((config: WebpackConfig) => void),
|
2021-11-21 16:14:46 +00:00
|
|
|
options: ExtendWebpackConfigOptions = {}
|
|
|
|
) {
|
|
|
|
const nuxt = useNuxt()
|
|
|
|
|
|
|
|
if (options.dev === false && nuxt.options.dev) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (options.build === false && nuxt.options.build) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
nuxt.hook('webpack:config', (configs: WebpackConfig[]) => {
|
|
|
|
if (options.server !== false) {
|
|
|
|
const config = configs.find(i => i.name === 'server')
|
|
|
|
if (config) {
|
|
|
|
fn(config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (options.client !== false) {
|
|
|
|
const config = configs.find(i => i.name === 'client')
|
|
|
|
if (config) {
|
|
|
|
fn(config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extend Vite config
|
|
|
|
*/
|
|
|
|
export function extendViteConfig (
|
|
|
|
fn: ((config: ViteConfig) => void),
|
|
|
|
options: ExtendViteConfigOptions = {}
|
|
|
|
) {
|
|
|
|
const nuxt = useNuxt()
|
|
|
|
|
|
|
|
if (options.dev === false && nuxt.options.dev) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (options.build === false && nuxt.options.build) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-15 21:34:29 +00:00
|
|
|
if (options.server !== false && options.client !== false) {
|
|
|
|
// Call fn() only once
|
|
|
|
return nuxt.hook('vite:extend', ({ config }) => fn(config))
|
|
|
|
}
|
|
|
|
|
2022-06-22 17:29:51 +00:00
|
|
|
nuxt.hook('vite:extendConfig', (config, { isClient, isServer }) => {
|
|
|
|
if (options.server !== false && isServer) {
|
|
|
|
return fn(config)
|
|
|
|
}
|
|
|
|
if (options.client !== false && isClient) {
|
|
|
|
return fn(config)
|
|
|
|
}
|
|
|
|
})
|
2021-11-21 16:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-13 15:00:57 +00:00
|
|
|
* Append webpack plugin to the config.
|
2021-11-21 16:14:46 +00:00
|
|
|
*/
|
2022-10-18 09:09:11 +00:00
|
|
|
export function addWebpackPlugin (plugin: WebpackPluginInstance | WebpackPluginInstance[], options?: ExtendWebpackConfigOptions) {
|
2023-04-29 22:39:08 +00:00
|
|
|
const method: 'push' | 'unshift' = options?.prepend ? 'unshift' : 'push'
|
2021-11-21 16:14:46 +00:00
|
|
|
extendWebpackConfig((config) => {
|
|
|
|
config.plugins = config.plugins || []
|
2022-10-18 09:09:11 +00:00
|
|
|
if (Array.isArray(plugin)) {
|
2023-04-29 22:39:08 +00:00
|
|
|
config.plugins[method](...plugin)
|
2022-10-18 09:09:11 +00:00
|
|
|
} else {
|
2023-04-29 22:39:08 +00:00
|
|
|
config.plugins[method](plugin)
|
2022-10-18 09:09:11 +00:00
|
|
|
}
|
2021-11-21 16:14:46 +00:00
|
|
|
}, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append Vite plugin to the config.
|
|
|
|
*/
|
2022-10-18 09:09:11 +00:00
|
|
|
export function addVitePlugin (plugin: VitePlugin | VitePlugin[], options?: ExtendViteConfigOptions) {
|
2023-04-29 22:39:08 +00:00
|
|
|
const method: 'push' | 'unshift' = options?.prepend ? 'unshift' : 'push'
|
2021-11-21 16:14:46 +00:00
|
|
|
extendViteConfig((config) => {
|
|
|
|
config.plugins = config.plugins || []
|
2022-10-18 09:09:11 +00:00
|
|
|
if (Array.isArray(plugin)) {
|
2023-04-29 22:39:08 +00:00
|
|
|
config.plugins[method](...plugin)
|
2022-10-18 09:09:11 +00:00
|
|
|
} else {
|
2023-04-29 22:39:08 +00:00
|
|
|
config.plugins[method](plugin)
|
2022-10-18 09:09:11 +00:00
|
|
|
}
|
2021-11-21 16:14:46 +00:00
|
|
|
}, options)
|
|
|
|
}
|