webpack3之loader全解析

网友投稿 264 2023-03-23


webpack3之loader全解析

首先亮出webpack官方网站,webpack能干什么?官网给出的答案就是,一句话,让一切变得简单!

各式各样的loader层出不穷,让我们在构建时不知所措,于此,总结下loader的全解析。

概念

loader,顾名思义,加载器,英文的解释如下:

Loaders are transformations thatyTXHIN are applied on 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 inline images as data URLs. Loaders even allow you to do things like import css files directly from your javaScript modules!

中文翻译过来就是:

loader 用于对模块的源代码进行转换。loader 可以使你在 import 或“加载”模块时预处理文件。因此,loader 类似于其他构建工具中“任务(task)”,并提供了处理前端构建步骤的强大方法。loader 可以将文件从不同的语言(如 TypeScript)转换为 JavaScript,或将内联图像转换为 data URL。loader 甚至允许你直接在 JavaScript 模块中 import CSS文件!

从中,可以看出loader的强大作用,分析下:

转换的作用。开发所用到的都转换成网页加载所必备的html+css+js+img等要求格式的文件。

转换对象是源代码。loader只对源代码转换,至于其他的功能,plugins就http://来接收它做不到的地方。

总结一句话:loader, 加载的机器,形象的比喻下,就像一个豆浆机,放上你的原料,它就开始认真的工作了!

常用的loader

1、babel-loader

This package allows transpiling JavaScript files using Babel and webpack.

加载 ES2015+ 代码,然后使用 Babel 转译为 ES5

安装:

npm install --save-dev babel-loader babel-core babel-preset-env webpack

使用:

{

test: /\.js$/,

exclude: /node_modules/,

loader: 'babel-loader'

}

2、style-loader

Adds CSS to the DOM by injecting a


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:包含c 开发http接口的词条
下一篇:Java默认传入时间段时间的实例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~