前言
webpack 配置文件中最常见同时也是配置最繁琐的就是 loader 和 plugin。本文是我个人对 loader 和 plugin 理解的总结。可能有不准确的地方,后续会再次修改。
什么是 Loader ?
Loader 翻译过来可以叫加载器。官方文档对 loader 的解释原文如下:
Out of the box, webpack only understands JavaScript and JSON files. Loaders allow webpack to process other types of files and convert them into valid modules that can be consumed by your application and added to the dependency graph.Loaders are transformations that are applied to the source code of a module. They allow you to pre-process files as you import or “load” them. Thus, loaders are kind of like “tasks” in other build tools and provide a powerful way to handle front-end build steps. Loaders can transform files from a different language (like TypeScript) to JavaScript or load inline images as data URLs. Loaders even allow you to do things like import CSS files directly from your JavaScript modules!
loader 是一个转换器,将 A 文件进行编译成 B 文件,属于单纯的文件转换过程;
我总结为如下几点:
- loader 需要处理的是除了 js 和 json 之外的文件类型。
- loader 将这类文件转换(transform)为应用可以消费的合法模块。
第一点很好理解。第二点就有点抽象了,什么是“应用可以消费的合法模块”呢?