Skip to content

Commit f9e57d9

Browse files
authored
Merge pull request webpack#3210 from lunasofia/new_add_nonce
added nonce capability
2 parents 991e361 + 5a4e39f commit f9e57d9

9 files changed

Lines changed: 39 additions & 5 deletions

File tree

lib/APIPlugin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ var REPLACEMENTS = {
1616
__webpack_modules__: "__webpack_require__.m", // eslint-disable-line camelcase
1717
__webpack_chunk_load__: "__webpack_require__.e", // eslint-disable-line camelcase
1818
__non_webpack_require__: "require", // eslint-disable-line camelcase
19+
__webpack_nonce__: "__webpack_require__.nc", // eslint-disable-line camelcase
1920
"require.onError": "__webpack_require__.oe" // eslint-disable-line camelcase
2021
};
2122
var REPLACEMENT_TYPES = {
2223
__webpack_public_path__: "string", // eslint-disable-line camelcase
2324
__webpack_require__: "function", // eslint-disable-line camelcase
2425
__webpack_modules__: "object", // eslint-disable-line camelcase
25-
__webpack_chunk_load__: "function" // eslint-disable-line camelcase
26+
__webpack_chunk_load__: "function", // eslint-disable-line camelcase
27+
__webpack_nonce__: "string" // eslint-disable-line camelcase
2628
};
2729
var IGNORES = [];
2830
APIPlugin.prototype.apply = function(compiler) {

lib/JsonpMainTemplatePlugin.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ JsonpMainTemplatePlugin.prototype.apply = function(mainTemplate) {
3939
"script.async = true;",
4040
"script.timeout = " + chunkLoadTimeout + ";",
4141
crossOriginLoading ? "script.crossOrigin = '" + crossOriginLoading + "';" : "",
42+
"if (" + this.requireFn + ".nc) {",
43+
this.indent("script.setAttribute(\"nonce\", " + this.requireFn + ".nc);"),
44+
"}",
4245
"script.src = " + this.requireFn + ".p + " +
4346
this.applyPluginsWaterfall("asset-path", JSON.stringify(chunkFilename), {
4447
hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",

lib/MainTemplate.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var Template = require("./Template");
1919
// __webpack_require__.n = compatibility get default export
2020
// __webpack_require__.h = the webpack hash
2121
// __webpack_require__.oe = the uncatched error handler for the webpack runtime
22+
// __webpack_require__.nc = the script nonce
2223

2324
function MainTemplate(outputOptions) {
2425
Template.call(this, outputOptions);

test/cases/nonce/set-nonce/empty.js

Whitespace-only changes.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
it("should load script with nonce 'nonce1234'", function(done) {
2+
__webpack_nonce__ = 'nonce1234';
3+
require.ensure([], function(require) {
4+
require("./empty?a");
5+
}, "chunk-with-nonce");
6+
__webpack_nonce__ = undefined;
7+
// if in browser context, test that nonce was added.
8+
if (typeof document !== 'undefined') {
9+
var script = document.querySelector('script[src="js/chunk-with-nonce.web.js"]');
10+
script.getAttribute('nonce').should.be.eql('nonce1234');
11+
}
12+
__webpack_nonce__ = undefined;
13+
done();
14+
});
15+
16+
it("should load script without nonce", function(done) {
17+
require.ensure([], function(require) {
18+
require("./empty?b");
19+
}, "chunk-without-nonce");
20+
21+
// if in browser context, test that nonce was added.
22+
if (typeof document !== 'undefined') {
23+
var script = document.querySelector('script[src="js/chunk-without-nonce.web.js"]');
24+
script.hasAttribute('nonce').should.be.eql(false);
25+
}
26+
__webpack_nonce__ = undefined;
27+
done();
28+
});

test/statsCases/aggressive-splitting-on-demand/expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Time: Xms
66
5ae9e18455b866684bd0.js 1.94 kB 2 [emitted]
77
e91ec4902ca3057b42bb.js 1.93 kB 3 [emitted]
88
0947f0875d56ab0bfe02.js 977 bytes 4 [emitted]
9-
6335d9dcc7fa048743b7.js 7.01 kB 6 [emitted] main
9+
6335d9dcc7fa048743b7.js 7.13 kB 6 [emitted] main
1010
cf500be0e585f01d2ccb.js 983 bytes 9 [emitted]
1111
a7bfb642a544b4302cc4.js 975 bytes 11 [emitted]
1212
Entrypoint main = 6335d9dcc7fa048743b7.js

test/statsCases/chunks/expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Time: Xms
44
0.bundle.js 227 bytes 0 [emitted]
55
1.bundle.js 106 bytes 1 [emitted]
66
2.bundle.js 200 bytes 2 [emitted]
7-
bundle.js 5.65 kB 3 [emitted] main
7+
bundle.js 5.77 kB 3 [emitted] main
88
chunk {0} 0.bundle.js 54 bytes {3} [rendered]
99
> [5] (webpack)/test/statsCases/chunks/index.js 3:0-16
1010
[2] (webpack)/test/statsCases/chunks/c.js 54 bytes {0} [built]

test/statsCases/optimize-chunks/expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Time: Xms
88
4.js 136 bytes 4, 6 [emitted] chunk
99
5.js 293 bytes 5, 3 [emitted] cir2 from cir1
1010
6.js 78 bytes 6 [emitted] ac in ab
11-
main.js 6.25 kB 7 [emitted] main
11+
main.js 6.37 kB 7 [emitted] main
1212
chunk {0} 0.js (cir1) 81 bytes {7} {5} {3} [rendered]
1313
> duplicate cir1 from cir2 [3] (webpack)/test/statsCases/optimize-chunks/circular2.js 1:0-79
1414
> duplicate cir1 [8] (webpack)/test/statsCases/optimize-chunks/index.js 13:0-54

test/statsCases/preset-verbose/expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Time: Xms
44
0.js 227 bytes 0 [emitted]
55
1.js 106 bytes 1 [emitted]
66
2.js 200 bytes 2 [emitted]
7-
main.js 5.64 kB 3 [emitted] main
7+
main.js 5.76 kB 3 [emitted] main
88
Entrypoint main = main.js
99
chunk {0} 0.js 54 bytes {3} [rendered]
1010
[2] (webpack)/test/statsCases/preset-verbose/c.js 54 bytes {0} [built]

0 commit comments

Comments
 (0)