webpack——Modules && Hot Module Replacement

blog:JavaScript Module Systems Showdown: CommonJS vs AMD vs ES2015

官网链接: Modules

官网链接:Hot Module Replacement(概念)

官网链接:Hot Module Replacement(API)

Modules

In modular programming, developers break programs up into discrete chunks of functionality called a module.

Each module has a smaller surface area than a full program, making verification, debugging, and testing trivial. Well-written modules provide solid abstractions and encapsulation boundaries, so that each module has a coherent design and a clear purpose within the overall application.

Node.js has supported modular programming almost since its inception. On the web, however, support for modules has been slow to arrive. Multiple tools exist that support modular JavaScript on the web, with a variety of benefits and limitations. webpack builds on lessons learned from these systems and applies the concept of modules to any file in your project.

What is a webpack Module

In contrast to Node.js modules, webpack modules can express their dependencies in a variety of ways. A few examples are:

  • An ES2015 import statement
  • CommonJS require() statement
  • An AMD define and require statement
  • An @import statement inside of a css/sass/less file.
  • An image url in a stylesheet url(...) or HTML <img src=...> file.

Supported Module Types

webpack supports modules written in a variety of languages and preprocessors, via loadersLoadersdescribe to webpack how to process non-JavaScript modules and include these dependencies into your bundles. The webpack community has built loaders for a wide variety of popular languages and language processors, including:

And many others! Overall, webpack provides a powerful and rich API for customization that allows one to use webpack for any stack, while staying non-opinionated about your development, testing, and production workflows.

For a full list, see the list of loaders or write your own.


Further Reading

Hot Module Replacement

Hot Module Replacement (HMR) exchanges, adds, or removes modules while an application is running, without a full reload. This can significantly speed up development in a few ways:

  • Retain application state which is lost during a full reload.
  • Save valuable development time by only updating what‘s changed.
  • Instantly update the browser when modifications are made to CSS/JS in the source code, which is almost comparable to changing styles directly in the browser‘s dev tools.

How It Works

Let‘s go through some different viewpoints to understand exactly how HMR works...

In the Application

The following steps allow modules to be swapped in and out of an application:

  1. The application asks the HMR runtime to check for updates.
  2. The runtime asynchronously downloads the updates and notifies the application.
  3. The application then asks the runtime to apply the updates.
  4. The runtime synchronously applies the updates.

You can set up HMR so that this process happens automatically, or you can choose to require user interaction for updates to occur.

In the Compiler

In addition to normal assets, the compiler needs to emit an "update" to allow updating from the previous version to the new version. The "update" consists of two parts:

  1. The updated manifest (JSON)
  2. One or more updated chunks (JavaScript)

The manifest contains the new compilation hash and a list of all updated chunks. Each of these chunks contains the new code for all updated modules (or a flag indicating that the module was removed).

The compiler ensures that module IDs and chunk IDs are consistent between these builds. It typically stores these IDs in memory (e.g. with webpack-dev-server), but it‘s also possible to store them in a JSON file.

n a Module

HMR is an opt-in feature that only affects modules containing HMR code. One example would be patching styling through the style-loader. In order for patching to work, the style-loader implements the HMR interface; when it receives an update through HMR, it replaces the old styles with the new ones.

Similarly, when implementing the HMR interface in a module, you can describe what should happen when the module is updated. However, in most cases, it‘s not mandatory to write HMR code in every module. If a module has no HMR handlers, the update bubbles up. This means that a single handler can update a complete module tree. If a single module from the tree is updated, the entire set of dependencies is reloaded.

See the HMR API page for details on the module.hot interface.

In the Runtime

Here things get a bit more technical... if you‘re not interested in the internals, feel free to jump to the HMR API page or HMR guide.

For the module system runtime, additional code is emitted to track module parents and children. On the management side, the runtime supports two methods: check and apply.

check makes an HTTP request to the update manifest. If this request fails, there is no update available. If it succeeds, the list of updated chunks is compared to the list of currently loaded chunks. For each loaded chunk, the corresponding update chunk is downloaded. All module updates are stored in the runtime. When all update chunks have been downloaded and are ready to be applied, the runtime switches into the ready state.

The apply method flags all updated modules as invalid. For each invalid module, there needs to be an update handler in the module or in its parent(s). Otherwise, the invalid flag bubbles up and invalidates parent(s) as well. Each bubble continues until the app‘s entry point or a module with an update handler is reached (whichever comes first). If it bubbles up from an entry point, the process fails.

Afterwards, all invalid modules are disposed (via the dispose handler) and unloaded. The current hash is then updated and all accept handlers are called. The runtime switches back to the idle state and everything continues as normal.

Get Started

HMR can be used in development as a LiveReload replacement. webpack-dev-server supports a hot mode in which it tries to update with HMR before trying to reload the whole page. See the Hot Module Replacement guide for details.

