From 4c21a1ac56b510710e21ecac6e2c7cbfd74dfff2 Mon Sep 17 00:00:00 2001 From: afc163 Date: Wed, 3 Feb 2016 13:34:18 +0800 Subject: [PATCH] Add hot-middleware --- nico.js | 39 +++++++++++++++++++++++---------------- package.json | 5 +++-- webpack.config.js | 8 ++++++-- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/nico.js b/nico.js index 1794d3cfa5..c14a45c323 100644 --- a/nico.js +++ b/nico.js @@ -6,6 +6,7 @@ var inspect = require('util').inspect; var Busboy = require('busboy'); var chalk = require('chalk'); var webpackMiddleware = require('webpack-dev-middleware'); +var webpackHotMiddleware = require('webpack-hot-middleware'); var webpackConfig = require('./webpack.config'); var webpackCompiler = webpack(webpackConfig); var handler; @@ -89,23 +90,29 @@ exports.middlewares = [ } }, { - name: 'webpackDevMiddleware', - filter: /\.(js|css)(\.map)?(\?.*)?$/, - handle: function(req, res, next) { - handler = handler || webpackMiddleware(webpackCompiler, { - publicPath: '/dist/', - lazy: false, - watchOptions: { - aggregateTimeout: 300, - poll: false - }, - noInfo: true - }); - try { - return handler(req, res, next); - } catch(e) {} + name: 'webpackDevMiddleware', + filter: /\.(js|css|json)(\.map)?(\?.*)?$/, + handle: function(req, res, next) { + handler = handler || webpackMiddleware(webpackCompiler, { + publicPath: webpackConfig.output.publicPath, + lazy: false, + watchOptions: { + aggregateTimeout: 300, + poll: false + }, + noInfo: true + }); + try { + return handler(req, res, next); + } catch(e) {} + } + }, + { + name: 'webpackHotMiddleware', + filter: /.*/, + handle: webpackHotMiddleware(webpackCompiler) } -}]; +]; exports.writers = [ 'nico-jsx.PageWriter', diff --git a/package.json b/package.json index 3292aacae0..ede6343eec 100644 --- a/package.json +++ b/package.json @@ -117,11 +117,12 @@ "values.js": "^1.0.3", "webpack": "^1.10.1", "webpack-babel-jest": "^1.0.0", - "webpack-dev-middleware": "^1.5.1" + "webpack-dev-middleware": "^1.5.1", + "webpack-hot-middleware": "^2.6.4" }, "scripts": { "babel": "babel components index.js --out-dir lib", - "start": "npm run clean && nico server --watch", + "start": "npm run clean && nico server", "clean": "rm -rf _site dist", "site": "npm run clean && webpack --config webpack.deploy.config.js && webpack --config webpack.antd.config.js && NODE_ENV=PRODUCTION nico build", "deploy": "rm -rf node_modules && node scripts/install.js && npm run just-deploy", diff --git a/webpack.config.js b/webpack.config.js index ee051dbd38..f6ca0c258c 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -4,7 +4,7 @@ var path = require('path'); var pkg = require('./package'); var entry = {}; -entry['demo'] = './scripts/demo.js'; +entry['demo'] = ['./scripts/demo.js', 'webpack-hot-middleware/client']; module.exports = { entry: entry, @@ -20,6 +20,7 @@ module.exports = { output: { path: path.join(process.cwd(), 'dist'), + publicPath: '/dist/', filename: '[name].js' }, @@ -57,7 +58,10 @@ module.exports = { }, plugins: [ - new ExtractTextPlugin('[name].css') + new ExtractTextPlugin('[name].css'), + new webpack.optimize.OccurenceOrderPlugin(), + new webpack.HotModuleReplacementPlugin(), + new webpack.NoErrorsPlugin() ], devtool: 'source-map'