refactor: extract customize webpack function (#3745)

This commit is contained in:
Clark Du 2018-08-16 18:14:26 +01:00 committed by Sébastien Chopin
parent a67136c6d6
commit 673a38216f
3 changed files with 19 additions and 31 deletions

View File

@ -260,6 +260,21 @@ export default class WebpackBaseConfig {
return plugins
}
customize(config) {
if (typeof this.options.build.extend === 'function') {
const extendedConfig = this.options.build.extend.call(this.builder, config, {
isDev: this.options.dev,
isServer: this.isServer,
isClient: !this.isServer
})
// Only overwrite config when something is returned for backwards compatibility
if (extendedConfig !== undefined) {
return extendedConfig
}
}
return config
}
config() {
// Prioritize nested node_modules in webpack search path (#2558)
const webpackModulesDir = ['node_modules'].concat(this.options.modulesDir)

View File

@ -76,7 +76,7 @@ export default class WebpackClientConfig extends WebpackBaseConfig {
}
config() {
let config = super.config()
const config = super.config()
// Entry points
config.entry = path.resolve(this.options.buildDir, 'client.js')
@ -119,20 +119,6 @@ export default class WebpackClientConfig extends WebpackBaseConfig {
)
}
// Extend config
if (typeof this.options.build.extend === 'function') {
const isDev = this.options.dev
const extendedConfig = this.options.build.extend.call(this.builder, config, {
isDev,
isClient: true
})
// Only overwrite config when something is returned for backwards compatibility
if (extendedConfig !== undefined) {
config = extendedConfig
}
}
// Make uglifyjs faster
if (!this.options.dev && !config.optimization.minimizer) {
// https://github.com/webpack-contrib/uglifyjs-webpack-plugin
@ -153,6 +139,6 @@ export default class WebpackClientConfig extends WebpackBaseConfig {
]
}
return config
return this.customize(config)
}
}

View File

@ -33,7 +33,7 @@ export default class WebpackServerConfig extends BaseConfig {
}
config() {
let config = super.config()
const config = super.config()
// Config devtool
config.devtool = 'cheap-source-map'
@ -75,19 +75,6 @@ export default class WebpackServerConfig extends BaseConfig {
}
})
// Extend config
if (typeof this.options.build.extend === 'function') {
const isDev = this.options.dev
const extendedConfig = this.options.build.extend.call(this.builder, config, {
isDev,
isServer: true
})
// Only overwrite config when something is returned for backwards compatibility
if (extendedConfig !== undefined) {
config = extendedConfig
}
}
return config
return this.customize(config)
}
}