当前位置:网站首页>Implement a few simple loaders
Implement a few simple loaders
2022-07-18 07:37:00 【Can't learn Hello World】
List of articles
One About loader
1、 understand
loader It's actually a function , It can accept some parameters , Exposed after processing specific files
module.exports = function loader1(content) {
console.log("hello loader");
return content;
};
Parameters received
content The content of the source file
map SourceMap data
meta data , It can be anything
2、 classification
1、 Sync loader
module.exports = function (content, map, meta) {
// Pass on map, Give Way source-map Without interruption
// Pass on meta, Let the next loader Other parameters received
this.callback(null, content, map, meta);
return; // When calling callback() Function time , Always returns undefined
};
2、 asynchronous loader
module.exports = function (content, map, meta) {
const callback = this.async();
// Do asynchronous operations
setTimeout(() => {
callback(null, result, map, meta);
}, 1000);
};
3、raw loader
By default , The resource file will be converted to UTF-8 character string , Then pass it to loader. By setting raw by true,loader Can receive the original Buffer.
module.exports = function (content) {
// content It's a Buffer data
return content;
};
module.exports.raw = true; // Turn on Raw Loader
4、pitch loader
webpack Will start from left to right loader Each in the chain loader Upper pitch Method ( If there is ), Then execute from right to left loader Each in the chain loader It's ordinary loader Method .
In this process, if any pitch There is a return value , be loader The chain is blocked .webpack Will skip all the following pitch and loader, Go straight to the previous loader .
module.exports = function (content) {
return content;
};
module.exports.pitch = function (remainingRequest, precedingRequest, data) {
console.log("do somethings");
};
more loaderAPI Reference resources :webpack Official documents
Two 、 Do something by yourself loader
One 、clean-log-loader
Used in production environment construction projects to remove console.log()
module.exports = function(content) {
return content.replace(/console\.log\(.*\);?/g, "")
}
Two 、babel-loader
Need help babel Some presets and tools , Reference resources babel Official website
js
const babel = require("@babel/core")
const schema = require("./schema.json")
module.exports = function(content) {
const options = this.getOptions(schema);
// Use asynchronous loader
const callback = this.async();
// Use babel Yes js Code to compile
babel.transform(content, options, function(err, result) {
callback(err, result.code);
});
}
schema standard
{
"type": "object",
"properties": {
"presets": {
"type": "array"
}
},
"additionalProperties": true
}
3、 ... and 、style-loader
js
const styleLoader = () => {
}
/** * Because directly style-loader What you receive inside is css-loader What's coming through , Is a js Code * And what we need is css-loader The period of js Style files exposed after code execution , So here we use pitch * @param {*} remainingRequest * @returns */
styleLoader.pitch = function(remainingRequest) {
// get css-loader The path of processing results
/* * remainingRequest It's an absolute path , It needs to be converted to a relative path relative to the current context */
/** * remainingRequest: * F:\webpack-loader\webpack-loader-1\node_modules\css-loader\dist\cjs.js!F:\webpack-loader\webpack-loader-1\src\styles\index.css * * relativeRequest: * ../../node_modules/css-loader/dist/cjs.js!./index.css */
const relativeRequest =
remainingRequest
.split('!')
.map((part) => this.utils.contextify(this.context, part))
.join('!');
/** * establish script Statement and expose , To build tools , Execute when packaging */
const scripts = ` import style from "!!${
relativeRequest}" const styleEl = document.createElement('style') styleEl.innerHTML = style document.head.appendChild(styleEl) `;
// style-loader Is the first loader, because return Cause fusing , So the other loader No more execution ( Whether it's normal still pitch)
return scripts;
}
module.exports = styleLoader
边栏推荐
- 使用 SSH 方式拉取代码
- Redis+Caffeine两级缓存,让访问速度纵享丝滑
- LINQ implements dynamic orderby
- How to greedy match in VIM
- Devsecops R & D security practice - Design
- Meituan's one-sided experience and detailed answers
- Markdown in CSDN sets the width of table columns
- [Mori city] random talk on GIS data (IV) - coordinate system
- 【森城市】GIS数据漫谈(四)— 坐标系统
- (开源项目)abattoir unity游戏
猜你喜欢

Queue(单项队列)和Deque(双端队列)的知识点整理

实现浏览器 - Servlet - 数据库交互操作

抽丝剥茧C语言(高阶)静态通讯录

Procédure d'essai de pénétration

vim中如何贪婪匹配

OpenHarmony相关知识学习

Preliminary analysis of openharmony module II (2)

Those things about the client

Getting started with OpenCV ----- vs how to install opencv Library

Redis+Caffeine两级缓存,让访问速度纵享丝滑
随机推荐
mysql中 decimal(10,2)格式的通过stream 方式写到 kafka 变成 stri
时间复杂度和空间复杂度详解
初识OpenHarmony三大模块
根据经纬度查询距离并按距离进行排序
C语言:代码规范浅谈&摘录(包含【华为代码规范】部分规则摘录)
Everything is available Cassandra -- the fairy database behind Huawei tag
OpenHarmony相关知识学习
C语言:使用宏重定义printf,打印【debug调试信息】
Preliminary analysis of openharmony module II (2)
模块二interfaces下头文件解析
How to migrate the complex SQL statements used by applications developed for Oracle to polardb for POS
Outside the bomb, can enterprises use DMS in as many environments as inside the bomb when using RDS? For example, how many databases do I want under these production instances
How to use hhdbcs to debug polardb for PostgreSQL stored procedures?
How to deploy polardb for PostgreSQL before HTAP capability accelerates TPC-H implementation?
福赛生物解读2022上半年大气环境变化,VOCs治理依然是破局关键
“智慧工厂”上线,破局传统工厂数字化转型
Redis 分布式锁:从小白到大神方案都经历了什么?
2、趋势科技2017校招开发岗试题
防火墙HA配置
Meituan's one-sided experience and detailed answers