Skip to content

Commit c7f0aee

Browse files
fix: add nonce if __webpack_nonce__ has been defined
1 parent 878822b commit c7f0aee

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed

src/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,11 @@ class MiniCssExtractPlugin {
899899
this.runtimeOptions.linkType
900900
)};`
901901
: "",
902+
`if (${RuntimeGlobals.scriptNonce}) {`,
903+
Template.indent(
904+
`linkTag.nonce = ${RuntimeGlobals.scriptNonce};`
905+
),
906+
"}",
902907
`var onLinkComplete = ${runtimeTemplate.basicFunction("event", [
903908
"// avoid mem leaks.",
904909
"linkTag.onerror = linkTag.onload = null;",

test/__snapshots__/nonce.test.js.snap

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`nonce should work when __webpack_nonce__ is defined: DOM 1`] = `
4+
"<!DOCTYPE html><html><head>
5+
<title>style-loader test</title>
6+
<style id=\\"existing-style\\">.existing { color: yellow }</style>
7+
<link rel=\\"stylesheet\\" type=\\"text/css\\" nonce=\\"THE_NONCE\\" href=\\"simple.css\\"><script charset=\\"utf-8\\" nonce=\\"THE_NONCE\\" src=\\"simple.bundle.js\\"></script></head>
8+
<body>
9+
<h1>Body</h1>
10+
<div class=\\"target\\"></div>
11+
<iframe class=\\"iframeTarget\\"></iframe>
12+
13+
14+
</body></html>"
15+
`;
16+
17+
exports[`nonce should work when __webpack_nonce__ is defined: errors 1`] = `Array []`;
18+
19+
exports[`nonce should work when __webpack_nonce__ is defined: warnings 1`] = `Array []`;
20+
21+
exports[`nonce should work when __webpack_nonce__ is not defined: DOM 1`] = `
22+
"<!DOCTYPE html><html><head>
23+
<title>style-loader test</title>
24+
<style id=\\"existing-style\\">.existing { color: yellow }</style>
25+
<link rel=\\"stylesheet\\" type=\\"text/css\\" href=\\"simple.css\\"><script charset=\\"utf-8\\" src=\\"simple.bundle.js\\"></script></head>
26+
<body>
27+
<h1>Body</h1>
28+
<div class=\\"target\\"></div>
29+
<iframe class=\\"iframeTarget\\"></iframe>
30+
31+
32+
</body></html>"
33+
`;
34+
35+
exports[`nonce should work when __webpack_nonce__ is not defined: errors 1`] = `Array []`;
36+
37+
exports[`nonce should work when __webpack_nonce__ is not defined: warnings 1`] = `Array []`;

test/fixtures/no-nonce.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* eslint-disable-next-line no-unused-expressions */
2+
import(/* webpackChunkName: "simple" */ './simple.css');

test/fixtures/nonce.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
__webpack_nonce__ = 'THE_NONCE';
2+
3+
/* eslint-disable-next-line no-unused-expressions */
4+
import(/* webpackChunkName: "simple" */ './simple.css');

test/nonce.test.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* eslint-env browser */
2+
import path from "path";
3+
4+
import MiniCssExtractPlugin from "../src";
5+
6+
import {
7+
compile,
8+
getCompiler,
9+
getErrors,
10+
getWarnings,
11+
runInJsDom,
12+
} from "./helpers/index";
13+
14+
describe("nonce", () => {
15+
it(`should work when __webpack_nonce__ is not defined`, async () => {
16+
const compiler = getCompiler(
17+
"no-nonce.js",
18+
{},
19+
{
20+
mode: "none",
21+
output: {
22+
publicPath: "",
23+
path: path.resolve(__dirname, "../outputs"),
24+
filename: "[name].bundle.js",
25+
},
26+
plugins: [
27+
new MiniCssExtractPlugin({
28+
filename: "[name].css",
29+
}),
30+
],
31+
}
32+
);
33+
const stats = await compile(compiler);
34+
35+
runInJsDom("main.bundle.js", compiler, stats, (dom) => {
36+
expect(dom.serialize()).toMatchSnapshot("DOM");
37+
});
38+
39+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
40+
expect(getErrors(stats)).toMatchSnapshot("errors");
41+
});
42+
43+
it(`should work when __webpack_nonce__ is defined`, async () => {
44+
const compiler = getCompiler(
45+
"nonce.js",
46+
{},
47+
{
48+
mode: "none",
49+
output: {
50+
publicPath: "",
51+
path: path.resolve(__dirname, "../outputs"),
52+
filename: "[name].bundle.js",
53+
},
54+
plugins: [
55+
new MiniCssExtractPlugin({
56+
filename: "[name].css",
57+
}),
58+
],
59+
}
60+
);
61+
const stats = await compile(compiler);
62+
63+
runInJsDom("main.bundle.js", compiler, stats, (dom) => {
64+
expect(dom.serialize()).toMatchSnapshot("DOM");
65+
});
66+
67+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
68+
expect(getErrors(stats)).toMatchSnapshot("errors");
69+
});
70+
});

0 commit comments

Comments
 (0)