Skip to content
4 changes: 4 additions & 0 deletions packages/bundler-vite/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { mergeConfig } from 'vite'

import { viteBundler } from './viteBundler.js'

export type * from './types.js'
export * from './viteBundler.js'

export { mergeConfig as viteMergeConfig, viteBundler }
export default viteBundler
20 changes: 18 additions & 2 deletions packages/bundler-vite/src/resolveViteConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ export const resolveViteConfig = ({
options: ViteBundlerOptions
isBuild: boolean
isServer: boolean
}): InlineConfig =>
mergeConfig(
}): InlineConfig => {
// generate base vite config
let viteConfig = mergeConfig(
{
clearScreen: false,
configFile: false,
Expand All @@ -43,3 +44,18 @@ export const resolveViteConfig = ({
// some vite options would not take effect inside a plugin, so we still need to merge them here in addition to userConfigPlugin
options.viteOptions ?? {},
)

// allow modifying vite config via `configureVite`
const configureViteResult = options.configureVite?.(
viteConfig,
isServer,
isBuild,
)
Comment thread
meteorlxy marked this conversation as resolved.

// if `configureVite` returns a configuration object, use it as the new vite config
if (configureViteResult) {
viteConfig = configureViteResult
}

return viteConfig
}
19 changes: 19 additions & 0 deletions packages/bundler-vite/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ import type { InlineConfig } from 'vite'
* Options for bundler-vite
*/
export interface ViteBundlerOptions extends BundlerOptions {
/**
* Vite options
*/
viteOptions?: InlineConfig
/**
* Options for @vitejs/plugin-vue
*/
vuePluginOptions?: VuePluginOptions
/**
* Modify Vite config
*
* @param config - Vite config
* @param isServer - Whether it is server bundle
* @param isBuild - Whether in build mode
* @returns if returns a configuration object, it will be used as the new vite config, otherwise the original config object will be used
*/
configureVite?: (
config: InlineConfig,
isServer: boolean,
isBuild: boolean,
) => InlineConfig | void
}
1 change: 1 addition & 0 deletions packages/bundler-vite/src/viteBundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { ViteBundlerOptions } from './types.js'

export const viteBundler = (options: ViteBundlerOptions = {}): Bundler => ({
name: '@vuepress/bundler-vite',
type: 'vite',
dev: async (app) => dev(options, app),
build: async (app) => build(options, app),
})
4 changes: 4 additions & 0 deletions packages/bundler-webpack/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { merge } from 'webpack-merge'

import { webpackBundler } from './webpackBundler.js'

export type * from './types.js'
export * from './webpackBundler.js'

export { merge as webpackMergeConfig, webpackBundler }
export default webpackBundler
8 changes: 3 additions & 5 deletions packages/bundler-webpack/src/resolveWebpackConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Configuration } from 'webpack'
import { merge } from 'webpack-merge'
import type { Config } from 'webpack-v5-chain'

import type { WebpackBundlerOptions } from './types.js'
Expand All @@ -19,7 +18,7 @@ export const resolveWebpackConfig = ({
options.chainWebpack?.(config, isServer, isBuild)

// generate webpack config from webpack-v5-chain
const webpackConfig = config.toConfig()
let webpackConfig = config.toConfig()

// allow modifying webpack config via `configureWebpack`
const configureWebpackResult = options.configureWebpack?.(
Expand All @@ -28,10 +27,9 @@ export const resolveWebpackConfig = ({
isBuild,
Comment thread
meteorlxy marked this conversation as resolved.
)

// if `configureWebpack` returns a configuration object,
// use webpack-merge to merge it
// if `configureWebpack` returns a configuration object, use this object as the new webpack config
if (configureWebpackResult) {
return merge(webpackConfig, configureWebpackResult)
webpackConfig = configureWebpackResult
}

return webpackConfig
Expand Down
5 changes: 5 additions & 0 deletions packages/bundler-webpack/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export type {
export interface WebpackBundlerOptions extends BundlerOptions {
/**
* use webpack-merge to set webpack config
*
* @param config - Webpack config
* @param isServer - Whether it is server bundle
* @param isBuild - Whether in build mode
* @returns if returns a configuration object, it will be used as the new webpack config, otherwise the original config object will be used
*/
Comment thread
Mister-Hope marked this conversation as resolved.
Comment thread
meteorlxy marked this conversation as resolved.
configureWebpack?: (
config: WebpackConfiguration,
Expand Down
1 change: 1 addition & 0 deletions packages/bundler-webpack/src/webpackBundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const webpackBundler = (
options: WebpackBundlerOptions = {},
): Bundler => ({
name: '@vuepress/bundler-webpack',
type: 'webpack',
dev: async (app) => dev(options, app),
build: async (app) => build(options, app),
})
2 changes: 1 addition & 1 deletion packages/core/src/app/resolveAppMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const resolveAppMarkdown = async (app: App): Promise<Markdown> => {
if (app.options.markdown.assets !== false) {
app.options.markdown.assets ??= {}
app.options.markdown.assets.absolutePathPrependBase ??=
app.options.bundler.name === '@vuepress/bundler-webpack'
app.options.bundler.type === 'webpack'
}

const markdown = createMarkdown(app.options.markdown)
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/types/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export interface Bundler {
*/
name: string

/**
* Type of the bundler, e.g. 'vite' or 'webpack'
*/
type: string

Comment thread
meteorlxy marked this conversation as resolved.
/**
* Method to run vuepress app in dev mode, starting dev server
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/core/tests/app/resolveAppOptions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ it('should create app options with default values', () => {
resolveAppOptions({
source,
theme: { name: 'theme' },
bundler: { name: 'bundler' } as Bundler,
bundler: { name: 'bundler', type: 'vite' } as Bundler,
}),
).toEqual({
base: '/',
Expand All @@ -23,7 +23,7 @@ it('should create app options with default values', () => {
'/': { lang: 'en-US', title: '', description: '' },
},
theme: { name: 'theme' },
bundler: { name: 'bundler' },
bundler: { name: 'bundler', type: 'vite' },
source,
dest: path.resolve(source, '.vuepress/dist'),
temp: path.resolve(source, '.vuepress/.temp'),
Expand Down
Loading