Skip to content

Commit

Permalink
Merge pull request #16388 from indeediansbrett/feat/issue-16374
Browse files Browse the repository at this point in the history
feat: ignoreBrowserWarnings option to ignore browser console warnings
  • Loading branch information
TheLarkInn committed Apr 26, 2023
2 parents 7398dbd + f5d368e commit 2c6dc77
Show file tree
Hide file tree
Showing 39 changed files with 548 additions and 17 deletions.
8 changes: 8 additions & 0 deletions declarations/WebpackOptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2096,6 +2096,10 @@ export interface Output {
* The filename of the Hot Update Main File. It is inside the 'output.path' directory.
*/
hotUpdateMainFilename?: HotUpdateMainFilename;
/**
* Ignore warnings in the browser.
*/
ignoreBrowserWarnings?: boolean;
/**
* Wrap javascript code into IIFE's to avoid leaking into global scope.
*/
Expand Down Expand Up @@ -3305,6 +3309,10 @@ export interface OutputNormalized {
* The filename of the Hot Update Main File. It is inside the 'output.path' directory.
*/
hotUpdateMainFilename?: HotUpdateMainFilename;
/**
* Ignore warnings in the browser.
*/
ignoreBrowserWarnings?: boolean;
/**
* Wrap javascript code into IIFE's to avoid leaking into global scope.
*/
Expand Down
43 changes: 35 additions & 8 deletions examples/module-federation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,11 @@ module.exports = new Promise((resolve, reject) => {
/******/ if(!__webpack_require__.o(__webpack_require__.S, name)) __webpack_require__.S[name] = {};
/******/ // runs all init snippets from all modules reachable
/******/ var scope = __webpack_require__.S[name];
/******/ var warn = (msg) => (typeof console !== "undefined" && console.warn && console.warn(msg));
/******/ var warn = (msg) => {
/******/ typeof console !== "undefined" && console.warn ? console.warn(msg) : (() => {
/******/
/******/ })()
/******/ };
/******/ var uniqueName = "module-federation-aaa";
/******/ var register = (name, version, factory, eager) => {
/******/ var versions = scope[name] = scope[name] || {};
Expand Down Expand Up @@ -789,8 +793,13 @@ module.exports = new Promise((resolve, reject) => {
/******/ if(entry) return get(entry);
/******/ throw new Error(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));
/******/ };
/******/ var warn = (msg) => {
/******/ typeof console !== "undefined" && console.warn ? console.warn(msg) : (() => {
/******/
/******/ })()
/******/ };
/******/ var warnInvalidVersion = (scope, scopeName, key, requiredVersion) => {
/******/ typeof console !== "undefined" && console.warn && console.warn(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));
/******/ warn(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));
/******/ };
/******/ var get = (entry) => {
/******/ entry.loaded = 1;
Expand Down Expand Up @@ -1208,7 +1217,11 @@ __webpack_require__.d(exports, {
/******/ if(!__webpack_require__.o(__webpack_require__.S, name)) __webpack_require__.S[name] = {};
/******/ // runs all init snippets from all modules reachable
/******/ var scope = __webpack_require__.S[name];
/******/ var warn = (msg) => (typeof console !== "undefined" && console.warn && console.warn(msg));
/******/ var warn = (msg) => {
/******/ typeof console !== "undefined" && console.warn ? console.warn(msg) : (() => {
/******/
/******/ })()
/******/ };
/******/ var uniqueName = "module-federation-bbb";
/******/ var register = (name, version, factory, eager) => {
/******/ var versions = scope[name] = scope[name] || {};
Expand Down Expand Up @@ -1289,7 +1302,7 @@ __webpack_require__.d(exports, {
/******/ };
/******/ var getSingletonVersion = (scope, scopeName, key, requiredVersion) => {
/******/ var version = findSingletonVersionKey(scope, key);
/******/ if (!satisfy(requiredVersion, version)) typeof console !== "undefined" && console.warn && console.warn(getInvalidSingletonVersionMessage(scope, key, version, requiredVersion));
/******/ if (!satisfy(requiredVersion, version)) warn(getInvalidSingletonVersionMessage(scope, key, version, requiredVersion));
/******/ return get(scope[key][version]);
/******/ };
/******/ var getStrictSingletonVersion = (scope, scopeName, key, requiredVersion) => {
Expand Down Expand Up @@ -1317,8 +1330,13 @@ __webpack_require__.d(exports, {
/******/ if(entry) return get(entry);
/******/ throw new Error(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));
/******/ };
/******/ var warn = (msg) => {
/******/ typeof console !== "undefined" && console.warn ? console.warn(msg) : (() => {
/******/
/******/ })()
/******/ };
/******/ var warnInvalidVersion = (scope, scopeName, key, requiredVersion) => {
/******/ typeof console !== "undefined" && console.warn && console.warn(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));
/******/ warn(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));
/******/ };
/******/ var get = (entry) => {
/******/ entry.loaded = 1;
Expand Down Expand Up @@ -1754,7 +1772,11 @@ __webpack_require__.d(exports, {
/******/ if(!__webpack_require__.o(__webpack_require__.S, name)) __webpack_require__.S[name] = {};
/******/ // runs all init snippets from all modules reachable
/******/ var scope = __webpack_require__.S[name];
/******/ var warn = (msg) => (typeof console !== "undefined" && console.warn && console.warn(msg));
/******/ var warn = (msg) => {
/******/ typeof console !== "undefined" && console.warn ? console.warn(msg) : (() => {
/******/
/******/ })()
/******/ };
/******/ var uniqueName = "module-federation-ccc";
/******/ var register = (name, version, factory, eager) => {
/******/ var versions = scope[name] = scope[name] || {};
Expand Down Expand Up @@ -1835,7 +1857,7 @@ __webpack_require__.d(exports, {
/******/ };
/******/ var getSingletonVersion = (scope, scopeName, key, requiredVersion) => {
/******/ var version = findSingletonVersionKey(scope, key);
/******/ if (!satisfy(requiredVersion, version)) typeof console !== "undefined" && console.warn && console.warn(getInvalidSingletonVersionMessage(scope, key, version, requiredVersion));
/******/ if (!satisfy(requiredVersion, version)) warn(getInvalidSingletonVersionMessage(scope, key, version, requiredVersion));
/******/ return get(scope[key][version]);
/******/ };
/******/ var getStrictSingletonVersion = (scope, scopeName, key, requiredVersion) => {
Expand Down Expand Up @@ -1863,8 +1885,13 @@ __webpack_require__.d(exports, {
/******/ if(entry) return get(entry);
/******/ throw new Error(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));
/******/ };
/******/ var warn = (msg) => {
/******/ typeof console !== "undefined" && console.warn ? console.warn(msg) : (() => {
/******/
/******/ })()
/******/ };
/******/ var warnInvalidVersion = (scope, scopeName, key, requiredVersion) => {
/******/ typeof console !== "undefined" && console.warn && console.warn(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));
/******/ warn(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));
/******/ };
/******/ var get = (entry) => {
/******/ entry.loaded = 1;
Expand Down
1 change: 1 addition & 0 deletions lib/config/normalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ const getNormalizedWebpackOptions = config => {
hotUpdateChunkFilename: output.hotUpdateChunkFilename,
hotUpdateGlobal: output.hotUpdateGlobal,
hotUpdateMainFilename: output.hotUpdateMainFilename,
ignoreBrowserWarnings: output.ignoreBrowserWarnings,
iife: output.iife,
importFunctionName: output.importFunctionName,
importMetaName: output.importMetaName,
Expand Down
12 changes: 9 additions & 3 deletions lib/sharing/ConsumeSharedRuntimeModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ class ConsumeSharedRuntimeModule extends RuntimeModule {
"scope, scopeName, key, requiredVersion",
[
"var version = findSingletonVersionKey(scope, key);",
"if (!satisfy(requiredVersion, version)) " +
'typeof console !== "undefined" && console.warn && console.warn(getInvalidSingletonVersionMessage(scope, key, version, requiredVersion));',
"if (!satisfy(requiredVersion, version)) warn(getInvalidSingletonVersionMessage(scope, key, version, requiredVersion));",
"return get(scope[key][version]);"
]
)};`,
Expand Down Expand Up @@ -166,10 +165,17 @@ class ConsumeSharedRuntimeModule extends RuntimeModule {
"throw new Error(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));"
]
)};`,
`var warn = ${
this.compilation.options.output.ignoreBrowserWarnings
? runtimeTemplate.basicFunction("", "")
: runtimeTemplate.basicFunction("msg", [
'if (typeof console !== "undefined" && console.warn) console.warn(msg);'
])
};`,
`var warnInvalidVersion = ${runtimeTemplate.basicFunction(
"scope, scopeName, key, requiredVersion",
[
'typeof console !== "undefined" && console.warn && console.warn(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));'
"warn(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));"
]
)};`,
`var get = ${runtimeTemplate.basicFunction("entry", [
Expand Down
11 changes: 7 additions & 4 deletions lib/sharing/ShareRuntimeModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,13 @@ class ShareRuntimeModule extends RuntimeModule {
`if(!${RuntimeGlobals.hasOwnProperty}(${RuntimeGlobals.shareScopeMap}, name)) ${RuntimeGlobals.shareScopeMap}[name] = {};`,
"// runs all init snippets from all modules reachable",
`var scope = ${RuntimeGlobals.shareScopeMap}[name];`,
`var warn = ${runtimeTemplate.returningFunction(
'typeof console !== "undefined" && console.warn && console.warn(msg)',
"msg"
)};`,
`var warn = ${
this.compilation.options.output.ignoreBrowserWarnings
? runtimeTemplate.basicFunction("", "")
: runtimeTemplate.basicFunction("msg", [
'if (typeof console !== "undefined" && console.warn) console.warn(msg);'
])
};`,
`var uniqueName = ${JSON.stringify(uniqueName || undefined)};`,
`var register = ${runtimeTemplate.basicFunction(
"name, version, factory, eager",
Expand Down
2 changes: 1 addition & 1 deletion schemas/WebpackOptions.check.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions schemas/WebpackOptions.json
Original file line number Diff line number Diff line change
Expand Up @@ -3162,6 +3162,10 @@
"hotUpdateMainFilename": {
"$ref": "#/definitions/HotUpdateMainFilename"
},
"ignoreBrowserWarnings": {
"description": "Ignore warnings in the browser.",
"type": "boolean"
},
"iife": {
"$ref": "#/definitions/Iife"
},
Expand Down Expand Up @@ -3364,6 +3368,10 @@
"hotUpdateMainFilename": {
"$ref": "#/definitions/HotUpdateMainFilename"
},
"ignoreBrowserWarnings": {
"description": "Ignore warnings in the browser.",
"type": "boolean"
},
"iife": {
"$ref": "#/definitions/Iife"
},
Expand Down
1 change: 1 addition & 0 deletions test/Defaults.unittest.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ describe("snapshots", () => {
"hotUpdateChunkFilename": "[id].[fullhash].hot-update.js",
"hotUpdateGlobal": "webpackHotUpdatewebpack",
"hotUpdateMainFilename": "[runtime].[fullhash].hot-update.json",
"ignoreBrowserWarnings": undefined,
"iife": true,
"importFunctionName": "import",
"importMetaName": "import.meta",
Expand Down
2 changes: 1 addition & 1 deletion test/Validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ describe("Validation", () => {
expect(msg).toMatchInlineSnapshot(`
"Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
- configuration.output has an unknown property 'ecmaVersion'. These properties are valid:
object { amdContainer?, assetModuleFilename?, asyncChunks?, auxiliaryComment?, charset?, chunkFilename?, chunkFormat?, chunkLoadTimeout?, chunkLoading?, chunkLoadingGlobal?, clean?, compareBeforeEmit?, crossOriginLoading?, cssChunkFilename?, cssFilename?, devtoolFallbackModuleFilenameTemplate?, devtoolModuleFilenameTemplate?, devtoolNamespace?, enabledChunkLoadingTypes?, enabledLibraryTypes?, enabledWasmLoadingTypes?, environment?, filename?, globalObject?, hashDigest?, hashDigestLength?, hashFunction?, hashSalt?, hotUpdateChunkFilename?, hotUpdateGlobal?, hotUpdateMainFilename?, iife?, importFunctionName?, importMetaName?, library?, libraryExport?, libraryTarget?, module?, path?, pathinfo?, publicPath?, scriptType?, sourceMapFilename?, sourcePrefix?, strictModuleErrorHandling?, strictModuleExceptionHandling?, trustedTypes?, umdNamedDefine?, uniqueName?, wasmLoading?, webassemblyModuleFilename?, workerChunkLoading?, workerPublicPath?, workerWasmLoading? }
object { amdContainer?, assetModuleFilename?, asyncChunks?, auxiliaryComment?, charset?, chunkFilename?, chunkFormat?, chunkLoadTimeout?, chunkLoading?, chunkLoadingGlobal?, clean?, compareBeforeEmit?, crossOriginLoading?, cssChunkFilename?, cssFilename?, devtoolFallbackModuleFilenameTemplate?, devtoolModuleFilenameTemplate?, devtoolNamespace?, enabledChunkLoadingTypes?, enabledLibraryTypes?, enabledWasmLoadingTypes?, environment?, filename?, globalObject?, hashDigest?, hashDigestLength?, hashFunction?, hashSalt?, hotUpdateChunkFilename?, hotUpdateGlobal?, hotUpdateMainFilename?, ignoreBrowserWarnings?, iife?, importFunctionName?, importMetaName?, library?, libraryExport?, libraryTarget?, module?, path?, pathinfo?, publicPath?, scriptType?, sourceMapFilename?, sourcePrefix?, strictModuleErrorHandling?, strictModuleExceptionHandling?, trustedTypes?, umdNamedDefine?, uniqueName?, wasmLoading?, webassemblyModuleFilename?, workerChunkLoading?, workerPublicPath?, workerWasmLoading? }
-> Options affecting the output of the compilation. \`output\` options tell webpack how to write the compiled files to disk.
Did you mean output.environment (output.ecmaVersion was a temporary configuration option during webpack 5 beta)?"
`)
Expand Down
13 changes: 13 additions & 0 deletions test/__snapshots__/Cli.basictest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6076,6 +6076,19 @@ Object {
"multiple": false,
"simpleType": "string",
},
"output-ignore-browser-warnings": Object {
"configs": Array [
Object {
"description": "Ignore warnings in the browser.",
"multiple": false,
"path": "output.ignoreBrowserWarnings",
"type": "boolean",
},
],
"description": "Ignore warnings in the browser.",
"multiple": false,
"simpleType": "boolean",
},
"output-iife": Object {
"configs": Array [
Object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = [[/prefix\/deep\/c/]];

0 comments on commit 2c6dc77

Please sign in to comment.