-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.dev.js
More file actions
98 lines (79 loc) · 2.38 KB
/
webpack.dev.js
File metadata and controls
98 lines (79 loc) · 2.38 KB
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
88
89
90
91
92
93
94
95
96
97
98
const { resolve } = require('path');
const webpack = require('webpack');
const config = require('./webpack.base');
const devServer = require('webpack-dev-server');
const { CheckerPlugin } = require('awesome-typescript-loader');
const {
DEV_SERVER_PORT,
PUBLIC_PATH,
EXTERNAL_PROXY_PORT
} = require('./config');
const ROOT = resolve(__dirname);
// set mode
config.mode = 'development';
// source map
config.devtool = 'source-map';
// config.devtool = 'inline-source-map';
// add hmr plugins
[].push.apply(config.plugins, [
// enable HMR globally
new webpack.HotModuleReplacementPlugin(),
// prints more readable module names in the browser console on HMR updates
new webpack.NamedModulesPlugin(),
// do not emit compiled assets that include errors
new webpack.NoEmitOnErrorsPlugin(),
// async error reporting
new CheckerPlugin()
]);
// add HMR stuff to entry
if (typeof config.entry === 'object'){
Object.keys(config.entry).forEach(entryKey => {
config.entry[entryKey] = constructHMREntry(config.entry[entryKey])
})
} else if (typeof config.entry === 'string'){
config.entry = constructHMREntry(config.entry)
}
function constructHMREntry(entry){
return [
// bundle the client for webpack-dev-server
// and connect to the provided endpoint
`webpack-dev-server/client?http://localhost:${DEV_SERVER_PORT}`,
// bundle the client for hot reloading
// only- means to only hot reload for successful updates
'webpack/hot/only-dev-server',
entry
];
}
// css
config.module.rules[1].use.unshift('style-loader');
// necessary for HMR to know where to load the hot update chunks
config.output.publicPath = PUBLIC_PATH;
// setup dev server config
const devServerConfig = {
hot: true,
publicPath: PUBLIC_PATH,
contentBase: ROOT,
headers: {'Access-Control-Allow-Origin': '*'},
// respond to 404s with index.html
historyApiFallback: true,
// logging
stats: 'errors-only',
clientLogLevel: 'warning',
proxy: {
// static route for files
'/static/': {
target: `http://localhost:${EXTERNAL_PROXY_PORT}`,
},
// anything prefixed with api, redirect to server
'/api/': {
target: `http://localhost:${EXTERNAL_PROXY_PORT}`,
}
}
};
// start server
new devServer(webpack(config), devServerConfig).listen(DEV_SERVER_PORT, '0.0.0.0', err => {
if (err){
throw new Error('webpack dev server error', err);
}
console.log(`Webpack dev server listening on port ${DEV_SERVER_PORT}`);
});