refactor(webpack): allow async presets

This commit is contained in:
Daniel Roe 2023-07-25 12:01:14 +01:00
parent f7e2b2bf6e
commit 98c195f83e
7 changed files with 16 additions and 16 deletions

View File

@ -10,11 +10,11 @@ import type { WebpackConfigContext } from '../utils/config'
import { applyPresets } from '../utils/config'
import { nuxt } from '../presets/nuxt'
export function client (ctx: WebpackConfigContext) {
export async function client (ctx: WebpackConfigContext) {
ctx.name = 'client'
ctx.isClient = true
applyPresets(ctx, [
await applyPresets(ctx, [
nuxt,
clientPlugins,
clientOptimization,

View File

@ -9,11 +9,11 @@ import { node } from '../presets/node'
const assetPattern = /\.(css|s[ca]ss|png|jpe?g|gif|svg|woff2?|eot|ttf|otf|webp|webm|mp4|ogv)(\?.*)?$/i
export function server (ctx: WebpackConfigContext) {
export async function server (ctx: WebpackConfigContext) {
ctx.name = 'server'
ctx.isServer = true
applyPresets(ctx, [
await applyPresets(ctx, [
nuxt,
node,
serverStandalone,

View File

@ -16,8 +16,8 @@ import WarningIgnorePlugin from '../plugins/warning-ignore'
import type { WebpackConfigContext } from '../utils/config'
import { applyPresets, fileName } from '../utils/config'
export function base (ctx: WebpackConfigContext) {
applyPresets(ctx, [
export async function base (ctx: WebpackConfigContext) {
await applyPresets(ctx, [
baseAlias,
baseConfig,
basePlugins,

View File

@ -8,8 +8,8 @@ import { pug } from './pug'
import { style } from './style'
import { vue } from './vue'
export function nuxt (ctx: WebpackConfigContext) {
applyPresets(ctx, [
export async function nuxt (ctx: WebpackConfigContext) {
await applyPresets(ctx, [
base,
assets,
esbuild,

View File

@ -4,8 +4,8 @@ import type { WebpackConfigContext } from '../utils/config'
import { applyPresets, fileName } from '../utils/config'
import { getPostcssConfig } from '../utils/postcss'
export function style (ctx: WebpackConfigContext) {
applyPresets(ctx, [
export async function style (ctx: WebpackConfigContext) {
await applyPresets(ctx, [
loaders,
extractCSS,
minimizer

View File

@ -36,15 +36,15 @@ export function createWebpackConfigContext (nuxt: Nuxt): WebpackConfigContext {
}
}
export function applyPresets (ctx: WebpackConfigContext, presets: WebpackConfigPresetItem | WebpackConfigPresetItem[]) {
export async function applyPresets (ctx: WebpackConfigContext, presets: WebpackConfigPresetItem | WebpackConfigPresetItem[]) {
if (!Array.isArray(presets)) {
presets = [presets]
}
for (const preset of presets) {
if (Array.isArray(preset)) {
preset[0](ctx, preset[1])
await preset[0](ctx, preset[1])
} else {
preset(ctx)
await preset(ctx)
}
}
}

View File

@ -25,12 +25,12 @@ import { applyPresets, createWebpackConfigContext, getWebpackConfig } from './ut
export async function bundle (nuxt: Nuxt) {
registerVirtualModules()
const webpackConfigs = [client, ...nuxt.options.ssr ? [server] : []].map((preset) => {
const webpackConfigs = await Promise.all([client, ...nuxt.options.ssr ? [server] : []].map(async (preset) => {
const ctx = createWebpackConfigContext(nuxt)
ctx.userConfig = defu(nuxt.options.webpack[`$${preset.name as 'client' | 'server'}`], ctx.userConfig)
applyPresets(ctx, preset)
await applyPresets(ctx, preset)
return getWebpackConfig(ctx)
})
}))
await nuxt.callHook('webpack:config', webpackConfigs)