feat(webpack): allow babel plugins to be defined by function (#7443)

This commit is contained in:
Daniel Roe 2020-06-03 20:26:37 +01:00 committed by GitHub
parent 8933d375e9
commit 0610f0b343
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 1 deletions

View File

@ -85,12 +85,13 @@ interface NuxtBabelPresetEnv {
envName: 'client' | 'modern' | 'server' 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 cacheCompression?: boolean
cacheDirectory?: boolean cacheDirectory?: boolean
cacheIdentifier?: string cacheIdentifier?: string
customize?: string | null customize?: string | null
presets?: ((env: NuxtBabelPresetEnv & NuxtWebpackEnv, defaultPreset: [string, object]) => PluginItem[] | void) | PluginItem[] | null presets?: ((env: NuxtBabelPresetEnv & NuxtWebpackEnv, defaultPreset: [string, object]) => PluginItem[] | void) | PluginItem[] | null
plugins?: ((env: NuxtBabelPresetEnv & NuxtWebpackEnv) => NonNullable<TransformOptions['plugins']>) | TransformOptions['plugins']
} }
interface Warning { interface Warning {

View File

@ -105,6 +105,15 @@ export default class WebpackBaseConfig {
return options return options
} }
if (typeof options.plugins === 'function') {
options.plugins = options.plugins(
{
envName,
...this.nuxtEnv
}
)
}
const defaultPreset = [require.resolve('@nuxt/babel-preset-app'), {}] const defaultPreset = [require.resolve('@nuxt/babel-preset-app'), {}]
if (typeof options.presets === 'function') { if (typeof options.presets === 'function') {

View 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'])
})
})