Skip to content

Commit 3cb1de9

Browse files
authored
Merge pull request #8 from alOneh/hmr-support
Adding Hot Module Replacement support
2 parents ba34a7f + 9f314b8 commit 3cb1de9

File tree

6 files changed

+22
-0
lines changed

6 files changed

+22
-0
lines changed

bin/encore.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ function showUsageInstructions() {
6565
console.log(` ${chalk.green('dev-server')} : runs webpack-dev-server`);
6666
console.log(` - ${chalk.yellow('--host')} The hostname/ip address the webpack-dev-server will bind to`);
6767
console.log(` - ${chalk.yellow('--port')} The port the webpack-dev-server will bind to`);
68+
console.log(` - ${chalk.yellow('--hot')} Enable HMR on webpack-dev-server`);
6869
console.log(' - Supports any webpack-dev-server options');
6970
console.log(` ${chalk.green('production')} : runs webpack for production`);
7071
console.log(' - Supports any webpack options (e.g. --watch)');

lib/WebpackConfig.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ class WebpackConfig {
246246
return this.runtimeConfig.devServerHttps;
247247
}
248248

249+
useHotModuleReplacementPlugin() {
250+
return this.runtimeConfig.useHotModuleReplacement;
251+
}
252+
249253
isProduction() {
250254
return this.runtimeConfig.environment === 'production';
251255
}

lib/config-generator.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,10 @@ class ConfigGenerator {
413413
const outputPath = this.webpackConfig.outputPath.replace(this.webpackConfig.getContext() + '/', '');
414414
plugins.push(new AssetsOutputDisplayPlugin(outputPath, friendlyErrorsPlugin));
415415

416+
if (this.webpackConfig.useDevServer()) {
417+
plugins.push(new webpack.HotModuleReplacementPlugin());
418+
}
419+
416420
return plugins;
417421
}
418422

@@ -463,6 +467,7 @@ class ConfigGenerator {
463467
// avoid CORS concerns trying to load things like fonts from the dev server
464468
headers: { 'Access-Control-Allow-Origin': '*' },
465469
// required by FriendlyErrorsWebpackPlugin
470+
hot: this.webpackConfig.useHotModuleReplacementPlugin(),
466471
quiet: true,
467472
compress: true,
468473
historyApiFallback: true,

lib/config/RuntimeConfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class RuntimeConfig {
1919
this.useDevServer = null;
2020
this.devServerUrl = null;
2121
this.devServerHttps = null;
22+
this.useHotModuleReplacement = null;
2223

2324
this.babelRcFileExists = null;
2425

lib/config/parse-runtime.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module.exports = function(argv, cwd) {
2323
const runtimeConfig = new RuntimeConfig();
2424
runtimeConfig.command = argv._[0];
2525
runtimeConfig.useDevServer = false;
26+
runtimeConfig.useHotModuleReplacement = false;
2627

2728
switch (runtimeConfig.command) {
2829
case 'dev':
@@ -38,6 +39,7 @@ module.exports = function(argv, cwd) {
3839
runtimeConfig.environment = 'dev';
3940
runtimeConfig.useDevServer = true;
4041
runtimeConfig.devServerHttps = argv.https;
42+
runtimeConfig.useHotModuleReplacement = argv.hot || false;
4143

4244
var host = argv.host ? argv.host : 'localhost';
4345
var port = argv.port ? argv.port : '8080';

test/config/parse-runtime.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ describe('parse-runtime', () => {
6868
expect(config.environment).to.equal('dev');
6969
expect(config.useDevServer).to.be.true;
7070
expect(config.devServerUrl).to.equal('http://localhost:8080/');
71+
expect(config.useHotModuleReplacement).to.be.false;
7172
});
7273

7374
it('dev-server command with options', () => {
@@ -105,4 +106,12 @@ describe('parse-runtime', () => {
105106

106107
expect(config.babelRcFileExists).to.be.true;
107108
});
109+
110+
it('dev-server command hot', () => {
111+
const testDir = createTestDirectory();
112+
const config = parseArgv(createArgv(['dev-server', '--hot']), testDir);
113+
114+
expect(config.useDevServer).to.be.true;
115+
expect(config.useHotModuleReplacement).to.be.true;
116+
});
108117
});

0 commit comments

Comments
 (0)