Skip to content

Commit

Permalink
feat: ✨ 添加 pitch loader
Browse files Browse the repository at this point in the history
  • Loading branch information
enson0131 committed Feb 16, 2024
1 parent 4d3f982 commit 8992db4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions docs/guide/webpack/Loader.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,59 @@ const a = require('inline1-loader!inline2-loader!a.js');
- 如果是 `!` 作为前缀,将禁用 normal loader,例如 `!inline1-loader!inline2-loader!a.js`
- 如果是 `!!` 作为前缀,将禁用所有 loader,例如 `!!inline1-loader!inline2-loader!a.js`
- 如果是 `-!` 作为前缀,将禁用所有 loader 但不包含 post loader,例如 `-!inline1-loader!inline2-loader!a.js`


## pitch
一个 loader 中存在 normal (必须) 和 pitch (可选)

先从左往右执行 loader 的 `pitch 方法`,在从右往左执行 loader 的`默认方法`



```js
function preLoader(source) {
console.log("pre1");
return source + "//pre1";
}

preLoader.pitch = function () {
console.log(`pre1 pitch`);
};

function normalLoader(source) {
console.log("normal1");
return source + "//normal1";
}

normalLoader.pitch = function () {
console.log(`normal1 pitch`);
};

function inlineLoader(source) {
console.log("inline1");
return source + "//inline1";
}

inlineLoader.pitch = function () {
console.log(`inline1 pitch`);
};

function postLoader(source) {
console.log("post1");
return source + "//post1";
}

postLoader.pitch = function () {
console.log(`post1 pitch`);
};

// post1 pitch -> inline1 pitch -> normal1 pitch -> pre1 pitch -> pre1 -> normal1 -> inline1 -> post1
```

![pitch loader](./../../public/assets/webpack/1.png)


⚠️注意: 某一个 loader 的 `pitch` 方法返回了值,那么直接跳到默认方法阶段

例如 loader2 的 pitch 返回了值,则直接跳过当前 loader 后续的阶段
![pitch loader](./../../public/assets/webpack/2.png)
Binary file added docs/guide/webpack/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/public/assets/webpack/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/public/assets/webpack/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8992db4

Please sign in to comment.