webpack-dev-server介绍

2017-04-24  本文已影响0人  壮哉我大前端

在react项目中使用 webpack-dev-server

1、什么是 webpack-dev-server

The webpack-dev-server is a little node.js Express server, which uses the webpack-dev-middleware to serve a webpack bundle

2、安装与运行

Using this config webpack-dev-server will serve the static files in your build folder. It’ll watch your source files for changes and when changes are made the bundle will be recompiled. This modified bundle is served from memory at the relative path specified in publicPath (see API). It will not be written to your configured output directory. Where a bundle already exists at the same url path the bundle in memory will take precedence (by default)

安装

配置

module.exports = {
    context: __dirname + "/app",
    entry: [
        "./entry"
    ],
    output: {
        path: __dirname + "/build",
        publicPath: "/build",
        filename: "bundle.js"
    }
};
 "scripts": {
    "iframe-start": "webpack-dev-server",
    "inline-start": "webpack-dev-server --inline"
  },

运行

The webpack-dev-server supports multiple modes to automatic refresh the page:
Iframe mode (page is embedded in an iframe and reloaded on change)
Inline mode (a small webpack-dev-server client entry is added to the bundle which refresh the page on change)

访问页面

3.拓展:使用node自己配置一个server

Hot Module Replacement with node.js API
Similar to the inline mode the user must make changes to the webpack configuration.
Three changes are needed:
1.add an entry point to the webpack configuration: webpack/hot/dev-server.
2.add the new webpack.HotModuleReplacementPlugin() to the webpack configuration.
3.add hot: true to the webpack-dev-server configuration to enable HMR on the server.

创建一个server.js

var webpack = require('webpack')
var config = require('./webpack.config.js')
/**
 * 没有添加这段代码时,是不会自动刷新页面的(有的时候不希望自动刷新)
 * 添加了这段代码,就需要配置webpack.config.js
 */
//config.entry.unshift("webpack-dev-server/client?http://localhost:3000/", "webpack/hot/dev-server")
var WebpackDevServer = require('webpack-dev-server')
new WebpackDevServer(webpack(config), {
    publicPath: config.output.publicPath,
    hot: true,
    noInfo: true,
    historyApiFallback: true
}).listen(3000, '127.0.0.1', function( err, result ){
    if ( err ) {
        console.log( err )
    }
    console.log( 'Listening at localhost:3000' );
})

配置webpack.config.js

var webpack = require('webpack');
module.exports = {
    ......
    plugins: [
        new webpack.HotModuleReplacementPlugin()
    ]
};
上一篇 下一篇

猜你喜欢

热点阅读