21 / 07 / 18
package.json
"scripts": { "test": "echo \"Error: no test specified\" && exit 1", "build": "webpack", "watch": "webpack --watch", "dev": "webpack serve --open" },
webpack.config.js
module.exports = { mode: 'development', plugins: [ new webpack.HotModuleReplacementPlugin() ], devServer: { contentBase: './dist', hot: true } }
const express = require('express'); const webpack = require('webpack'); const webpackDevMiddleware = require('webpack-dev-middleware'); const app = express(); const config = require('./webpack.config.js'); const compiler = webpack(config); app.use(webpackDevMiddleware(compiler, { publicPath: config.output.publicPath })); app.listen(3000, function(){ console.log('Example app listenting on port 3000'); });
启动阶段
在文件系统进行编译,将初始代码webpack compile进行打包,打包后将编译好的文件传输给Bundle Server,Bundle Server相当于一个服务器,Bundle Server就可以run这个文件,以一个server的方式让这个文件通过浏览器可以访问的到
文件更新阶段
文件变动后,将代码webpack compile进行编译,编译后交给HMR Server,HMR Server就可以知道哪些模块发生了改变,HMR Server通知HMR Runtime,HMR Runtime更新代码,改变。(HMR Runtime在客户端,HMR Server在服务端)。