2016-11-10 02:38:11 +00:00
|
|
|
'use strict'
|
|
|
|
|
2017-01-11 19:14:59 +00:00
|
|
|
import { each } from 'lodash'
|
|
|
|
import webpack from 'webpack'
|
2017-02-21 17:10:19 +00:00
|
|
|
import HTMLPlugin from 'html-webpack-plugin'
|
2017-03-24 00:28:04 +00:00
|
|
|
import FriendlyErrorsWebpackPlugin from 'friendly-errors-webpack-plugin'
|
2017-02-21 17:10:19 +00:00
|
|
|
import ScriptExtHtmlWebpackPlugin from 'script-ext-html-webpack-plugin'
|
|
|
|
import PreloadWebpackPlugin from 'preload-webpack-plugin'
|
2017-02-08 13:09:59 +00:00
|
|
|
import ProgressBarPlugin from 'progress-bar-webpack-plugin'
|
2017-01-23 16:55:39 +00:00
|
|
|
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'
|
2017-01-11 19:14:59 +00:00
|
|
|
import base from './base.config.js'
|
|
|
|
import { resolve } from 'path'
|
2016-11-07 01:34:58 +00:00
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Webpack Client Config
|
|
|
|
|
|
|
|
|
| Generate public/dist/client-vendor-bundle.js
|
|
|
|
| Generate public/dist/client-bundle.js
|
|
|
|
|
|
|
|
|
| In production, will generate public/dist/style.css
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
*/
|
2017-01-11 19:14:59 +00:00
|
|
|
export default function () {
|
2017-01-09 14:10:22 +00:00
|
|
|
let config = base.call(this, { isClient: true })
|
2016-11-07 01:34:58 +00:00
|
|
|
|
2016-11-09 22:59:41 +00:00
|
|
|
// Entry
|
|
|
|
config.entry.app = resolve(this.dir, '.nuxt', 'client.js')
|
|
|
|
|
|
|
|
// Add vendors
|
|
|
|
if (this.options.store) {
|
|
|
|
config.entry.vendor.push('vuex')
|
|
|
|
}
|
|
|
|
config.entry.vendor = config.entry.vendor.concat(this.options.build.vendor)
|
|
|
|
|
|
|
|
// Output
|
|
|
|
config.output.path = resolve(this.dir, '.nuxt', 'dist')
|
|
|
|
config.output.filename = this.options.build.filenames.app
|
|
|
|
|
2016-11-25 14:37:06 +00:00
|
|
|
// env object defined in nuxt.config.js
|
|
|
|
let env = {}
|
2017-01-11 19:14:59 +00:00
|
|
|
each(this.options.env, (value, key) => {
|
2016-11-25 14:37:06 +00:00
|
|
|
env['process.env.' + key] = (typeof value === 'string' ? JSON.stringify(value) : value)
|
|
|
|
})
|
2016-11-09 22:59:41 +00:00
|
|
|
// Webpack plugins
|
|
|
|
config.plugins = (config.plugins || []).concat([
|
2017-02-21 17:10:19 +00:00
|
|
|
// Strip comments in Vue code
|
2016-11-25 14:37:06 +00:00
|
|
|
new webpack.DefinePlugin(Object.assign(env, {
|
2016-11-09 22:59:41 +00:00
|
|
|
'process.env.NODE_ENV': JSON.stringify(this.dev ? 'development' : 'production'),
|
2016-11-24 00:46:20 +00:00
|
|
|
'process.BROWSER_BUILD': true,
|
2017-02-28 12:10:58 +00:00
|
|
|
'process.SERVER_BUILD': false,
|
|
|
|
'process.browser': true,
|
|
|
|
'process.server': true
|
2016-11-25 14:37:06 +00:00
|
|
|
})),
|
2016-11-09 22:59:41 +00:00
|
|
|
// Extract vendor chunks for better caching
|
|
|
|
new webpack.optimize.CommonsChunkPlugin({
|
|
|
|
name: 'vendor',
|
|
|
|
filename: this.options.build.filenames.vendor
|
2017-02-21 17:10:19 +00:00
|
|
|
}),
|
2017-03-25 23:52:39 +00:00
|
|
|
// Extract manifest
|
|
|
|
new webpack.optimize.CommonsChunkPlugin({
|
|
|
|
name: 'manifest',
|
|
|
|
filename: this.options.build.filenames.manifest
|
|
|
|
}),
|
2017-02-21 17:10:19 +00:00
|
|
|
// Generate output HTML
|
|
|
|
new HTMLPlugin({
|
2017-02-22 18:19:49 +00:00
|
|
|
template: this.options.appTemplatePath
|
2017-02-21 17:10:19 +00:00
|
|
|
}),
|
|
|
|
// Add defer to scripts
|
|
|
|
new ScriptExtHtmlWebpackPlugin({
|
|
|
|
defaultAttribute: 'defer'
|
2016-11-07 01:34:58 +00:00
|
|
|
})
|
|
|
|
])
|
|
|
|
|
2017-02-23 13:43:57 +00:00
|
|
|
if (!this.dev && this.options.performance.prefetch === true) {
|
2017-02-21 17:39:29 +00:00
|
|
|
// Add prefetch code-splitted routes
|
|
|
|
config.plugins.push(
|
|
|
|
new PreloadWebpackPlugin({
|
|
|
|
rel: 'prefetch'
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
2017-02-08 13:09:59 +00:00
|
|
|
// client bundle progress bar
|
|
|
|
config.plugins.push(
|
|
|
|
new ProgressBarPlugin()
|
|
|
|
)
|
2017-03-24 00:28:04 +00:00
|
|
|
// Add friendly error plugin
|
|
|
|
if (this.dev) {
|
|
|
|
config.plugins.push(new FriendlyErrorsWebpackPlugin())
|
|
|
|
}
|
2016-11-09 22:59:41 +00:00
|
|
|
// Production client build
|
|
|
|
if (!this.dev) {
|
|
|
|
config.plugins.push(
|
2017-03-24 00:28:04 +00:00
|
|
|
// This is needed in webpack 2 for minifying CSS
|
|
|
|
new webpack.LoaderOptionsPlugin({
|
|
|
|
minimize: true
|
|
|
|
}),
|
2016-11-09 22:59:41 +00:00
|
|
|
// Minify JS
|
|
|
|
new webpack.optimize.UglifyJsPlugin({
|
2017-03-24 00:28:04 +00:00
|
|
|
sourceMap: true,
|
2016-11-09 22:59:41 +00:00
|
|
|
compress: {
|
|
|
|
warnings: false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
2016-12-27 13:54:10 +00:00
|
|
|
// Extend config
|
|
|
|
if (typeof this.options.build.extend === 'function') {
|
|
|
|
this.options.build.extend(config, {
|
|
|
|
dev: this.dev,
|
|
|
|
isClient: true
|
|
|
|
})
|
|
|
|
}
|
2017-01-23 16:55:39 +00:00
|
|
|
// Webpack Bundle Analyzer
|
|
|
|
if (!this.dev && this.options.build.analyze) {
|
|
|
|
let options = {}
|
|
|
|
if (typeof this.options.build.analyze === 'object') {
|
|
|
|
options = this.options.build.analyze
|
|
|
|
}
|
|
|
|
config.plugins.push(
|
|
|
|
new BundleAnalyzerPlugin(options)
|
|
|
|
)
|
|
|
|
}
|
2016-11-09 22:59:41 +00:00
|
|
|
return config
|
2016-11-07 01:34:58 +00:00
|
|
|
}
|