Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

Commit

Permalink
update to webpack 2
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewagain committed Feb 8, 2017
1 parent 48f0d9c commit a085f31
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 51 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
node_modules
www/bundle.js
*/www/bundle.js
*.log
14 changes: 11 additions & 3 deletions middleware/README.md
Expand Up @@ -3,21 +3,29 @@ HMR w/ webpack-hot-middleware and express
This project is a simple example of how to use [webpack-hot-middleware](https://github.com/glenjamin/webpack-hot-middleware) to enable HMR on your express server.



Setup
---

npm install
```
npm install
```



Running
---

npm start
```
npm start
```

Open [http://localhost:3000](http://localhost:3000).



Discussion
---
The `npm start` command runs: `node server.js`. This starts the express server which is configured to use `webpack-hot-middleware`.

This is ideal when you are already using or planning to use express to host your web page.
This is ideal when you are already using or planning to use express.
2 changes: 1 addition & 1 deletion middleware/js/box-creator.js
Expand Up @@ -6,7 +6,7 @@
* To prevent a new <div> from being created every time this module is reloaded,
* `module.hot.dispose` is used to remove the old <div>.
*/
var sideEffectNode = document.createElement('div');
const sideEffectNode = document.createElement('div');
sideEffectNode.setAttribute('style', 'background-color: lightblue; padding: 20px; margin: 10px;');
sideEffectNode.textContent = 'Side Effect';
document.body.appendChild(sideEffectNode);
Expand Down
4 changes: 2 additions & 2 deletions middleware/js/index.js
@@ -1,10 +1,10 @@
var mathDoer = require('./math-doer');
const mathDoer = require('./math-doer');

// This require() statement has side effects.
require('./box-creator');

// mathDoer just does some math on 2 inputs.
var result = mathDoer(7, 3);
const result = mathDoer(7, 3);

// Console.log statements are reprinted on every reload.
console.log('Math result:' + result);
Expand Down
8 changes: 4 additions & 4 deletions middleware/package.json
Expand Up @@ -21,9 +21,9 @@
"url": "git://github.com/ahfarmer/webpack-hmr-3-ways.git"
},
"dependencies": {
"express": "^4.13.3",
"webpack": "^1.12.2",
"webpack-dev-middleware": "^1.2.0",
"webpack-hot-middleware": "^2.4.1"
"express": "^4.14.1",
"webpack": "^2.2.1",
"webpack-dev-middleware": "^1.10.0",
"webpack-hot-middleware": "^2.16.1"
}
}
23 changes: 11 additions & 12 deletions middleware/server.js
@@ -1,12 +1,11 @@
var express = require('express');
var webpackDevMiddleware = require("webpack-dev-middleware");
var webpackHotMiddleware = require("webpack-hot-middleware");
var webpack = require('webpack');
var webpackConfig = require('./webpack.config.js');
var path = require('path');
var app = express();
const express = require('express');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const webpack = require('webpack');
const webpackConfig = require('./webpack.config.js');
const app = express();

var compiler = webpack(webpackConfig);
const compiler = webpack(webpackConfig);

