mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-26 07:32:01 +00:00
feat(kit): extendWebpackConfig
and extendViteConfig
utils (#386)
This commit is contained in:
parent
57a2974450
commit
0e93d752fd
@ -3,7 +3,7 @@ import path, { basename, parse } from 'upath'
|
|||||||
import hash from 'hash-sum'
|
import hash from 'hash-sum'
|
||||||
import consola from 'consola'
|
import consola from 'consola'
|
||||||
import type { WebpackPluginInstance, Configuration as WebpackConfig } from 'webpack'
|
import type { WebpackPluginInstance, Configuration as WebpackConfig } from 'webpack'
|
||||||
import type { Plugin as VitePlugin } from 'vite'
|
import type { Plugin as VitePlugin, UserConfig as ViteConfig } from 'vite'
|
||||||
import { useNuxt } from '../nuxt'
|
import { useNuxt } from '../nuxt'
|
||||||
import { chainFn } from '../utils/task'
|
import { chainFn } from '../utils/task'
|
||||||
import type { TemplateOpts, PluginTemplateOpts } from '../types/module'
|
import type { TemplateOpts, PluginTemplateOpts } from '../types/module'
|
||||||
@ -122,6 +122,8 @@ export function addServerMiddleware (middleware) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Allows extending webpack build config by chaining `options.build.extend` function.
|
* Allows extending webpack build config by chaining `options.build.extend` function.
|
||||||
|
*
|
||||||
|
* @deprecated use extendWebpackConfig() instead
|
||||||
*/
|
*/
|
||||||
export function extendBuild (fn) {
|
export function extendBuild (fn) {
|
||||||
const nuxt = useNuxt()
|
const nuxt = useNuxt()
|
||||||
@ -139,7 +141,7 @@ export function extendRoutes (fn) {
|
|||||||
nuxt.options.router.extendRoutes = chainFn(nuxt.options.router.extendRoutes, fn)
|
nuxt.options.router.extendRoutes = chainFn(nuxt.options.router.extendRoutes, fn)
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AddPluginHookOptions {
|
export interface ExtendConfigOptions {
|
||||||
/**
|
/**
|
||||||
* Install plugin on dev
|
* Install plugin on dev
|
||||||
*
|
*
|
||||||
@ -154,7 +156,7 @@ export interface AddPluginHookOptions {
|
|||||||
build?: boolean
|
build?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AddWebpackPluginHookOptions extends AddPluginHookOptions {
|
export interface ExtendWebpackConfigOptions extends ExtendConfigOptions {
|
||||||
/**
|
/**
|
||||||
* Install plugin on server side
|
* Install plugin on server side
|
||||||
*
|
*
|
||||||
@ -169,13 +171,18 @@ export interface AddWebpackPluginHookOptions extends AddPluginHookOptions {
|
|||||||
client?: boolean
|
client?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AddVitePluginHookOptions extends AddPluginHookOptions {
|
export interface ExtendViteConfigOptions extends ExtendConfigOptions {}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Append Webpack plugin to the config.
|
* Extend Webpack config
|
||||||
|
*
|
||||||
|
* The fallback function might be called multiple times
|
||||||
|
* when applying to both client and server builds.
|
||||||
*/
|
*/
|
||||||
export function addWebpackPlugin (plugin: WebpackPluginInstance, options: AddWebpackPluginHookOptions = {}) {
|
export function extendWebpackConfig (
|
||||||
|
fn: ((config: WebpackConfig)=> void),
|
||||||
|
options: ExtendWebpackConfigOptions = {}
|
||||||
|
) {
|
||||||
const nuxt = useNuxt()
|
const nuxt = useNuxt()
|
||||||
|
|
||||||
if (options.dev === false && nuxt.options.dev) {
|
if (options.dev === false && nuxt.options.dev) {
|
||||||
@ -189,24 +196,25 @@ export function addWebpackPlugin (plugin: WebpackPluginInstance, options: AddWeb
|
|||||||
if (options.server !== false) {
|
if (options.server !== false) {
|
||||||
const config = configs.find(i => i.name === 'server')
|
const config = configs.find(i => i.name === 'server')
|
||||||
if (config) {
|
if (config) {
|
||||||
config.plugins = config.plugins || []
|
fn(config)
|
||||||
config.plugins.push(plugin)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (options.client !== false) {
|
if (options.client !== false) {
|
||||||
const config = configs.find(i => i.name === 'client')
|
const config = configs.find(i => i.name === 'client')
|
||||||
if (config) {
|
if (config) {
|
||||||
config.plugins = config.plugins || []
|
fn(config)
|
||||||
config.plugins.push(plugin)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Append Vite plugin to the config.
|
* Extend Vite config
|
||||||
*/
|
*/
|
||||||
export function addVitePlugin (plugin: VitePlugin, options: AddVitePluginHookOptions = {}) {
|
export function extendViteConfig (
|
||||||
|
fn: ((config: ViteConfig) => void),
|
||||||
|
options: ExtendViteConfigOptions = {}
|
||||||
|
) {
|
||||||
const nuxt = useNuxt()
|
const nuxt = useNuxt()
|
||||||
|
|
||||||
if (options.dev === false && nuxt.options.dev) {
|
if (options.dev === false && nuxt.options.dev) {
|
||||||
@ -216,8 +224,25 @@ export function addVitePlugin (plugin: VitePlugin, options: AddVitePluginHookOpt
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
nuxt.hook('vite:extend', ({ config }) => {
|
nuxt.hook('vite:extend', ({ config }) => fn(config))
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append Webpack plugin to the config.
|
||||||
|
*/
|
||||||
|
export function addWebpackPlugin (plugin: WebpackPluginInstance, options?: ExtendWebpackConfigOptions) {
|
||||||
|
extendWebpackConfig((config) => {
|
||||||
config.plugins = config.plugins || []
|
config.plugins = config.plugins || []
|
||||||
config.plugins.push(plugin)
|
config.plugins.push(plugin)
|
||||||
})
|
}, options)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append Vite plugin to the config.
|
||||||
|
*/
|
||||||
|
export function addVitePlugin (plugin: VitePlugin, options?: ExtendViteConfigOptions) {
|
||||||
|
extendViteConfig((config) => {
|
||||||
|
config.plugins = config.plugins || []
|
||||||
|
config.plugins.push(plugin)
|
||||||
|
}, options)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user