Skip to content

Commit 937dffc

Browse files
authored
Switch to webpack-dev-server (close vuejs-templates#964) (vuejs-templates#975)
* Finished testable version. * close vuejs-templates#960 * fix node-notifier version * remove console.log * moved general dependency out of unit test-only dependency block. * fix typo * ignore /test folder * make HMR work correctly. * improve console messages for HMR - now show filenames of replaced modules. * fix typo in eslint-loader config * move imports for the env files from /config/index.js directly into the webpack config. Reasoning: thosen file imports are not configuration, so they don't belong inside of config/index.js * fix wrong overlay config
1 parent 89c6fb1 commit 937dffc

11 files changed

+134
-158
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
node_modules
22
.DS_Store
33
docs/_book
4-
test
4+
test/

template/build/dev-client.js

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

template/build/dev-server.js

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

template/build/logo.png

6.69 KB
Loading

template/build/utils.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const path = require('path')
33
const config = require('../config')
44
const ExtractTextPlugin = require('extract-text-webpack-plugin')
5+
const pkg = require('../package.json')
56

67
exports.assetsPath = function (_path) {
78
const assetsSubDirectory = process.env.NODE_ENV === 'production'
@@ -70,3 +71,22 @@ exports.styleLoaders = function (options) {
7071
}
7172
return output
7273
}
74+
75+
exports.createNotifierCallback = function () {
76+
const notifier = require('node-notifier')
77+
78+
return (severity, errors) => {
79+
if (severity !== 'error') {
80+
return
81+
}
82+
const error = errors[0]
83+
84+
const filename = error.file.split('!').pop()
85+
notifier.notify({
86+
title: pkg.name,
87+
message: severity + ': ' + error.name,
88+
subtitle: filename || '',
89+
icon: path.join(__dirname, 'logo.png')
90+
})
91+
}
92+
}

template/build/webpack.base.conf.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@ module.exports = {
3131
module: {
3232
rules: [
3333
{{#lint}}
34-
{
34+
...(config.dev.useEslint? [{
3535
test: /\.(js|vue)$/,
3636
loader: 'eslint-loader',
3737
enforce: 'pre',
3838
include: [resolve('src'), resolve('test')],
3939
options: {
40-
formatter: require('eslint-friendly-formatter')
40+
formatter: require('eslint-friendly-formatter'),
41+
emitWarning: !config.dev.showEslintErrorsInOverlay
4142
}
42-
},
43+
}] : []),
4344
{{/lint}}
4445
{
4546
test: /\.vue$/,

template/build/webpack.dev.conf.js

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,38 @@ const merge = require('webpack-merge')
66
const baseWebpackConfig = require('./webpack.base.conf')
77
const HtmlWebpackPlugin = require('html-webpack-plugin')
88
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
9+
const portfinder = require('portfinder')
910

10-
// add hot-reload related code to entry chunks
11-
Object.keys(baseWebpackConfig.entry).forEach(function (name) {
12-
baseWebpackConfig.entry[name] = ['./build/dev-client'].concat(baseWebpackConfig.entry[name])
13-
})
14-
15-
module.exports = merge(baseWebpackConfig, {
11+
const devWebpackConfig = merge(baseWebpackConfig, {
1612
module: {
1713
rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap })
1814
},
1915
// cheap-module-eval-source-map is faster for development
2016
devtool: '#cheap-module-eval-source-map',
17+
18+
// these devServer options should be customized in /config/index.js
19+
devServer: {
20+
hot: true,
21+
host: process.env.HOST || config.dev.host,
22+
port: process.env.PORT || config.dev.port,
23+
open: config.dev.autoOpenBrowser,
24+
overlay: config.dev.errorOverlay ? {
25+
warnings: false,
26+
errors: true,
27+
} : false,
28+
publicPath: config.dev.assetsPublicPath,
29+
proxy: config.dev.proxyTable,
30+
quiet: true, // necessary for FriendlyErrorsPlugin
31+
watchOptions: {
32+
poll: config.dev.poll,
33+
}
34+
},
2135
plugins: [
2236
new webpack.DefinePlugin({
23-
'process.env': config.dev.env
24-
}),
25-
// https://github.com/glenjamin/webpack-hot-middleware#installation--usage
37+
'process.env': require('../config/dev.env')
38+
}),
2639
new webpack.HotModuleReplacementPlugin(),
40+
new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.
2741
new webpack.NoEmitOnErrorsPlugin(),
2842
// https://github.com/ampedandwired/html-webpack-plugin
2943
new HtmlWebpackPlugin({
@@ -34,3 +48,29 @@ module.exports = merge(baseWebpackConfig, {
3448
new FriendlyErrorsPlugin()
3549
]
3650
})
51+
52+
module.exports = new Promise((resolve, reject) => {
53+
portfinder.basePort = process.env.PORT || config.dev.port
54+
portfinder.getPort((err, port) => {
55+
if (err) {
56+
reject(err)
57+
} else {
58+
// publish the new Port, necessary for e2e tests
59+
process.env.PORT = port
60+
// add port to devServer config
61+
devWebpackConfig.devServer.port = port
62+
63+
// Add FriendlyErrorsPlugin
64+
devWebpackConfig.plugins.push(new FriendlyErrorsPlugin({
65+
compilationSuccessInfo: {
66+
messages: [`Your application is running here: http://${config.dev.host}:${port}`],
67+
},
68+
onErrors: config.dev.notifyOnErrors
69+
? utils.createNotifierCallback()
70+
: undefined
71+
}))
72+
73+
resolve(devWebpackConfig)
74+
}
75+
})
76+
})

template/build/webpack.prod.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
1212

1313
const env = {{#if_or unit e2e}}process.env.NODE_ENV === 'testing'
1414
? require('../config/test.env')
15-
: {{/if_or}}config.build.env
15+
: {{/if_or}}require('../config/prod.env')
1616

1717
const webpackConfig = merge(baseWebpackConfig, {
1818
module: {

template/config/index.js

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,58 @@
55
const path = require('path')
66

77
module.exports = {
8+
dev: {
9+
host: 'localhost', // can be overwritten by process.env.HOST
10+
port: 8080, // can be overwritten by process.env.HOST, if port is in use, a free one will be determined
11+
12+
// Paths
13+
assetsSubDirectory: 'static',
14+
assetsPublicPath: '/',
15+
proxyTable: {},
16+
17+
// Various Dev Server settings
18+
autoOpenBrowser: false,
19+
errorOverlay: true,
20+
notifyOnErrors: true,
21+
poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-
22+
23+
// Use Eslint Loader?
24+
// If true, your code will be linted during bundling and
25+
// linting errors and warings will be shown in the console.
26+
useEslint: true,
27+
// If true, eslint errors and warings will also be shown in the error overlay
28+
// in the browser.
29+
showEslintErrorsInOverlay: false,
30+
31+
// CSS Sourcemaps off by default because relative paths are "buggy"
32+
// with this option, according to the CSS-Loader README
33+
// (https://github.com/webpack/css-loader#sourcemaps)
34+
// In our experience, they generally work as expected,
35+
// just be aware of this issue when enabling this option.
36+
cssSourceMap: false
37+
},
838
build: {
9-
env: require('./prod.env'),
39+
// Template for index.html
1040
index: path.resolve(__dirname, '../dist/index.html'),
41+
42+
// Paths
1143
assetsRoot: path.resolve(__dirname, '../dist'),
1244
assetsSubDirectory: 'static',
1345
assetsPublicPath: '/',
46+
1447
productionSourceMap: true,
1548
// Gzip off by default as many popular static hosts such as
1649
// Surge or Netlify already gzip all static assets for you.
1750
// Before setting to `true`, make sure to:
1851
// npm install --save-dev compression-webpack-plugin
52+
1953
productionGzip: false,
2054
productionGzipExtensions: ['js', 'css'],
2155
// Run the build command with an extra argument to
2256
// View the bundle analyzer report after build finishes:
2357
// `npm run build --report`
2458
// Set to `true` or `false` to always turn it on or off
59+
2560
bundleAnalyzerReport: process.env.npm_config_report
26-
},
27-
dev: {
28-
env: require('./dev.env'),
29-
port: process.env.PORT || 8080,
30-
autoOpenBrowser: true,
31-
assetsSubDirectory: 'static',
32-
assetsPublicPath: '/',
33-
proxyTable: {},
34-
// CSS Sourcemaps off by default because relative paths are "buggy"
35-
// with this option, according to the CSS-Loader README
36-
// (https://github.com/webpack/css-loader#sourcemaps)
37-
// In our experience, they generally work as expected,
38-
// just be aware of this issue when enabling this option.
39-
cssSourceMap: false
4061
}
4162
}

template/package.json

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"author": "{{ author }}",
66
"private": true,
77
"scripts": {
8-
"dev": "node build/dev-server.js",
8+
"dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
99
"start": "npm run dev",
1010
"build": "node build/build.js"{{#unit}},
1111
"unit": "cross-env BABEL_ENV=test karma start test/unit/karma.conf.js --single-run"{{/unit}}{{#e2e}},
@@ -29,7 +29,6 @@
2929
"babel-preset-stage-2": "^6.22.0",
3030
"babel-register": "^6.22.0",
3131
"chalk": "^2.0.1",
32-
"connect-history-api-fallback": "^1.3.0",
3332
"copy-webpack-plugin": "^4.0.1",
3433
"css-loader": "^0.28.0",
3534
{{#lint}}
@@ -51,12 +50,10 @@
5150
{{/if_eq}}
5251
{{/lint}}
5352
"eventsource-polyfill": "^0.9.6",
54-
"express": "^4.14.1",
5553
"extract-text-webpack-plugin": "^3.0.0",
5654
"file-loader": "^1.1.4",
5755
"friendly-errors-webpack-plugin": "^1.6.1",
5856
"html-webpack-plugin": "^2.30.1",
59-
"http-proxy-middleware": "^0.17.3",
6057
"webpack-bundle-analyzer": "^2.9.0",
6158
{{#unit}}
6259
"cross-env": "^5.0.1",
@@ -77,6 +74,7 @@
7774
"babel-plugin-istanbul": "^4.1.1",
7875
"phantomjs-prebuilt": "^2.1.14",
7976
{{/unit}}
77+
"node-notifier": "^5.1.2",
8078
{{#e2e}}
8179
"chromedriver": "^2.27.2",
8280
"cross-spawn": "^5.0.1",
@@ -85,7 +83,6 @@
8583
{{/e2e}}
8684
"semver": "^5.3.0",
8785
"shelljs": "^0.7.6",
88-
"opn": "^5.1.0",
8986
"optimize-css-assets-webpack-plugin": "^3.2.0",
9087
"ora": "^1.2.0",
9188
"rimraf": "^2.6.0",
@@ -95,8 +92,7 @@
9592
"vue-template-compiler": "^2.5.2",
9693
"portfinder": "^1.0.13",
9794
"webpack": "^3.6.0",
98-
"webpack-dev-middleware": "^1.12.0",
99-
"webpack-hot-middleware": "^2.18.2",
95+
"webpack-dev-server": "^2.9.1",
10096
"webpack-merge": "^4.1.0"
10197
},
10298
"engines": {

0 commit comments

Comments
 (0)