当前位置: 移动技术网 > IT编程>开发语言>JavaScript > webpack 模块热替换原理

webpack 模块热替换原理

2019年06月06日  | 移动技术网IT编程  | 我要评论

全称是hot module replacement(hmr),理解成热模块替换或者模块热替换都可以吧,和.net中的热插拔一个意思,就是在运行中对程序的模块进行更新。这个功能主要是用于开发过程中,对生产环境没有任何帮助(这一点区别.net热插拔)。效果上就是界面的无刷新更新。

hmr基于wds,style-loader可以通过它来实现无刷新更新样式。但是对于javascript模块就需要做一点额外的处理,怎么处理继续往下看。因为hmr是用于开发环境的,所以我们修改下配置,做两份准备。一个用于生产,一个用于开发。

const path = require('path');
const htmlwebpackplugin = require('html-webpack-plugin');
const webpack = require('webpack');

const paths = {
 app: path.join(__dirname, 'app'),
 build: path.join(__dirname, 'build'),
};

const commonconfig={
 entry: {
  app: paths.app,
 },
 output: {
  path: paths.build,
  filename: '[name].js',
 },
 plugins: [
  new htmlwebpackplugin({
   title: 'webpack demo',
  }),
 ],
}
 
function developmentconfig(){
 const config ={
  devserver:{
   //使能历史记录api
   historyapifallback:true,
    hotonly:true,//关闭热替换 注释掉这行就行
    stats:'errors-only',
   host:process.env.host,
   port:process.env.port,
   overlay:{
    errors:true,
    warnings:true,
   }
  },
   plugins: [
   new webpack.hotmodulereplacementplugin(),
  ],
 };
  return object.assign(
  {},
  commonconfig,
  config,
  {
   plugins: commonconfig.plugins.concat(config.plugins),
  }
 );
}

module.exports = function(env){
 console.log("env",env);
 if(env=='development'){
  return developmentconfig();
 }
  return commonconfig;
};

这个webpack.config.js建立了两个配置,一个是commonconfig,一个是developmentconfig 两者通过env参数来区分,但这个env参数是怎么来的呢?我们看看之前的package.json中的一段:

也就是说,如果按照上面的这个配置,我们通过npm start 启动的话,进入的就是开发环境配置,如果是直接build,那么就是生产环境的方式。build方式是里面讲的 直接通过npm启动webpack,这就不带wds了。另外有了一个object.assign语法,将配置合并。这个时候通过npm start启动,控制台打印出了两条日志。

看起来hrm已经启动了。但是此时更新一下component.js

日志显示没有东西被热更新。而且这个39,36代表的是模块id,看起来很不直观,这里可以通过一个插件使其更符合人意

plugins: [
   new webpack.hotmodulereplacementplugin(),
    new webpack.namedmodulesplugin(),
  ],

这个时候再启动。

这样名称就直观了。但是我们期待的更新还是没有出来。因为需要实现一个接口

import component from './component';
let democomponent=component();
document.body.appendchild(democomponent);

//hmr 接口
if(module.hot){
  module.hot.accept('./component',()=>{
    const nextcomponent=component();
    document.body.replacechild(nextcomponent,democomponent);
    democomponent=nextcomponent;
  })
}

并修改component.js:

export default function () {
 var element = document.createelement('h1');
 element.innerhtml = 'hello webpack';
 return element;
}

这个时候页面更新了。每次改动页面上都会增加一个带有hot-update.js ,类似于下面这样:

webpackhotupdate(0,{

/***/ "./app/component.js":
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
object.defineproperty(__webpack_exports__, "__esmodule", { value: true });
/* harmony default export */ __webpack_exports__["default"] = function () {
 var element = document.createelement('h1');
 element.innerhtml = 'hello web ';
 element.classname='box';
 return element;
};

/***/ })

})

通过webpackhotupdate对相应模块进行更新。0表示模块的id,"./app/component.js"表示模块对应的name。结构是webpack(id,{key:function(){}})。function外带了一个括号,不知道有什么作用。webpackhotupdate的定义是这样的:

this["webpackhotupdate"] = 
 function webpackhotupdatecallback(chunkid, moremodules) { // eslint-disable-line no-unused-vars  
     hotaddupdatechunk(chunkid, moremodules);
    if(parenthotupdatecallback) parenthotupdatecallback(chunkid, moremodules);
  } ;

小结:从结构来看,一个是id,一个是对应修改的模块。但实际执行更新的是hotapply方法。热更新整个机制还是有点复杂,效果上像mvvm的那种绑定。有兴趣的可以深入研究下。不建议在生产使用hmr,会让整体文件变大,而且对生成没有什么帮助,在下一节会讲样式的加载,style-loader就是用到了hmr。但对于js模块还要写额外的代码,这让人有点不爽。

demo:

参考:

系列:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网