From ce56e3b61c1489c21fd8e935f3301eafd503b967 Mon Sep 17 00:00:00 2001 From: Ivan Kopeykin Date: Mon, 9 May 2022 21:45:42 +0300 Subject: [PATCH] add NonceRuntimeModule --- lib/RuntimePlugin.js | 7 ++++++ lib/runtime/NonceRuntimeModule.js | 24 ++++++++++++++++++++ test/configCases/web/nonce/index.js | 5 ++++ test/configCases/web/nonce/nonce.js | 1 + test/configCases/web/nonce/webpack.config.js | 7 ++++++ 5 files changed, 44 insertions(+) create mode 100644 lib/runtime/NonceRuntimeModule.js create mode 100644 test/configCases/web/nonce/index.js create mode 100644 test/configCases/web/nonce/nonce.js create mode 100644 test/configCases/web/nonce/webpack.config.js diff --git a/lib/RuntimePlugin.js b/lib/RuntimePlugin.js index 3e0fc6e4e50..624473d37b5 100644 --- a/lib/RuntimePlugin.js +++ b/lib/RuntimePlugin.js @@ -26,6 +26,7 @@ const GlobalRuntimeModule = require("./runtime/GlobalRuntimeModule"); const HasOwnPropertyRuntimeModule = require("./runtime/HasOwnPropertyRuntimeModule"); const LoadScriptRuntimeModule = require("./runtime/LoadScriptRuntimeModule"); const MakeNamespaceObjectRuntimeModule = require("./runtime/MakeNamespaceObjectRuntimeModule"); +const NonceRuntimeModule = require("./runtime/NonceRuntimeModule"); const OnChunksLoadedRuntimeModule = require("./runtime/OnChunksLoadedRuntimeModule"); const PublicPathRuntimeModule = require("./runtime/PublicPathRuntimeModule"); const RelativeUrlRuntimeModule = require("./runtime/RelativeUrlRuntimeModule"); @@ -431,6 +432,12 @@ class RuntimePlugin { return true; } }); + compilation.hooks.runtimeRequirementInTree + .for(RuntimeGlobals.scriptNonce) + .tap("RuntimePlugin", chunk => { + compilation.addRuntimeModule(chunk, new NonceRuntimeModule()); + return true; + }); // TODO webpack 6: remove CompatRuntimeModule compilation.hooks.additionalTreeRuntimeRequirements.tap( "RuntimePlugin", diff --git a/lib/runtime/NonceRuntimeModule.js b/lib/runtime/NonceRuntimeModule.js new file mode 100644 index 00000000000..b160c612def --- /dev/null +++ b/lib/runtime/NonceRuntimeModule.js @@ -0,0 +1,24 @@ +/* + MIT License http://www.opensource.org/licenses/mit-license.php + Author Ivan Kopeykin @vankop +*/ + +"use strict"; + +const RuntimeGlobals = require("../RuntimeGlobals"); +const RuntimeModule = require("../RuntimeModule"); + +class NonceRuntimeModule extends RuntimeModule { + constructor() { + super("nonce", RuntimeModule.STAGE_ATTACH); + } + + /** + * @returns {string} runtime code + */ + generate() { + return `${RuntimeGlobals.scriptNonce} = undefined;`; + } +} + +module.exports = NonceRuntimeModule; diff --git a/test/configCases/web/nonce/index.js b/test/configCases/web/nonce/index.js new file mode 100644 index 00000000000..f8cfa5d426f --- /dev/null +++ b/test/configCases/web/nonce/index.js @@ -0,0 +1,5 @@ +import "./nonce"; + +it("should set nonce", () => { + expect(__webpack_nonce__).toBe("nonce"); +}); diff --git a/test/configCases/web/nonce/nonce.js b/test/configCases/web/nonce/nonce.js new file mode 100644 index 00000000000..60f0dee8e11 --- /dev/null +++ b/test/configCases/web/nonce/nonce.js @@ -0,0 +1 @@ +__webpack_nonce__ = "nonce"; diff --git a/test/configCases/web/nonce/webpack.config.js b/test/configCases/web/nonce/webpack.config.js new file mode 100644 index 00000000000..375abb14705 --- /dev/null +++ b/test/configCases/web/nonce/webpack.config.js @@ -0,0 +1,7 @@ +const webpack = require("../../../../"); +/** @type {import("../../../../").Configuration} */ +module.exports = { + target: "web", + // plugin that intercepts __webpack_require__ + plugins: [new webpack.HotModuleReplacementPlugin()] +};