2018-03-16 19:52:17 +00:00
|
|
|
import path from 'path'
|
|
|
|
|
2018-03-16 19:11:24 +00:00
|
|
|
import _ from 'lodash'
|
2018-03-16 16:12:06 +00:00
|
|
|
import webpack from 'webpack'
|
|
|
|
import HTMLPlugin from 'html-webpack-plugin'
|
2018-03-16 16:18:57 +00:00
|
|
|
import FriendlyErrorsWebpackPlugin from '@nuxtjs/friendly-errors-webpack-plugin'
|
2018-03-16 16:12:06 +00:00
|
|
|
import StylishPlugin from 'webpack-stylish'
|
2018-03-16 19:11:24 +00:00
|
|
|
import BundleAnalyzer from 'webpack-bundle-analyzer'
|
2018-03-16 19:52:17 +00:00
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
import Debug from 'debug'
|
2018-03-16 19:11:24 +00:00
|
|
|
import base from './base.config'
|
2017-03-11 23:55:01 +00:00
|
|
|
|
2018-03-16 19:52:17 +00:00
|
|
|
// import VueSSRClientPlugin from 'vue-server-renderer/client-plugin'
|
|
|
|
import VueSSRClientPlugin from './plugins/vue/client'
|
|
|
|
|
2017-08-21 11:16:35 +00:00
|
|
|
const debug = Debug('nuxt:build')
|
|
|
|
debug.color = 2 // Force green color
|
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
export default function webpackClientConfig() {
|
2017-12-08 14:18:37 +00:00
|
|
|
let config = base.call(this, { name: 'client', isServer: false })
|
2017-06-15 22:19:53 +00:00
|
|
|
|
2017-11-07 12:05:41 +00:00
|
|
|
// Entry points
|
2018-03-16 19:11:24 +00:00
|
|
|
config.entry.app = path.resolve(this.options.buildDir, 'client.js')
|
2016-11-09 22:59:41 +00:00
|
|
|
|
2017-06-15 22:19:53 +00:00
|
|
|
// Env object defined in nuxt.config.js
|
2016-11-25 14:37:06 +00:00
|
|
|
let env = {}
|
2018-03-16 19:11:24 +00:00
|
|
|
_.each(this.options.env, (value, key) => {
|
2018-01-13 05:22:11 +00:00
|
|
|
env['process.env.' + key] =
|
|
|
|
['boolean', 'number'].indexOf(typeof value) !== -1
|
|
|
|
? value
|
|
|
|
: JSON.stringify(value)
|
2016-11-25 14:37:06 +00:00
|
|
|
})
|
2017-06-15 22:19:53 +00:00
|
|
|
|
2017-08-18 08:08:43 +00:00
|
|
|
// Generate output HTML for SPA
|
|
|
|
config.plugins.push(
|
|
|
|
new HTMLPlugin({
|
|
|
|
filename: 'index.spa.html',
|
|
|
|
template: this.options.appTemplatePath,
|
|
|
|
inject: true,
|
|
|
|
chunksSortMode: 'dependency'
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2017-07-11 00:24:39 +00:00
|
|
|
// Generate output HTML for SSR
|
|
|
|
if (this.options.build.ssr) {
|
|
|
|
config.plugins.push(
|
|
|
|
new HTMLPlugin({
|
|
|
|
filename: 'index.ssr.html',
|
|
|
|
template: this.options.appTemplatePath,
|
|
|
|
inject: false // Resources will be injected using bundleRenderer
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-06-15 22:19:53 +00:00
|
|
|
// Generate vue-ssr-client-manifest
|
|
|
|
config.plugins.push(
|
|
|
|
new VueSSRClientPlugin({
|
|
|
|
filename: 'vue-ssr-client-manifest.json'
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
// Define Env
|
|
|
|
config.plugins.push(
|
2018-01-13 05:22:11 +00:00
|
|
|
new webpack.DefinePlugin(
|
|
|
|
Object.assign(env, {
|
|
|
|
'process.env.VUE_ENV': JSON.stringify('client'),
|
|
|
|
'process.mode': JSON.stringify(this.options.mode),
|
|
|
|
'process.browser': true,
|
|
|
|
'process.client': true,
|
|
|
|
'process.server': false,
|
|
|
|
'process.static': this.isStatic
|
|
|
|
})
|
|
|
|
)
|
2017-06-15 22:19:53 +00:00
|
|
|
)
|
|
|
|
|
2018-01-13 05:22:11 +00:00
|
|
|
const shouldClearConsole =
|
|
|
|
this.options.build.stats !== false &&
|
|
|
|
this.options.build.stats !== 'errors-only'
|
2017-12-17 13:59:50 +00:00
|
|
|
|
2017-12-13 06:53:32 +00:00
|
|
|
// Add friendly error plugin
|
2018-01-13 05:22:11 +00:00
|
|
|
config.plugins.push(
|
|
|
|
new FriendlyErrorsWebpackPlugin({ clearConsole: shouldClearConsole })
|
|
|
|
)
|
2017-12-13 06:53:32 +00:00
|
|
|
|
2018-03-11 21:28:56 +00:00
|
|
|
// Optimization
|
|
|
|
config.optimization = {
|
|
|
|
splitChunks: {
|
2018-03-14 08:21:41 +00:00
|
|
|
chunks: 'all',
|
|
|
|
// TODO: remove spa after https://github.com/jantimon/html-webpack-plugin/issues/878 solved
|
|
|
|
name: this.options.dev || this.options.mode === 'spa'
|
2018-03-11 21:28:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-15 22:19:53 +00:00
|
|
|
// --------------------------------------
|
|
|
|
// Dev specific config
|
|
|
|
// --------------------------------------
|
2017-06-11 14:17:36 +00:00
|
|
|
if (this.options.dev) {
|
2017-06-15 14:53:00 +00:00
|
|
|
// Add HMR support
|
2017-08-25 12:01:16 +00:00
|
|
|
config.entry.app = [
|
|
|
|
// https://github.com/glenjamin/webpack-hot-middleware#config
|
2018-01-13 05:22:11 +00:00
|
|
|
`webpack-hot-middleware/client?name=client&reload=true&timeout=30000&path=${
|
|
|
|
this.options.router.base
|
|
|
|
}/__webpack_hmr`.replace(/\/\//g, '/'),
|
2017-08-25 12:01:16 +00:00
|
|
|
config.entry.app
|
|
|
|
]
|
2018-02-26 10:32:40 +00:00
|
|
|
|
|
|
|
// HMR
|
|
|
|
config.plugins.push(new webpack.HotModuleReplacementPlugin())
|
2017-06-15 14:53:00 +00:00
|
|
|
}
|
2017-06-15 22:19:53 +00:00
|
|
|
|
|
|
|
// --------------------------------------
|
|
|
|
// Production specific config
|
|
|
|
// --------------------------------------
|
2017-06-11 14:17:36 +00:00
|
|
|
if (!this.options.dev) {
|
2018-01-25 08:01:48 +00:00
|
|
|
// Chunks size limit
|
|
|
|
// https://webpack.js.org/plugins/aggressive-splitting-plugin/
|
|
|
|
if (this.options.build.maxChunkSize) {
|
|
|
|
config.plugins.push(
|
|
|
|
new webpack.optimize.AggressiveSplittingPlugin({
|
|
|
|
minSize: this.options.build.maxChunkSize,
|
|
|
|
maxSize: this.options.build.maxChunkSize
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-03-13 12:06:12 +00:00
|
|
|
// https://github.com/webpack-contrib/webpack-stylish
|
|
|
|
if (!this.options.dev) {
|
|
|
|
config.plugins.push(new StylishPlugin())
|
|
|
|
}
|
|
|
|
|
2017-06-15 22:19:53 +00:00
|
|
|
// Webpack Bundle Analyzer
|
|
|
|
if (this.options.build.analyze) {
|
2018-01-13 05:22:11 +00:00
|
|
|
config.plugins.push(
|
2018-03-16 19:11:24 +00:00
|
|
|
new BundleAnalyzer.BundleAnalyzerPlugin(Object.assign({}, this.options.build.analyze))
|
2018-01-13 05:22:11 +00:00
|
|
|
)
|
2017-06-15 22:19:53 +00:00
|
|
|
}
|
2016-11-09 22:59:41 +00:00
|
|
|
}
|
2017-06-15 22:19:53 +00:00
|
|
|
|
2016-12-27 13:54:10 +00:00
|
|
|
// Extend config
|
|
|
|
if (typeof this.options.build.extend === 'function') {
|
2017-10-20 10:05:22 +00:00
|
|
|
const isDev = this.options.dev
|
2017-08-21 20:21:16 +00:00
|
|
|
const extendedConfig = this.options.build.extend.call(this, config, {
|
2017-10-20 10:05:22 +00:00
|
|
|
isDev,
|
2016-12-27 13:54:10 +00:00
|
|
|
isClient: true
|
|
|
|
})
|
2018-03-11 21:28:56 +00:00
|
|
|
|
2017-08-21 20:21:16 +00:00
|
|
|
// Only overwrite config when something is returned for backwards compatibility
|
|
|
|
if (extendedConfig !== undefined) {
|
|
|
|
config = extendedConfig
|
|
|
|
}
|
2016-12-27 13:54:10 +00:00
|
|
|
}
|
2017-06-15 22:19:53 +00:00
|
|
|
|
2016-11-09 22:59:41 +00:00
|
|
|
return config
|
2016-11-07 01:34:58 +00:00
|
|
|
}
|