File tree 2 files changed +36
-0
lines changed
2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -208,6 +208,13 @@ function createStyleElement (options) {
208
208
options . attrs . type = "text/css" ;
209
209
}
210
210
211
+ if ( options . attrs . nonce === undefined ) {
212
+ const nonce = getNonce ( ) ;
213
+ if ( nonce ) {
214
+ options . attrs . nonce = nonce ;
215
+ }
216
+ }
217
+
211
218
addAttrs ( style , options . attrs ) ;
212
219
insertStyleElement ( options , style ) ;
213
220
@@ -234,6 +241,14 @@ function addAttrs (el, attrs) {
234
241
} ) ;
235
242
}
236
243
244
+ function getNonce ( ) {
245
+ if ( typeof __webpack_nonce__ === 'undefined' ) {
246
+ return null ;
247
+ }
248
+
249
+ return __webpack_nonce__ ;
250
+ }
251
+
237
252
function addStyle ( obj , options ) {
238
253
var style , update , remove , result ;
239
254
Original file line number Diff line number Diff line change @@ -244,6 +244,27 @@ describe("basic tests", function() {
244
244
runCompilerTest ( expected , done ) ;
245
245
} ) ; // it attrs
246
246
247
+ it ( "nonce" , function ( done ) {
248
+ // Setup
249
+ const expectedNonce = "testNonce" ;
250
+
251
+ fs . writeFileSync (
252
+ rootDir + "main.js" ,
253
+ [
254
+ `__webpack_nonce__ = '${ expectedNonce } '` ,
255
+ "var a = require('./style.css');"
256
+ ] . join ( "\n" )
257
+ ) ;
258
+
259
+ // Run
260
+ let expected = [
261
+ existingStyle ,
262
+ `<style type="text/css" nonce="${ expectedNonce } ">${ requiredCss } </style>`
263
+ ] . join ( "\n" ) ;
264
+
265
+ runCompilerTest ( expected , done ) ;
266
+ } ) ; // it attrs
267
+
247
268
it ( "type attribute" , function ( done ) {
248
269
// Setup
249
270
styleLoaderOptions . attrs = { type : 'text/less' } ;
You can’t perform that action at this time.
0 commit comments