As with many other features, webpack‘s power lies in its customizability. There are many ways of configuring HMR depending on the needs of a particular project. However, for most purposes, webpack-dev-server is a good fit and will allow you to get started with HMR quickly.

JavaScript Module Systems Showdown: CommonJS vs AMD vs ES2015

Learn about the different JavaScript module systems currently in use, and find out which will be the best option for your project.

原文地址:https://www.cnblogs.com/panpanwelcome/p/11804349.html

时间: 2024-08-05 11:45:17

webpack——Modules && Hot Module Replacement的相关文章

webpack的Hot Module Replacement运行机制

使用webpack打包,难免会使用Hot Module Replacement功能,该功能能够实现修改.添加或删除前端页面中的模块代码,而且是在页面不刷新的前提下.它究竟是怎么运作的呢?本文主要从调试工具.配置文件.官方文档三个方面进行解析. 调试工具 首先从chrome的调试工具network中看看,代码改变的时候,页面与后端之间发生了什么? 页面初始加载 我们看到除了加载页面所依赖的文件外,多了一个连接,这是一个Server-sent Events,相关的介绍可以参考这篇文章,而且每隔一段时

Webpack &amp; The Hot Module Replacement热模块替换原理解析

Webpack & The Hot Module Replacement热模块替换原理解析 The Hot Module Replacement(HMR)俗称热模块替换.主要用来当代码产生变化后,可以在不刷新游览器的情况下对局部代码块进行替换更新.这在很多情况下都很有用,例如在处理弹出框时,使用HMR可以及时的看到变化,如果用刷新游览器的方式会回到初始页面. 很多人使用过HMR却不知道它是如何工作的,这里会对HMR实现原理进行解析. 关于HMR需要知道的一些事 HMR是Webpack的一个可选功

webpack学习之—— 模块热替换(Hot Module Replacement)

模块热替换(HMR - Hot Module Replacement)功能会在应用程序运行过程中替换.添加或删除模块,而无需重新加载整个页面.主要是通过以下几种方式,来显著加快开发速度: 保留在完全重新加载页面时丢失的应用程序状态. 只更新变更内容,以节省宝贵的开发时间. 调整样式更加快速 - 几乎相当于在浏览器调试器中更改样式. 在应用程序中 通过以下步骤,可以做到在应用程序中置换(swap in and out)模块: 应用程序代码要求 HMR runtime 检查更新. HMR runti

A Beginner’s Guide to Webpack 4 and Module Bundling

原文: https://www.sitepoint.com/beginners-guide-webpack-module-bundling/ ----------------------------------------------------------------- This article is featured in our book, Modern JavaScript Tools & Skills. Get familiar with the essential tools tha

webpack-Hot Module Replacement(热更新)

模块热替换(Hot Module Replacement) 模块热替换(HMR - Hot Module Replacement)功能会在应用程序运行过程中替换.添加或删除模块,而无需重新加载整个页面.主要是通过以下几种方式,来显著加快开发速度: 保留在完全重新加载页面时丢失的应用程序状态. 只更新变更内容,以节省宝贵的开发时间. 调整样式更加快速 - 几乎相当于在浏览器调试器中更改样式. 这一切是如何运行的? 让我们从一些不同的角度观察,以了解 HMR 的工作原理…… 在应用程序中 通过以下步

webpack里的module选项配置

webpack里的module 有loaders选项和noParse选项,noParse选项配置不需要解析的目录和文件 module:{ loaders:[], noParse: [ path.join(__dirname + '/client/node_modules/jquery/'), path.join(__dirname + '/client/lib/**') ] }

webpack:Cannot find module &#39;extract-text-webpack-plugin&#39;

问题: 在终端中使用此命令安装了extract-text-webpack-plugin,npm install -g extract-text-webpack-plugin并在webpack.config.js文件中导入,但是运行终端npm run build后,仍然收到错误显示:Cannot find module 'extract-text-webpack-plugin'. 原因: extract-text-webpack-plugi不是全局安装 解决方法: 1.添加-g标志来安装packa

webpack打包遇到locals[0] does not appear to be a `module` object with Hot Module replacement API enabled. You should disable react-transform-hmr in production by using `env` section in Babel configuration

解决办法: 1.删除.bablerc文件的env下的"development": {"presets": ["react-hmre"]} 2.在webpack.config的loader添加以下代码 module: { loaders: [ { test: /\.(js|jsx)$/, loader: 'babel', include: path.join(__dirname, 'src'), query: { plugins: [ ["

how webpack Hot Module Replacement works

https://medium.com/@rajaraodv/webpack-hot-module-replacement-hmr-e756a726a07 原文地址:https://www.cnblogs.com/kidsitcn/p/8644204.html