feat(webpack): extendable `babel.presets` and babel `envName` (#5637)

This commit is contained in:
Xin Du (Clark) 2019-05-06 15:00:07 +01:00 committed by Pooya Parsa
parent 8576b4fcfd
commit adb381275b
2 changed files with 28 additions and 23 deletions

View File

@ -1,7 +1,6 @@
import path from 'path' import path from 'path'
import consola from 'consola' import consola from 'consola'
import TimeFixPlugin from 'time-fix-plugin' import TimeFixPlugin from 'time-fix-plugin'
import clone from 'lodash/clone'
import cloneDeep from 'lodash/cloneDeep' import cloneDeep from 'lodash/cloneDeep'
import escapeRegExp from 'lodash/escapeRegExp' import escapeRegExp from 'lodash/escapeRegExp'
import VueLoader from 'vue-loader' import VueLoader from 'vue-loader'
@ -70,21 +69,28 @@ export default class WebpackBaseConfig {
} }
getBabelOptions() { getBabelOptions() {
const options = clone(this.buildContext.buildOptions.babel) const options = {
...this.buildContext.buildOptions.babel,
if (typeof options.presets === 'function') { envName: this.name
options.presets = options.presets({ isServer: this.isServer })
} }
if (!options.babelrc && !options.presets) { if (options.configFile !== false) {
options.presets = [ return options
[ }
const defaultPreset = [
require.resolve('@nuxt/babel-preset-app'), require.resolve('@nuxt/babel-preset-app'),
{ {
buildTarget: this.isServer ? 'server' : 'client' buildTarget: this.isServer ? 'server' : 'client'
} }
] ]
]
if (typeof options.presets === 'function') {
options.presets = options.presets({ isServer: this.isServer }, defaultPreset)
}
if (!options.babelrc && !options.presets) {
options.presets = [ defaultPreset ]
} }
return options return options

View File

@ -1,4 +1,3 @@
import clone from 'lodash/clone'
import WebpackClientConfig from './client' import WebpackClientConfig from './client'
export default class WebpackModernConfig extends WebpackClientConfig { export default class WebpackModernConfig extends WebpackClientConfig {
@ -15,9 +14,10 @@ export default class WebpackModernConfig extends WebpackClientConfig {
} }
getBabelOptions() { getBabelOptions() {
const options = clone(this.buildContext.buildOptions.babel) return {
...this.buildContext.buildOptions.babel,
options.presets = [ envName: this.name,
presets: [
[ [
require.resolve('@nuxt/babel-preset-app'), require.resolve('@nuxt/babel-preset-app'),
{ {
@ -25,7 +25,6 @@ export default class WebpackModernConfig extends WebpackClientConfig {
} }
] ]
] ]
}
return options
} }
} }