Skip to content

Commit 0b49f91

Browse files
author
Sandra Freihofer
committed
Switch to webpack-dev-server
See vuejs-templates/webpack#975
1 parent 06fba51 commit 0b49f91

File tree

8 files changed

+586
-348
lines changed

8 files changed

+586
-348
lines changed

build/dev-client.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

build/dev-server.js

Lines changed: 0 additions & 90 deletions
This file was deleted.

build/webpack.base.conf.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ module.exports = {
3434
enforce: 'pre',
3535
include: [resolve('src'), resolve('test')],
3636
options: {
37-
formatter: require('eslint-friendly-formatter')
37+
formatter: require('eslint-friendly-formatter'),
38+
emitWarning: !config.dev.showEslintErrorsInOverlay
3839
}
3940
},
4041
{

build/webpack.dev.conf.js

100644100755
Lines changed: 75 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,92 @@
1-
var utils = require('./utils')
2-
var webpack = require('webpack')
3-
var config = require('../config')
4-
var merge = require('webpack-merge')
5-
var baseWebpackConfig = require('./webpack.base.conf')
6-
var HtmlWebpackPlugin = require('html-webpack-plugin')
7-
var FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
1+
'use strict'
2+
const utils = require('./utils')
3+
const webpack = require('webpack')
4+
const config = require('../config')
5+
const merge = require('webpack-merge')
6+
const path = require('path')
7+
const baseWebpackConfig = require('./webpack.base.conf')
8+
const CopyWebpackPlugin = require('copy-webpack-plugin')
9+
const HtmlWebpackPlugin = require('html-webpack-plugin')
10+
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
11+
const portfinder = require('portfinder')
812

9-
// add hot-reload related code to entry chunks
10-
Object.keys(baseWebpackConfig.entry).forEach(function (name) {
11-
baseWebpackConfig.entry[name] = ['./build/dev-client'].concat(baseWebpackConfig.entry[name])
12-
})
13+
const HOST = process.env.HOST
14+
const PORT = process.env.PORT && Number(process.env.PORT)
1315

14-
module.exports = merge(baseWebpackConfig, {
16+
const devWebpackConfig = merge(baseWebpackConfig, {
1517
module: {
16-
rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap })
18+
rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap, usePostCSS: true })
1719
},
1820
// cheap-module-eval-source-map is faster for development
19-
devtool: '#cheap-module-eval-source-map',
21+
devtool: config.dev.devtool,
22+
23+
// these devServer options should be customized in /config/index.js
24+
devServer: {
25+
clientLogLevel: 'warning',
26+
historyApiFallback: {
27+
rewrites: [
28+
{ from: /.*/, to: path.posix.join(config.dev.assetsPublicPath, 'index.html') },
29+
],
30+
},
31+
hot: true,
32+
contentBase: false, // since we use CopyWebpackPlugin.
33+
compress: true,
34+
host: HOST || config.dev.host,
35+
port: PORT || config.dev.port,
36+
open: config.dev.autoOpenBrowser,
37+
overlay: config.dev.errorOverlay
38+
? { warnings: false, errors: true }
39+
: false,
40+
publicPath: config.dev.assetsPublicPath,
41+
proxy: config.dev.proxyTable,
42+
quiet: true, // necessary for FriendlyErrorsPlugin
43+
watchOptions: {
44+
poll: config.dev.poll,
45+
}
46+
},
2047
plugins: [
2148
new webpack.DefinePlugin({
22-
'process.env': config.dev.env
49+
'process.env': require('../config/dev.env')
2350
}),
24-
// https://github.com/glenjamin/webpack-hot-middleware#installation--usage
2551
new webpack.HotModuleReplacementPlugin(),
52+
new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.
2653
new webpack.NoEmitOnErrorsPlugin(),
2754
// https://github.com/ampedandwired/html-webpack-plugin
2855
new HtmlWebpackPlugin({
2956
filename: 'index.html',
30-
template: 'index-template.html',
57+
template: 'index.html',
3158
inject: true
3259
}),
33-
new FriendlyErrorsPlugin()
60+
// copy custom static assets
61+
new CopyWebpackPlugin([
62+
{
63+
from: path.resolve(__dirname, '../static'),
64+
to: config.dev.assetsSubDirectory,
65+
ignore: ['.*']
66+
}
67+
])
3468
]
3569
})
70+
71+
module.exports = new Promise((resolve, reject) => {
72+
portfinder.basePort = process.env.PORT || config.dev.port
73+
portfinder.getPort((err, port) => {
74+
if (err) {
75+
reject(err)
76+
} else {
77+
// publish the new Port, necessary for e2e tests
78+
process.env.PORT = port
79+
// add port to devServer config
80+
devWebpackConfig.devServer.port = port
81+
82+
// Add FriendlyErrorsPlugin
83+
devWebpackConfig.plugins.push(new FriendlyErrorsPlugin({
84+
compilationSuccessInfo: {
85+
messages: [`Your application is running here: http://${devWebpackConfig.devServer.host}:${port}`],
86+
}
87+
}))
88+
89+
resolve(devWebpackConfig)
90+
}
91+
})
92+
})

