koa中webpack熱加載&&NODE_ENV配置

原文地址javascript

背景

目前在用koa+react+redux搭建一個微信後臺,須要用到webpack熱加載的方式方便進行開發,同時參考redux官方例子的配置方式,發現process.env.NODE_ENV一直是undefined,因此有了這篇文章。前端

本文主要介紹express以及koa中webpack熱加載的實現方式,同時解決process.env.NODE_ENV傳遞的問題。java

express熱加載

經過引入webpack熱加載的中間件便可,以下所示node

server.jsreact

var webpack = require('webpack')
var webpackDevMiddleware = require('webpack-dev-middleware')
var webpackHotMiddleware = require('webpack-hot-middleware')
var config = require('./webpack.config')
var compiler = webpack(config)
app.use(webpackDevMiddleware(compiler, {
  noInfo: true,
  publicPath: config.output.publicPath
}))
app.use(webpackHotMiddleware(compiler))

webpack.config.js中引入熱加載插件,添加entry入口,以及publicPathwebpack

var path = require('path')
var webpack = require('webpack')

module.exports = {
  devtool: 'cheap-module-eval-source-map',
  entry: [
    'webpack-hot-middleware/client',
    './index'
  ],
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js',
    publicPath: '/static/'
  },
  plugins: [
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.HotModuleReplacementPlugin()
  ],
  module: {
    loaders: [
      {
        test: /\.js$/,
        loaders: [ 'babel' ],
        exclude: /node_modules/,
        include: __dirname
      }
    ]
  }
}

完成以上文件的修改便可實現啓動node服務器實現熱加載web

koa中實現熱加載

在koa中若是直接使用express的加載方式,會形成每次觸發請求以及返回請求都會從新打包。express

解決辦法:直接使用koa-webpack-dev-middleware以及koa-webpack-dev-middleware,固然閱讀這兩個中間件源碼能夠發現它們的加載方式實際上仍是和express差很少,只是在執行中間件的時候就已經完成了熱加載的過程,以下:redux

var expressMiddleware = require('webpack-dev-middleware');

function middleware(doIt, req, res) {
  var originalEnd = res.end;

  return function (done) {
    res.end = function () {
      originalEnd.apply(this, arguments);
      done(null, 0);
    };
    doIt(req, res, function () {
      done(null, 1);
    })
  }
}
module.exports = function (compiler, option) {
  var doIt = expressMiddleware(compiler, option);
  return function*(next) {
    var ctx = this;
    var req = this.req;
    var runNext = yield middleware(doIt, req, {
      end: function (content) {
        ctx.body = content;
      },
      setHeader: function () {
        ctx.set.apply(ctx, arguments);
      }
    });
    if (runNext) {
      yield *next;
    }
  };
};

在koa中的使用方式服務器

var webpack = require('webpack');
var webpackDevMiddleware = require('koa-webpack-dev-middleware');
var webpackHotMiddleware = require('koa-webpack-hot-middleware');
var config = require('./webpack.config')
var compiler = webpack(config);
app.use(webpackDevMiddleware(compiler, {
  noInfo: true,
  publicPath: config.output.publicPath
}));
app.use(webpackHotMiddleware(compiler));

react中 process.env.NODE_ENV undefined

這個問題其實是由於react或者前端本省並不可能獲取到process.env.NODE_ENV,只能經過後臺傳遞參數到前端才能夠。

解決方法:在webpack中有一個插件能夠解決這個問題

webpack.config.js(列出部分代碼)

var env = process.env.NODE_ENV;
var config = {
  plugins: [
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin(),
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify(env)
    })
  ],
}
相關文章
相關標籤/搜索