mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-26 23:52:06 +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'
|
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 {
|
||||||
|
@ -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') {
|
||||||
|
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