app.use(webpackDevMiddleware(compiler, {
hot: true,
Expand All @@ -24,13 +23,13 @@ app.use(webpackHotMiddleware(compiler, {
heartbeat: 10 * 1000,
}));

app.get('/', function (req, res) {
app.get('/', function(req, res) {
res.send('<body>Hello World<script src=\'assets/bundle.js\'></script></body>');
});

var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
const server = app.listen(3000, function() {
const host = server.address().address;
const port = server.address().port;

console.log('Example app listening at http://%s:%s', host, port);
});
12 changes: 5 additions & 7 deletions middleware/webpack.config.js
@@ -1,7 +1,7 @@
var path = require('path');
var webpack = require('webpack');
const path = require('path');
const webpack = require('webpack');

var config = {
module.exports = {
context: path.join(__dirname, 'js'),
entry: [
'webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000',
Expand All @@ -13,9 +13,7 @@ var config = {
publicPath: '/assets/',
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin()
]
new webpack.NoEmitOnErrorsPlugin(),
],
};
module.exports = config;
2 changes: 1 addition & 1 deletion server-api/js/box-creator.js
Expand Up @@ -6,7 +6,7 @@
* To prevent a new <div> from being created every time this module is reloaded,
* `module.hot.dispose` is used to remove the old <div>.
*/
var sideEffectNode = document.createElement('div');
const sideEffectNode = document.createElement('div');
sideEffectNode.setAttribute('style', 'background-color: lightblue; padding: 20px; margin: 10px;');
sideEffectNode.textContent = 'Side Effect';
document.body.appendChild(sideEffectNode);
Expand Down
4 changes: 2 additions & 2 deletions server-api/js/index.js
@@ -1,10 +1,10 @@
var mathDoer = require('./math-doer');
const mathDoer = require('./math-doer');

// This require() statement has side effects.
require('./box-creator');

// mathDoer just does some math on 2 inputs.
var result = mathDoer(7, 3);
const result = mathDoer(7, 3);

// Console.log statements are reprinted on every reload.
console.log('Math result:' + result);
Expand Down
4 changes: 2 additions & 2 deletions server-api/package.json
Expand Up @@ -20,7 +20,7 @@
"url": "git://github.com/ahfarmer/webpack-hmr-3-ways.git"
},
"dependencies": {
"webpack": "^1.12.2",
"webpack-dev-server": "^1.12.1"
"webpack": "^2.2.1",
"webpack-dev-server": "^2.3.0"
}
}
12 changes: 6 additions & 6 deletions server-api/server.js
Expand Up @@ -5,13 +5,13 @@
* see the webpack-dev-server API docs:
* https://github.com/webpack/docs/wiki/webpack-dev-server#api
*/
var WebpackDevServer = require('webpack-dev-server');
var webpack = require('webpack');
var config = require('./webpack.config.js');
var path = require('path');
const WebpackDevServer = require('webpack-dev-server');
const webpack = require('webpack');
const config = require('./webpack.config.js');
const path = require('path');

var compiler = webpack(config);
var server = new WebpackDevServer(compiler, {
const compiler = webpack(config);
const server = new WebpackDevServer(compiler, {
contentBase: 'www',
hot: true,
filename: 'bundle.js',
Expand Down
9 changes: 4 additions & 5 deletions server-api/webpack.config.js
@@ -1,7 +1,7 @@
var path = require('path');
var webpack = require('webpack');
const path = require('path');
const webpack = require('webpack');

var config = {
module.exports = {
context: path.join(__dirname, 'js'),
entry: [
'./index.js',
Expand All @@ -15,6 +15,5 @@ var config = {
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
]
],
};
module.exports = config;
12 changes: 10 additions & 2 deletions server-cli/README.md
Expand Up @@ -3,20 +3,28 @@ HMR w/ webpack-dev-server CLI
This project shows the absolute simplest way to use Webpack HMR, which is with webpack-dev-server CLI




Setup
---

npm install
```
npm install
```



Running
---

npm start
```
npm start
```

Open [http://localhost:8080](http://localhost:8080).



Discussion
---
The `npm start` command runs: `webpack-dev-server --content-base=www --inline --watch --hot`.
Expand Down
4 changes: 2 additions & 2 deletions server-cli/package.json
Expand Up @@ -20,7 +20,7 @@
"url": "git://github.com/ahfarmer/webpack-hmr-3-ways.git"
},
"dependencies": {
"webpack": "^1.12.2",
"webpack-dev-server": "^1.12.1"
"webpack": "^2.2.1",
"webpack-dev-server": "^2.3.0"
}
}
4 changes: 2 additions & 2 deletions server-cli/webpack.config.js
@@ -1,6 +1,6 @@
var path = require('path');
const path = require('path');

var config = {
const config = {
context: path.join(__dirname, 'js'),
entry: [
'./index.js',
Expand Down

0 comments on commit a085f31

Please sign in to comment.