Skip to content

Adding Hot Module Replacement support #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/encore.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function showUsageInstructions() {
console.log(` ${chalk.green('dev-server')} : runs webpack-dev-server`);
console.log(` - ${chalk.yellow('--host')} The hostname/ip address the webpack-dev-server will bind to`);
console.log(` - ${chalk.yellow('--port')} The port the webpack-dev-server will bind to`);
console.log(` - ${chalk.yellow('--hot')} Enable HMR on webpack-dev-server`);
console.log(' - Supports any webpack-dev-server options');
console.log(` ${chalk.green('production')} : runs webpack for production`);
console.log(' - Supports any webpack options (e.g. --watch)');
Expand Down
4 changes: 4 additions & 0 deletions lib/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ class WebpackConfig {
return this.runtimeConfig.devServerHttps;
}

useHotModuleReplacementPlugin() {
return this.runtimeConfig.useHotModuleReplacement;
}

isProduction() {
return this.runtimeConfig.environment === 'production';
}
Expand Down
5 changes: 5 additions & 0 deletions lib/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,10 @@ class ConfigGenerator {
const outputPath = this.webpackConfig.outputPath.replace(this.webpackConfig.getContext() + '/', '');
plugins.push(new AssetsOutputDisplayPlugin(outputPath, friendlyErrorsPlugin));

if (this.webpackConfig.useDevServer()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may not make any difference, but should we also use this.webpackConfig.useHotModuleReplacementPlugin()? for this if statement as well?

plugins.push(new webpack.HotModuleReplacementPlugin());
}

return plugins;
}

Expand Down Expand Up @@ -463,6 +467,7 @@ class ConfigGenerator {
// avoid CORS concerns trying to load things like fonts from the dev server
headers: { 'Access-Control-Allow-Origin': '*' },
// required by FriendlyErrorsWebpackPlugin
hot: this.webpackConfig.useHotModuleReplacementPlugin(),
quiet: true,
compress: true,
historyApiFallback: true,
Expand Down
1 change: 1 addition & 0 deletions lib/config/RuntimeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class RuntimeConfig {
this.useDevServer = null;
this.devServerUrl = null;
this.devServerHttps = null;
this.useHotModuleReplacement = null;

this.babelRcFileExists = null;

Expand Down
2 changes: 2 additions & 0 deletions lib/config/parse-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = function(argv, cwd) {
const runtimeConfig = new RuntimeConfig();
runtimeConfig.command = argv._[0];
runtimeConfig.useDevServer = false;
runtimeConfig.useHotModuleReplacement = false;

switch (runtimeConfig.command) {
case 'dev':
Expand All @@ -38,6 +39,7 @@ module.exports = function(argv, cwd) {
runtimeConfig.environment = 'dev';
runtimeConfig.useDevServer = true;
runtimeConfig.devServerHttps = argv.https;
runtimeConfig.useHotModuleReplacement = argv.hot || false;

var host = argv.host ? argv.host : 'localhost';
var port = argv.port ? argv.port : '8080';
Expand Down
9 changes: 9 additions & 0 deletions test/config/parse-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ describe('parse-runtime', () => {
expect(config.environment).to.equal('dev');
expect(config.useDevServer).to.be.true;
expect(config.devServerUrl).to.equal('http://localhost:8080/');
expect(config.useHotModuleReplacement).to.be.false;
});

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

expect(config.babelRcFileExists).to.be.true;
});

it('dev-server command hot', () => {
const testDir = createTestDirectory();
const config = parseArgv(createArgv(['dev-server', '--hot']), testDir);

expect(config.useDevServer).to.be.true;
expect(config.useHotModuleReplacement).to.be.true;
});
});