借助webpackdevmiddleware和webpackhotmiddleware实现热替换功能的服务器

37次阅读

共计 638 个字符,预计需要花费 2 分钟才能阅读完成。

  1. 修改 webpack 的配置:
    entry: [‘webpack-hot-middleware/client?noInfo=true&reload=true’, ‘./src/main.js’],
    plugins: [new webpack.HotModuleReplacementPlugin()]
  2. 服务器代码的实现:
    const express = require(‘express’)
    const webpack = require(‘webpack’)
    const config = require(‘./webpack.config.js’)
    const webpackDevMiddleware = require(‘webpack-dev-middleware’)
    const webpackHotMiddleware = require(‘webpack-hot-middleware’)

    const app = new express()
    const compiler = webpack(config)

    app.use(webpackDevMiddleware(compiler, {

    publicPath: config.output.publicPath

    }))
    app.use(webpackHotMiddleware(compiler, {

    log: (info) => console.log(info),
    heartbeat: 1000

    }))

    app.listen(8080, () => {

    console.log('server started!')

    })

正文完
 0