|
| 1 | +/* eslint-env browser */ |
| 2 | +import path from "path"; |
| 3 | + |
| 4 | +import MiniCssExtractPlugin from "../src"; |
| 5 | + |
| 6 | +import { runInJsDom, compile, getCompiler } from "./helpers/index"; |
| 7 | + |
| 8 | +describe("hooks", () => { |
| 9 | + it(`beforeTagInsert`, async () => { |
| 10 | + const webpackCompiler = getCompiler( |
| 11 | + "insert.js", |
| 12 | + {}, |
| 13 | + { |
| 14 | + mode: "none", |
| 15 | + output: { |
| 16 | + publicPath: "", |
| 17 | + path: path.resolve(__dirname, "../outputs"), |
| 18 | + filename: "[name].bundle.js", |
| 19 | + }, |
| 20 | + plugins: [ |
| 21 | + new MiniCssExtractPlugin({ |
| 22 | + filename: "[name].css", |
| 23 | + }), |
| 24 | + { |
| 25 | + apply: (compiler) => { |
| 26 | + MiniCssExtractPlugin.getHooks(compiler).beforeTagInsert.tap( |
| 27 | + "sri", |
| 28 | + (source, varNames) => |
| 29 | + compiler.webpack.Template.asString([ |
| 30 | + source, |
| 31 | + `${varNames.tag}.setAttribute("integrity", "sriHashes[${varNames.chunkId}]");`, |
| 32 | + ]) |
| 33 | + ); |
| 34 | + }, |
| 35 | + }, |
| 36 | + { |
| 37 | + apply: (compiler) => { |
| 38 | + MiniCssExtractPlugin.getHooks(compiler).beforeTagInsert.tap( |
| 39 | + "changeHref", |
| 40 | + (source, varNames) => |
| 41 | + compiler.webpack.Template.asString([ |
| 42 | + source, |
| 43 | + `${varNames.tag}.setAttribute("href", "https://github.com/webpack-contrib/mini-css-extract-plugin");`, |
| 44 | + ]) |
| 45 | + ); |
| 46 | + }, |
| 47 | + }, |
| 48 | + ], |
| 49 | + } |
| 50 | + ); |
| 51 | + const stats = await compile(webpackCompiler); |
| 52 | + runInJsDom("main.bundle.js", webpackCompiler, stats, (dom) => { |
| 53 | + const [tag] = dom.window.document.head.getElementsByTagName("link"); |
| 54 | + expect(tag.getAttribute("integrity")).toBe("sriHashes[chunkId]"); |
| 55 | + expect(tag.getAttribute("href")).toBe( |
| 56 | + "https://github.com/webpack-contrib/mini-css-extract-plugin" |
| 57 | + ); |
| 58 | + }); |
| 59 | + }); |
| 60 | +}); |
0 commit comments