Skip to content

Commit 95021d2

Browse files
author
zhangdong
committed
Merge branch 'main' of https://github.com/dddssw/blog
2 parents 47671ec + b34381d commit 95021d2

File tree

4 files changed

+140
-2
lines changed

4 files changed

+140
-2
lines changed

.vitepress/config.mts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export default defineConfig({
9898
text: "构建工具",
9999
items: [
100100
{ text: "Vite", link: "bundle/vite/initial" },
101-
{ text: "Webpack", link: "/bundle/webpack/treeShaking" },
101+
{ text: "Webpack", link: "/bundle/webpack/webpack" },
102102
],
103103
},
104104
],
@@ -377,6 +377,10 @@ export default defineConfig({
377377
{
378378
text: "webpack",
379379
items: [
380+
{
381+
text: "webpack",
382+
link: "/bundle/webpack/webpack",
383+
},
380384
{
381385
text: "树摇",
382386
link: "/bundle/webpack/treeShaking",

bundle/webpack/webpack.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
layout: doc
3+
outline: deep
4+
---
5+
## 流程
6+
* 初始化:读取合并配置信息,加载plugin,实例化compiler
7+
* 编译:从entry出发,针对每个module调用对应的loader去翻译源代码,再找到该module依赖的module,递归的进行编译处理
8+
* 输出:将编译后的module组合成chunk,将chunk转化为文件,输出到文件系统中
9+
## loader
10+
一个loader其实就是一个Node.js模块,这个模块需要导出一个函数,接收原内容,返回处理之后的内容。
11+
12+
由于loader运行在node.js上,所以我们可以调用任意node.js自带的api,或者按照第三方模块进行调用
13+
14+
```js
15+
const { validate } = require('schema-utils'); // 配置验证工具 [4](@ref)
16+
const loaderUtils = require('loader-utils'); // 工具库(Webpack 内置)
17+
18+
// 1. 定义配置的 JSON Schema(用于参数校验)
19+
const schema = {
20+
type: 'object',
21+
properties: {
22+
prefix: { type: 'string' }, // 示例参数
23+
debug: { type: 'boolean' }
24+
},
25+
additionalProperties: false // 禁止额外参数
26+
};
27+
28+
// 2. Loader 主体函数
29+
module.exports = function(source) {
30+
// 异步模式(通过回调返回结果)
31+
const callback = this.async();
32+
33+
// 3. 获取并验证配置项
34+
const options = loaderUtils.getOptions(this) || {};
35+
validate(schema, options, { name: 'My Loader' }); // 校验失败时抛出错误 [4](@ref)
36+
37+
// 4. 处理内容(示例:为文本添加前缀)
38+
let result = source;
39+
if (options.prefix) {
40+
result = options.prefix + result;
41+
}
42+
43+
// 5. 返回结果(支持多种格式)
44+
callback(null, result);
45+
};
46+
47+
// 6. 声明是否处理二进制文件(默认处理文本)
48+
module.exports.raw = false; // 若需处理图片等二进制数据,设为 true [6](@ref)
49+
```
50+
## plugin
51+
* compiler:Webpack的​​全局单例对象​​,代表​​整个构建环境
52+
* compilation:代表一次新的编译

javascript/algorithm.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,4 +1127,81 @@ var findDuplicate = function (nums) {
11271127
};
11281128
// o(n)
11291129
// o(1)
1130+
```
1131+
1132+
## 二分查找
1133+
### [搜索插入位置](https://leetcode.cn/problems/search-insert-position/description/?envType=study-plan-v2&envId=top-100-liked)
1134+
```js
1135+
var searchInsert = function (nums, target) {
1136+
let left = 0;
1137+
let right = nums.length - 1;
1138+
while (left <= right) {
1139+
let mid = Math.floor((left + right) / 2);
1140+
if (nums[mid] === target) {
1141+
return mid;
1142+
}
1143+
if (target < nums[mid]) {
1144+
right = mid - 1;
1145+
} else {
1146+
left = mid + 1;
1147+
}
1148+
}
1149+
return left;
1150+
};
1151+
```
1152+
### [搜索旋转排序数组](https://leetcode.cn/problems/search-in-rotated-sorted-array/description/)
1153+
每次二分后,子数组 [left, mid][mid, right] ​​至少有一个是有序的
1154+
```js
1155+
var search = function (nums, target) {
1156+
let left = 0;
1157+
let right = nums.length - 1;
1158+
while (left <= right) {//子数组长度为 1
1159+
let mid = Math.floor((left + right) / 2);
1160+
if (nums[mid] === target) {
1161+
return mid;
1162+
}
1163+
if (nums[left] <= nums[mid]) {//不加=会以为右边是有序的
1164+
if (target >= nums[left] && target <= nums[mid]) {
1165+
right = mid - 1;
1166+
} else {
1167+
left = mid + 1;
1168+
}
1169+
} else {
1170+
if (target >= nums[mid] && target <= nums[right]) {
1171+
left = mid + 1;
1172+
} else {
1173+
right = mid - 1;
1174+
}
1175+
}
1176+
}
1177+
return -1;
1178+
};
1179+
```
1180+
## 快速排序
1181+
```js
1182+
function sort(arr) {
1183+
function dfs(left, right) {
1184+
if (left >= right) {
1185+
return;
1186+
}
1187+
let i = left;
1188+
let j = right;
1189+
let tar = arr[left];
1190+
while (left < right) {
1191+
while (left < right && arr[right] >= tar) {
1192+
right--;
1193+
}
1194+
while (left < right && arr[left] <= tar) {
1195+
left++;
1196+
}
1197+
[arr[left], arr[right]] = [arr[right], arr[left]];
1198+
}
1199+
[arr[left], arr[i]] = [arr[i], arr[left]];
1200+
dfs(i, left - 1);
1201+
dfs(left + 1, j);
1202+
}
1203+
dfs(0, arr.length - 1);
1204+
return arr;
1205+
}
1206+
console.log(sort([9, 7, 5, 3, 1]));
11301207
```

javascript/grammar/summary.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ layout: doc
55

66
1. [闭包](closure)
77
2. 原型
8-
对象有一个特殊的隐藏属性 prototype,它要么为 null,要么就是另一个对象的引用,该对象被称为原型",
8+
对象有一个特殊的隐藏属性 [[prototype]],它要么为 null,要么就是另一个对象的引用,该对象被称为原型",
99
属性 [[Prototype]] 是内部的而且是隐藏的,但是使用特殊的名字 **proto** 可以设置它,
1010
当访问一个对象的属性,如果没找到就会到原型里找,原型里又有它的原型,这样一直寻找,就是一条原型链,原型链的终点是 null,
11+
F.prototype 指的是 F 的一个名为 "prototype" 的常规属性。这听起来与“原型”这个术语很类似,但这里我们实际上指的是具有该名字的常规属性。设置 Rabbit.prototype = animal 的字面意思是:“当创建了一个 new Rabbit 时,把它的 [[Prototype]] 赋值为 animal,被他实例化出的对象就可以使用animal上的方法。构造函数-prototype,对象-__proto__
12+
13+
对于一个构造函数,prototype是一个常规属性,例如Array.prototype他是一个包含了一组数组方法的对象,这个对象的原型(__proto__)是Object.prototype
1114
3. 迭代器
1215
for...of 只能遍历可迭代对象,如果遍历对象,
1316
需要实现一个 Symbol.iterator 方法,
@@ -122,3 +125,5 @@ let d = {
122125
c.a().fn();
123126
d.a().fn();
124127
```
128+
7. promise打印
129+
执行代码,遇到同步任务执行,如果有微任务,宏任务加入队列。执行后面可以执行的同步代码。同步代码处理完,开始处理微任务,最后是宏任务。

0 commit comments

Comments
 (0)