Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(kit): addBuildPlugin for builder-agnostic implementation #20587

Merged
merged 7 commits into from May 14, 2023
15 changes: 15 additions & 0 deletions packages/kit/src/build.ts
Expand Up @@ -136,3 +136,18 @@ export function addVitePlugin (pluginOrGetter: VitePlugin | VitePlugin[] | (() =
}
}, options)
}

interface AddBuildPluginFactory {
vite?: () => VitePlugin | VitePlugin[]
webpack?: () => WebpackPluginInstance | WebpackPluginInstance[]
}

export function addBuildPlugin (pluginFactory: AddBuildPluginFactory, options?: ExtendConfigOptions) {
if (pluginFactory.vite) {
addVitePlugin(pluginFactory.vite, options)
}

if (pluginFactory.webpack) {
addWebpackPlugin(pluginFactory.webpack, options)
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an idea: This function would also check if current builder is supported or not and make a warning a build plugin is added without vite/webpack support

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the idea. Also maybe provide verbose warning and suggestion in module building mode?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For module building (i guess testing is only possible) we could do if partial in either case too :)

5 changes: 2 additions & 3 deletions test/fixtures/basic/nuxt.config.ts
@@ -1,4 +1,4 @@
import { addComponent, addVitePlugin, addWebpackPlugin } from 'nuxt/kit'
import { addBuildPlugin, addComponent } from 'nuxt/kit'
import type { NuxtPage } from 'nuxt/schema'
import { createUnplugin } from 'unplugin'
import { withoutLeadingSlash } from 'ufo'
Expand Down Expand Up @@ -104,8 +104,7 @@ export default defineNuxtConfig({
if (id === 'virtual.css') { return ':root { --virtual: red }' }
}
}))
addVitePlugin(() => plugin.vite())
addWebpackPlugin(() => plugin.webpack())
addBuildPlugin(plugin)
},
function (_options, nuxt) {
const routesToDuplicate = ['/async-parent', '/fixed-keyed-child-parent', '/keyed-child-parent', '/with-layout', '/with-layout2']
Expand Down