webpack-dev-server 使用方法

1、使用命令行形式

第一步:安装webpack-dev-server node模块 npm install webpack-dev-server --save-dev

第二步:在webpack.config.js中添加

devServer:{
        historyApiFallback:true,
        hot:true,
        inline:true,
        progress:true,
    },
     plugins: [
         new webpack.optimize.UglifyJsPlugin({
             compress: {
                 warnings: false,
             },
             output: {
                 comments: false,
             },
         }),
         new webpack.DefinePlugin({
             'process.env.NODE.ENV':"development"
         }),
        new webpack.HotModuleReplacementPlugin(),  
    ]

第三步:package.json文件的script中添加:["start": "webpack-dev-server --inline --hot --port 8088"]

第四步: 使用npm start 运行


2、使用node API的形式


第一步:安装webpack-dev-server node模块 npm install webpack-dev-server --save-dev

第二步:在webpack.config.js中的entry中添加

entry: [
             'webpack-dev-server/client?http://localhost:8088/',
             'webpack/hot/dev-server',
             './src/static/js/app.js'
     ],

第三步:在plugins中添加

new webpack.DefinePlugin({
            'process.env.NODE.ENV':"development"
     }),
     new webpack.HotModuleReplacementPlugin(),


第四步:新建webpack-dev-server.js文件,与webpack.config.js同级目录

var config = require("./webpack.config.js");
    var webpack = require('webpack');
    var WebpackDevServer = require('webpack-dev-server');

    //config.entry.app.unshift("webpack-dev-server/client?http://localhost:8088/");

    var compiler = webpack(config);
    var server = new WebpackDevServer(compiler, {
    // webpack-dev-server options

    //contentBase: "dist/",
    // Can also be an array, or: contentBase: "http://localhost/",

    hot: true,
    // Enable special support for Hot Module Replacement
    // Page is no longer updated, but a "webpackHotUpdate" message is send to the content
    // Use "webpack/hot/dev-server" as additional module in your entry point
    // Note: this does _not_ add the `HotModuleReplacementPlugin` like the CLI option does.

    // Set this as true if you want to access dev server from arbitrary url.
    // This is handy if you are using a html5 router.
    historyApiFallback: false,

    // Set this if you want to enable gzip compression for assets
    compress: true,

    // Set this if you want webpack-dev-server to delegate a single path to an arbitrary server.
    // Use "**" to proxy all paths to the specified server.
    // This is useful if you want to get rid of 'http://localhost:8080/' in script[src],
    // and has many other use cases (see https://github.com/webpack/webpack-dev-server/pull/127 ).
    /*proxy: {
        "**": "http://localhost:8088"
    },*/

    setup: function(app) {
        // Here you can access the Express app object and add your own custom middleware to it.
        // For example, to define custom handlers for some paths:
        // app.get('/some/path', function(req, res) {
        //   res.json({ custom: 'response' });
        // });
    },

    // pass [static options](http://expressjs.com/en/4x/api.html#express.static) to inner express server
    staticOptions: {
    },

    // webpack-dev-middleware options
    quiet: false,
    noInfo: false,
    lazy: true,
    filename: "app.bundle.js",
    watchOptions: {
        aggregateTimeout: 300,
        poll: 1000
    },
    // It's a required option.
    publicPath: "/assets/",
    headers: { "X-Custom-Header": "yes" },
    stats: { colors: true }
});
server.listen(8088, "localhost", function() {});
// server.close();


第五步:在index.html中添加
<script src="http://localhost:8088/webpack-dev-server.js"></script>

第六步:运行 node webpack-dev-server.js
              或者
             将此命令添加到package.json文件的script中:["start": "node webpack-dev-server.js"]


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值