vue-elm-devServer-config

开发脚本dev-server.js

The compiler object represents the fully configured Webpack environment. This object is built once upon starting Webpack, and is configured with all operational settings including options, loaders, and plugins.
A compilation object represents a single build of versioned assets

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
var config = require('../config')
if (!process.env.NODE_ENV) process.env.NODE_ENV = JSON.parse(config.dev.env.NODE_ENV) // "development"
var path = require('path')
var express = require('express')
var webpack = require('webpack')
var opn = require('opn')
var proxyMiddleware = require('http-proxy-middleware')
var webpackConfig = require('./webpack.dev.conf')
// default port where dev server listens for incoming traffic
var port = process.env.PORT || config.dev.port
// Define HTTP proxies to your custom API backend
// https://github.com/chimurai/http-proxy-middleware
var server = express()
var compiler = webpack(webpackConfig)
var devMiddleware = require('webpack-dev-middleware')(compiler, { // 将打包后文件放内存
publicPath: webpackConfig.output.publicPath, //和webpack的output的一样
stats: {
colors: true,
chunks: false
}
})
var hotMiddleware = require('webpack-hot-middleware')(compiler) //热重载
// force page reload when html-webpack-plugin template changes
//While running Webpack development middleware, a new compilation will be created each time a file change is detected, thus generating a new set of compiled assets.
compiler.plugin('compilation', function(compilation) { // webpack在compile时的回调
//The Compiler has emitted all assets.
compilation.plugin('html-webpack-plugin-after-emit', function(data, cb) { //文件由webpack生成后
hotMiddleware.publish({
action: 'reload'
})
cb()
})
})
var context = config.dev.context
var proxypath = config.dev.proxypath
var options = {
target: proxypath,
changeOrigin: true,
}
if (context.length) {
server.use(proxyMiddleware(context, options))
}
server.use(proxyMiddleware('/payapi', {
target: 'https://pay.ele.me',
changeOrigin: true,
}))
server.use(proxyMiddleware('/m.ele.me@json', {
target: 'https://crayfish.elemecdn.com',
changeOrigin: true,
}))
// handle fallback for HTML5 history API
server.use(require('connect-history-api-fallback')())
serve webpack bundle output
server.use(devMiddleware)
enable hot-reload and state-preserving
compilation error display
server.use(hotMiddleware)
// serve pure static assets
var staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory)
server.use(staticPath, express.static('./static'))
module.exports = server.listen(port, function(err) {
if (err) {
console.log(err)
return
}
var uri = 'http://localhost:' + port
console.log('Listening at ' + uri + '\n')
// when env is testing, don't need open it
if (process.env.NODE_ENV !== 'testing') {
opn(uri)
}
})

webpack1->webpack2迁移文档

https://webpack.js.org/guides/migrating/