forked from webpack-contrib/mini-css-extract-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.test.js
74 lines (70 loc) · 2.29 KB
/
hooks.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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"
);
});
});
});