mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
feat(webpack): allow babel plugins to be defined by function (#7443)
This commit is contained in:
parent
8933d375e9
commit
0610f0b343
3
packages/types/config/build.d.ts
vendored
3
packages/types/config/build.d.ts
vendored
@ -85,12 +85,13 @@ interface NuxtBabelPresetEnv {
|
||||
envName: 'client' | 'modern' | 'server'
|
||||
}
|
||||
|
||||
interface NuxtBabelOptions extends Pick<TransformOptions, Exclude<keyof TransformOptions, 'presets'>> {
|
||||
interface NuxtBabelOptions extends Pick<TransformOptions, Exclude<keyof TransformOptions, 'presets' | 'plugins'>> {
|
||||
cacheCompression?: boolean
|
||||
cacheDirectory?: boolean
|
||||
cacheIdentifier?: string
|
||||
customize?: string | null
|
||||
presets?: ((env: NuxtBabelPresetEnv & NuxtWebpackEnv, defaultPreset: [string, object]) => PluginItem[] | void) | PluginItem[] | null
|
||||
plugins?: ((env: NuxtBabelPresetEnv & NuxtWebpackEnv) => NonNullable<TransformOptions['plugins']>) | TransformOptions['plugins']
|
||||
}
|
||||
|
||||
interface Warning {
|
||||
|
@ -105,6 +105,15 @@ export default class WebpackBaseConfig {
|
||||
return options
|
||||
}
|
||||
|
||||
if (typeof options.plugins === 'function') {
|
||||
options.plugins = options.plugins(
|
||||
{
|
||||
envName,
|
||||
...this.nuxtEnv
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
const defaultPreset = [require.resolve('@nuxt/babel-preset-app'), {}]
|
||||
|
||||
if (typeof options.presets === 'function') {
|
||||
|
23
packages/webpack/test/index.test.js
Normal file
23
packages/webpack/test/index.test.js
Normal file
@ -0,0 +1,23 @@
|
||||
import WebpackBaseConfig from '../src/config/base'
|
||||
|
||||
describe('webpack: babel', () => {
|
||||
const getConfigWithPlugins = plugins =>
|
||||
new WebpackBaseConfig({
|
||||
buildContext: {
|
||||
options: {
|
||||
dev: false
|
||||
},
|
||||
buildOptions: {
|
||||
babel: {
|
||||
plugins
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
test('should allow defining plugins with an array', () => {
|
||||
expect(getConfigWithPlugins(['myPlugin']).getBabelOptions().plugins).toEqual(['myPlugin'])
|
||||
})
|
||||
test('should allow defining plugins with a function', () => {
|
||||
expect(getConfigWithPlugins(({ isDev }) => [`myPlugin-${isDev}`]).getBabelOptions().plugins).toEqual(['myPlugin-false'])
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue
Block a user