File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -208,6 +208,13 @@ function createStyleElement (options) {
208208 options . attrs . type = "text/css" ;
209209 }
210210
211+ if ( options . attrs . nonce === undefined ) {
212+ const nonce = getNonce ( ) ;
213+ if ( nonce ) {
214+ options . attrs . nonce = nonce ;
215+ }
216+ }
217+
211218 addAttrs ( style , options . attrs ) ;
212219 insertStyleElement ( options , style ) ;
213220
@@ -234,6 +241,14 @@ function addAttrs (el, attrs) {
234241 } ) ;
235242}
236243
244+ function getNonce ( ) {
245+ if ( typeof __webpack_nonce__ === 'undefined' ) {
246+ return null ;
247+ }
248+
249+ return __webpack_nonce__ ;
250+ }
251+
237252function addStyle ( obj , options ) {
238253 var style , update , remove , result ;
239254
Original file line number Diff line number Diff line change @@ -244,6 +244,27 @@ describe("basic tests", function() {
244244 runCompilerTest ( expected , done ) ;
245245 } ) ; // it attrs
246246
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+
247268 it ( "type attribute" , function ( done ) {
248269 // Setup
249270 styleLoaderOptions . attrs = { type : 'text/less' } ;
You can’t perform that action at this time.
0 commit comments