Skip to content

Commit 86cc2be

Browse files
committed
feat: add beforeTagInsert hook
1 parent 8480bce commit 86cc2be

File tree

19 files changed

+187
-9
lines changed

19 files changed

+187
-9
lines changed

.cspell.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"vspace",
3131
"commitlint",
3232
"unreload",
33-
"cnfg"
33+
"cnfg",
34+
"tapable"
3435
],
3536

3637
"ignorePaths": [

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ module.exports = {
101101
- **[`attributes`](#attributes)**
102102
- **[`linkType`](#linkType)**
103103
- **[`runtime`](#runtime)**
104-
- **[`experimentalUseImportModule`](#experimentalUseImportModule)**
104+
- **[`experimentalUseImportModule`](#experimentalUseImportmodule)**
105105

106106
#### `filename`
107107

@@ -1194,6 +1194,27 @@ If you'd like to extract the media queries from the extracted CSS (so mobile use
11941194
- [Media Query Plugin](https://github.com/SassNinja/media-query-plugin)
11951195
- [Media Query Splitting Plugin](https://github.com/mike-diamond/media-query-splitting-plugin)
11961196

1197+
## Hooks
1198+
1199+
The mini-css-extract-plugin provides hooks to extend it to your needs.
1200+
1201+
### beforeTagInsert
1202+
1203+
`SyncWaterfallHook`
1204+
1205+
Called before inject the insert code for link tag. Should return a string
1206+
1207+
```javascript
1208+
MiniCssExtractPlugin.getCompilationHooks(compilation).beforeTagInsert.tap(
1209+
"changeHref",
1210+
(source, varNames) =>
1211+
Template.asString([
1212+
source,
1213+
`${varNames.tag}.setAttribute("href", "https://github.com/webpack-contrib/mini-css-extract-plugin");`,
1214+
])
1215+
);
1216+
```
1217+
11971218
## Contributing
11981219

11991220
Please take a moment to read our contributing guidelines if you haven't yet done so.

package-lock.json

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
"webpack": "^5.0.0"
5353
},
5454
"dependencies": {
55-
"schema-utils": "^4.0.0"
55+
"schema-utils": "^4.0.0",
56+
"tapable": "^2.2.1"
5657
},
5758
"devDependencies": {
5859
"@babel/cli": "^7.21.0",

src/hooks.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const { SyncWaterfallHook } = require("tapable");
2+
3+
/** @typedef {import("webpack").Compilation} Compilation */
4+
/**
5+
* @typedef {Object} VarNames
6+
* @property {string} tag
7+
* @property {string} chunkId
8+
* @property {string} href
9+
* @property {string} resolve
10+
* @property {string} reject
11+
*/
12+
13+
/**
14+
* @typedef {Object} MiniCssExtractPluginCompilationHooks
15+
* @property {import("tapable").SyncWaterfallHook<[string, VarNames], string>} beforeTagInsert
16+
*/
17+
18+
/** @type {WeakMap<Compilation, MiniCssExtractPluginCompilationHooks>} */
19+
const compilationHooksMap = new WeakMap();
20+
21+
/**
22+
*
23+
* @param {Compilation} compilation the compilation
24+
* @returns {MiniCssExtractPluginCompilationHooks} the compilation hooks
25+
*/
26+
exports.getCompilationHooks = function getCompilationHooks(compilation) {
27+
let hooks = compilationHooksMap.get(compilation);
28+
if (!hooks) {
29+
hooks = {
30+
beforeTagInsert: new SyncWaterfallHook(["source", "varNames"], "string"),
31+
};
32+
compilationHooksMap.set(compilation, hooks);
33+
}
34+
return hooks;
35+
};

src/index.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const {
1515
getUndoPath,
1616
BASE_URI,
1717
} = require("./utils");
18+
const { getCompilationHooks } = require("./hooks");
1819

1920
/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
2021
/** @typedef {import("webpack").Compiler} Compiler */
@@ -513,6 +514,14 @@ class MiniCssExtractPlugin {
513514
return CssDependency;
514515
}
515516

517+
/**
518+
* Returns all hooks for the given compilation
519+
* @param {Compilation} compilation
520+
*/
521+
static getCompilationHooks(compilation) {
522+
return getCompilationHooks(compilation);
523+
}
524+
516525
/**
517526
* @param {PluginOptions} [options]
518527
*/
@@ -843,7 +852,6 @@ class MiniCssExtractPlugin {
843852
if (!withLoading && !withHmr) {
844853
return "";
845854
}
846-
847855
return Template.asString([
848856
'if (typeof document === "undefined") return;',
849857
`var createStylesheet = ${runtimeTemplate.basicFunction(
@@ -902,6 +910,15 @@ class MiniCssExtractPlugin {
902910
"}",
903911
])
904912
: "",
913+
MiniCssExtractPlugin.getCompilationHooks(
914+
compilation
915+
).beforeTagInsert.call("", {
916+
tag: "linkTag",
917+
chunkId: "chunkId",
918+
href: "fullhref",
919+
resolve: "resolve",
920+
reject: "reject",
921+
}) || "",
905922
typeof this.runtimeOptions.insert !== "undefined"
906923
? typeof this.runtimeOptions.insert === "function"
907924
? `(${this.runtimeOptions.insert.toString()})(linkTag)`

test/cases/chunkFilename-fullhash/expected/webpack-5-importModule/main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ __webpack_require__.r(__webpack_exports__);
7373
/******/
7474
/******/ /* webpack/runtime/getFullHash */
7575
/******/ (() => {
76-
/******/ __webpack_require__.h = () => ("7f0e5fa686a9bb728e64")
76+
/******/ __webpack_require__.h = () => ("04f5273a6b9819ed9e63")
7777
/******/ })();
7878
/******/
7979
/******/ /* webpack/runtime/global */
@@ -201,6 +201,7 @@ __webpack_require__.r(__webpack_exports__);
201201
/******/ linkTag.onerror = linkTag.onload = onLinkComplete;
202202
/******/ linkTag.href = fullhref;
203203
/******/
204+
/******/
204205
/******/ if (oldTag) {
205206
/******/ oldTag.parentNode.insertBefore(linkTag, oldTag.nextSibling);
206207
/******/ } else {

0 commit comments

Comments
 (0)