build/webpack.prod.conf.js

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
1-
var path = require('path')
2-
var utils = require('./utils')
3-
var webpack = require('webpack')
4-
var config = require('../config')
5-
var merge = require('webpack-merge')
6-
var baseWebpackConfig = require('./webpack.base.conf')
7-
var CopyWebpackPlugin = require('copy-webpack-plugin')
8-
var HtmlWebpackPlugin = require('html-webpack-plugin')
9-
var ExtractTextPlugin = require('extract-text-webpack-plugin')
10-
var OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
1+
'use strict'
2+
const path = require('path')
3+
const utils = require('./utils')
4+
const webpack = require('webpack')
5+
const config = require('../config')
6+
const merge = require('webpack-merge')
7+
const baseWebpackConfig = require('./webpack.base.conf')
8+
const CopyWebpackPlugin = require('copy-webpack-plugin')
9+
const HtmlWebpackPlugin = require('html-webpack-plugin')
10+
const ExtractTextPlugin = require('extract-text-webpack-plugin')
11+
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
12+
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
1113

12-
var env = config.build.env
14+
const env = require('../config/prod.env')
1315

14-
var webpackConfig = merge(baseWebpackConfig, {
16+
const webpackConfig = merge(baseWebpackConfig, {
1517
module: {
1618
rules: utils.styleLoaders({
1719
sourceMap: config.build.productionSourceMap,
18-
extract: true
20+
extract: true,
21+
usePostCSS: true
1922
})
2023
},
21-
devtool: config.build.productionSourceMap ? '#source-map' : false,
24+
devtool: config.build.productionSourceMap ? config.build.devtool : false,
2225
output: {
2326
path: config.build.assetsRoot,
2427
filename: utils.assetsPath('js/[name].[chunkhash].js'),
@@ -29,22 +32,30 @@ var webpackConfig = merge(baseWebpackConfig, {
2932
new webpack.DefinePlugin({
3033
'process.env': env
3134
}),
32-
new webpack.optimize.UglifyJsPlugin({
33-
compress: {
34-
warnings: false
35+
new UglifyJsPlugin({
36+
uglifyOptions: {
37+
compress: {
38+
warnings: false
39+
}
3540
},
36-
sourceMap: true
41+
sourceMap: config.build.productionSourceMap,
42+
parallel: true
3743
}),
3844
// extract css into its own file
3945
new ExtractTextPlugin({
40-
filename: utils.assetsPath('css/[name].[contenthash].css')
46+
filename: utils.assetsPath('css/[name].[contenthash].css'),
47+
// Setting the following option to `false` will not extract CSS from codesplit chunks.
48+
// Their CSS will instead be inserted dynamically with style-loader when the codesplit chunk has been loaded by webpack.
49+
// It's currently set to `true` because we are seeing that sourcemaps are included in the codesplit bundle as well when it's `false`,
50+
// increasing file size: https://github.com/vuejs-templates/webpack/issues/1110
51+
allChunks: true,
4152
}),
4253
// Compress extracted CSS. We are using this plugin so that possible
4354
// duplicated CSS from different components can be deduped.
4455
new OptimizeCSSPlugin({
45-
cssProcessorOptions: {
46-
safe: true
47-
}
56+
cssProcessorOptions: config.build.productionSourceMap
57+
? { safe: true, map: { inline: false } }
58+
: { safe: true }
4859
}),
4960
// generate dist index.html with correct asset hash for caching.
5061
// you can customize output by editing /index.html
@@ -63,14 +74,18 @@ var webpackConfig = merge(baseWebpackConfig, {
6374
// necessary to consistently work with multiple chunks via CommonsChunkPlugin
6475
chunksSortMode: 'dependency'
6576
}),
77+
// keep module.id stable when vendor modules does not change
78+
new webpack.HashedModuleIdsPlugin(),
79+
// enable scope hoisting
80+
new webpack.optimize.ModuleConcatenationPlugin(),
6681
// split your library css/js into separate files
6782
new webpack.optimize.CommonsChunkPlugin({
6883
name: 'v-chacheli'
6984
}),
7085
// split vendor js into its own file
7186
new webpack.optimize.CommonsChunkPlugin({
7287
name: 'vendor',
73-
minChunks: function (module, count) {
88+
minChunks (module) {
7489
// any required modules inside node_modules are extracted to vendor
7590
return (
7691
module.resource &&
@@ -99,7 +114,7 @@ var webpackConfig = merge(baseWebpackConfig, {
99114
})
100115

101116
if (config.build.productionGzip) {
102-
var CompressionWebpackPlugin = require('compression-webpack-plugin')
117+
const CompressionWebpackPlugin = require('compression-webpack-plugin')
103118

104119
webpackConfig.plugins.push(
105120
new CompressionWebpackPlugin({
@@ -117,7 +132,7 @@ if (config.build.productionGzip) {
117132
}
118133

119134
if (config.build.bundleAnalyzerReport) {
120-
var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
135+
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
121136
webpackConfig.plugins.push(new BundleAnalyzerPlugin())
122137
}
123138

0 commit comments

Comments
 (0)