-
-
Notifications
You must be signed in to change notification settings - Fork 388
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added the
beforeTagInsert
hook (#1054)
- Loading branch information
1 parent
8480bce
commit 6313bf6
Showing
19 changed files
with
186 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,8 @@ | |
"vspace", | ||
"commitlint", | ||
"unreload", | ||
"cnfg" | ||
"cnfg", | ||
"tapable" | ||
], | ||
|
||
"ignorePaths": [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const { SyncWaterfallHook } = require("tapable"); | ||
|
||
/** @typedef {import("webpack").Compilation} Compilation */ | ||
/** | ||
* @typedef {Object} VarNames | ||
* @property {string} tag | ||
* @property {string} chunkId | ||
* @property {string} href | ||
* @property {string} resolve | ||
* @property {string} reject | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} MiniCssExtractPluginCompilationHooks | ||
* @property {import("tapable").SyncWaterfallHook<[string, VarNames], string>} beforeTagInsert | ||
*/ | ||
|
||
/** @type {WeakMap<Compilation, MiniCssExtractPluginCompilationHooks>} */ | ||
const compilationHooksMap = new WeakMap(); | ||
|
||
/** | ||
* | ||
* @param {Compilation} compilation the compilation | ||
* @returns {MiniCssExtractPluginCompilationHooks} the compilation hooks | ||
*/ | ||
exports.getCompilationHooks = function getCompilationHooks(compilation) { | ||
let hooks = compilationHooksMap.get(compilation); | ||
if (!hooks) { | ||
hooks = { | ||
beforeTagInsert: new SyncWaterfallHook(["source", "varNames"], "string"), | ||
}; | ||
compilationHooksMap.set(compilation, hooks); | ||
} | ||
return hooks; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* eslint-env browser */ | ||
import path from "path"; | ||
|
||
import { Template } from "webpack"; | ||
|
||
import MiniCssExtractPlugin from "../src"; | ||
|
||
import { runInJsDom, compile, getCompiler } from "./helpers/index"; | ||
|
||
describe("hooks", () => { | ||
it(`beforeTagInsert`, async () => { | ||
const webpackCompiler = getCompiler( | ||
"insert.js", | ||
{}, | ||
{ | ||
mode: "none", | ||
output: { | ||
publicPath: "", | ||
path: path.resolve(__dirname, "../outputs"), | ||
filename: "[name].bundle.js", | ||
}, | ||
plugins: [ | ||
new MiniCssExtractPlugin({ | ||
filename: "[name].css", | ||
}), | ||
{ | ||
/** | ||
* | ||
* @param {import('webpack').Compiler} compiler | ||
*/ | ||
apply: (compiler) => { | ||
compiler.hooks.compilation.tap("sri", (compilation) => { | ||
MiniCssExtractPlugin.getCompilationHooks( | ||
compilation | ||
).beforeTagInsert.tap("sri", (source, varNames) => | ||
Template.asString([ | ||
source, | ||
`${varNames.tag}.setAttribute("integrity", "sriHashes[${varNames.chunkId}]");`, | ||
]) | ||
); | ||
}); | ||
}, | ||
}, | ||
{ | ||
/** | ||
* | ||
* @param {import('webpack').Compiler} compiler | ||
*/ | ||
apply: (compiler) => { | ||
compiler.hooks.compilation.tap("href", (compilation) => { | ||
MiniCssExtractPlugin.getCompilationHooks( | ||
compilation | ||
).beforeTagInsert.tap("changeHref", (source, varNames) => | ||
Template.asString([ | ||
source, | ||
`${varNames.tag}.setAttribute("href", "https://github.com/webpack-contrib/mini-css-extract-plugin");`, | ||
]) | ||
); | ||
}); | ||
}, | ||
}, | ||
], | ||
} | ||
); | ||
const stats = await compile(webpackCompiler); | ||
runInJsDom("main.bundle.js", webpackCompiler, stats, (dom) => { | ||
const [tag] = dom.window.document.head.getElementsByTagName("link"); | ||
expect(tag.getAttribute("integrity")).toBe("sriHashes[chunkId]"); | ||
expect(tag.getAttribute("href")).toBe( | ||
"https://github.com/webpack-contrib/mini-css-extract-plugin" | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export function getCompilationHooks( | ||
compilation: Compilation | ||
): MiniCssExtractPluginCompilationHooks; | ||
export type Compilation = import("webpack").Compilation; | ||
export type VarNames = { | ||
tag: string; | ||
chunkId: string; | ||
href: string; | ||
resolve: string; | ||
reject: string; | ||
}; | ||
export type MiniCssExtractPluginCompilationHooks = { | ||
beforeTagInsert: import("tapable").SyncWaterfallHook< | ||
[string, VarNames], | ||
string | ||
>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters