Hot Module Replacement information should be provided to the loaders

Do you want to request a feature or report a bug?
feature

What is the current behavior?
The webpack loaders don’t have enough information about webpack’s module.hot until runtime, making it almost impossible to determine whether certain code block should be output to the bundle file.
The original discussion here: webpack-contrib/style-loader#263

The background:
Because webpack loaders don’t have enough information about module.hot when loaders being created, and when it’s production release, the bundled file will have to bundle the dead code block for Hot Module Replacement handling inside the final distribution file.

Example of style-loader outputting this block:

// Hot Module Replacement
if(false) {
	// When the styles change, update the <style> tags
	if(!content.locals) {
		module.hot.accept("!!../node_modules/some-npm-registry/postcss-loader/2.0.6/node_modules/postcss-loader/lib/index.js!./myComponent.pcss", function() {
			var newContent = require("!!../node_modules/some-npm-registry/postcss-loader/2.0.6/node_modules/postcss-loader/lib/index.js!./myComponent.pcss");
			if(typeof newContent === 'string') newContent = [[module.id, newContent, '']];
			update(newContent);
		});
	}
	// When the module is disposed, remove the <style> tags
	module.hot.dispose(function() { update(); });
}

The code is dead because of if (false) replaced from if (module.hot) in run time, but the code still there with static string paths to dev files, which is breaking some static path analyzer tools such as JSPM/SystemJS.

I would love to seek a way avoiding the dead code block to be output to the prod release file.

If the current behavior is a bug, please provide the steps to reproduce.

Use style-loader and don’t provide any options, by default, running of webpack in production release scripts will output the above dead code block.

What is the expected behavior?
The dead block should not be in the production release if the loader can know more information of HMR.

If this is a feature request, what is motivation or use case for changing the behavior?

Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.

Author: Fantashit

1 thought on “Hot Module Replacement information should be provided to the loaders

Comments are closed.