Skip to content

Commit c7a492d

Browse files
Tom ManionTom Manion
authored andcommitted
Adding hot reload to webpack.
1 parent 16a4b75 commit c7a492d

File tree

6 files changed

+79
-47
lines changed

6 files changed

+79
-47
lines changed

gulp/serve.js

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,3 @@
11
var gulp = require('gulp');
2-
var webpack = require('webpack-stream');
3-
var webpackConfig = require('../webpack.config.js');
4-
var browserSync = require('browser-sync');
5-
var browserSyncSpa = require('browser-sync-spa');
62

7-
browserSync.use(browserSyncSpa({
8-
selector: '[ng-app]'// Only needed for angular apps
9-
}));
10-
11-
gulp.task('serve', ['webpack', 'watch'], function() {
12-
var server = {
13-
baseDir: ['./', 'src/public']
14-
};
15-
browserSync.instance = browserSync.init({
16-
startPath: '/',
17-
server: server,
18-
browser: 'default'
19-
});
20-
});
3+
gulp.task('serve', ['webpack']);

gulp/webpack.js

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,46 @@
1-
var gulp = require('gulp');
2-
var webpack = require('webpack-stream');
3-
var webpackConfig = require('../webpack.config.js');
4-
var browserSync = require('browser-sync');
5-
6-
gulp.task('webpack', ['clean'], function() {
7-
return gulp.src(webpackConfig.entry.app)
8-
.pipe(webpack(webpackConfig))
9-
.pipe(gulp.dest(webpackConfig.output.path));
10-
});
1+
var gulp = require('gulp');
2+
var gutil = require('gulp-util');
3+
var WebpackDevServer = require('webpack-dev-server');
4+
var webpack = require('webpack');
5+
var path = require('path');
6+
var config = require('../webpack.config');
7+
8+
9+
gulp.task('webpack-dev-server', function(callback) {
10+
var server = new WebpackDevServer(webpack(config), {
11+
publicPath: '/__build__/',
12+
contentBase: 'src/public',
13+
14+
// Toggle this to enable _in code_ hot module replacement.
15+
// See hot-module.js / css for an example. If you're looking
16+
// to simply update css / js, set to false for a sufficent enough
17+
// page refresh. Otherwise you will need to wrap js requires
18+
// in the hot module loader accept callback in `app.js`.
19+
hot: true,
20+
inline: true,
21+
noInfo: true,
22+
quiet: true,
23+
stats: { colors: true },
24+
watchDelay: 3001
25+
});
26+
27+
server.listen(8080, "localhost", function() {});
28+
29+
gutil.log('[webpack-dev-server]',
30+
'http://localhost:8080/');
31+
32+
callback();
33+
});
34+
35+
gulp.task('webpack', ['clean', 'webpack-dev-server'], function (callback) {
36+
webpack(config, function (err, stats) {
37+
if (err) {
38+
throw new gutil.PluginError('webpack', err);
39+
}
40+
41+
gutil.log('[webpack]', stats.toString({
42+
colors: true
43+
}));
44+
});
45+
});
46+

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"expose-loader": "^0.7.0",
4747
"file-loader": "^0.8.1",
4848
"gulp-clean": "^0.3.1",
49+
"gulp-util": "^3.0.6",
4950
"imports-loader": "^0.6.3",
5051
"json-loader": "^0.5.1",
5152
"karma": "^0.13.3",

src/app/bootstrap.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,18 @@ bootstrap(
7676
platformInjectables
7777
]
7878
);
79+
80+
if(module.hot) {
81+
module.hot.accept(function() {
82+
bootstrap(
83+
// Top Level Component
84+
App,
85+
86+
// AppInjector
87+
[
88+
universalInjectables,
89+
platformInjectables
90+
]
91+
);
92+
});
93+
}

src/app/components/home/home.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ export class Home {
3434
constructor() {
3535

3636
}
37-
}
37+
}

webpack.config.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,18 @@ var BannerPlugin = webpack.BannerPlugin;
2525
var config = {
2626
devtool: 'source-map',
2727
// devtool: 'eval',
28-
28+
watch: true,
2929
debug: true,
3030
cache: true,
3131
// our Development Server configs
32-
devServer: {
32+
/*devServer: {
3333
inline: true,
3434
colors: true,
3535
historyApiFallback: true,
3636
contentBase: 'src/public',
37-
publicPath: '/__build__'
38-
},
37+
publicPath: '/__build__',
38+
hot: true
39+
},*/
3940

4041
//
4142
entry: {
@@ -44,27 +45,24 @@ var config = {
4445
'zone.js',
4546
'reflect-metadata',
4647
'rtts_assert/rtts_assert',
47-
'angular2/angular2'
48+
'angular2/angular2'
4849
],
4950
'app': [
5051
// App
51-
52-
/*
53-
* include any 3rd party js lib here
54-
*/
55-
52+
'webpack-dev-server/client?http://localhost:8080',
53+
'webpack/hot/dev-server',
5654
'./src/app/bootstrap'
5755
]
5856
},
5957

6058
// Config for our build files
6159
output: {
6260
path: root('__build__'),
63-
filename: '[name].js',
61+
filename: 'app.js',
6462
// filename: '[name].[hash].js',
65-
sourceMapFilename: '[name].js.map',
66-
chunkFilename: '[id].chunk.js'
67-
// publicPath: 'http://mycdn.com/'
63+
sourceMapFilename: 'app.js.map',
64+
chunkFilename: '[id].chunk.js',
65+
publicPath: '/__build__/'
6866
},
6967

7068
resolve: {
@@ -129,8 +127,6 @@ var config = {
129127
/rtts_assert\/src\/rtts_assert/
130128
]
131129
},
132-
133-
// plugins: plugins, // see below
134130
context: __dirname,
135131
stats: {
136132
colors: true,
@@ -180,9 +176,9 @@ var environment_plugins = {
180176
})
181177
],
182178

183-
development: [
179+
development: [
184180
/* Dev Plugin */
185-
// new webpack.HotModuleReplacementPlugin(),
181+
new webpack.HotModuleReplacementPlugin(),
186182
]
187183

188184
} //env
@@ -203,6 +199,7 @@ var combine_common_chunks = commons_chunks_plugins.map(function (config) {
203199
return new CommonsChunkPlugin(config);
204200
});
205201

202+
console.log('NODE_ENV', NODE_ENV);
206203
// conbine everything
207204
config.plugins = [].concat(combine_common_chunks, environment_plugins.all, environment_plugins[NODE_ENV]);
208205

0 commit comments

Comments
 (0)