Skip to content

Commit 2c6dc77

Browse files
authored
Merge pull request #16388 from indeediansbrett/feat/issue-16374
feat: ignoreBrowserWarnings option to ignore browser console warnings
2 parents 7398dbd + f5d368e commit 2c6dc77

39 files changed

Lines changed: 548 additions & 17 deletions

declarations/WebpackOptions.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,6 +2096,10 @@ export interface Output {
20962096
* The filename of the Hot Update Main File. It is inside the 'output.path' directory.
20972097
*/
20982098
hotUpdateMainFilename?: HotUpdateMainFilename;
2099+
/**
2100+
* Ignore warnings in the browser.
2101+
*/
2102+
ignoreBrowserWarnings?: boolean;
20992103
/**
21002104
* Wrap javascript code into IIFE's to avoid leaking into global scope.
21012105
*/
@@ -3305,6 +3309,10 @@ export interface OutputNormalized {
33053309
* The filename of the Hot Update Main File. It is inside the 'output.path' directory.
33063310
*/
33073311
hotUpdateMainFilename?: HotUpdateMainFilename;
3312+
/**
3313+
* Ignore warnings in the browser.
3314+
*/
3315+
ignoreBrowserWarnings?: boolean;
33083316
/**
33093317
* Wrap javascript code into IIFE's to avoid leaking into global scope.
33103318
*/

examples/module-federation/README.md

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,11 @@ module.exports = new Promise((resolve, reject) => {
679679
/******/ if(!__webpack_require__.o(__webpack_require__.S, name)) __webpack_require__.S[name] = {};
680680
/******/ // runs all init snippets from all modules reachable
681681
/******/ var scope = __webpack_require__.S[name];
682-
/******/ var warn = (msg) => (typeof console !== "undefined" && console.warn && console.warn(msg));
682+
/******/ var warn = (msg) => {
683+
/******/ typeof console !== "undefined" && console.warn ? console.warn(msg) : (() => {
684+
/******/
685+
/******/ })()
686+
/******/ };
683687
/******/ var uniqueName = "module-federation-aaa";
684688
/******/ var register = (name, version, factory, eager) => {
685689
/******/ var versions = scope[name] = scope[name] || {};
@@ -789,8 +793,13 @@ module.exports = new Promise((resolve, reject) => {
789793
/******/ if(entry) return get(entry);
790794
/******/ throw new Error(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));
791795
/******/ };
796+
/******/ var warn = (msg) => {
797+
/******/ typeof console !== "undefined" && console.warn ? console.warn(msg) : (() => {
798+
/******/
799+
/******/ })()
800+
/******/ };
792801
/******/ var warnInvalidVersion = (scope, scopeName, key, requiredVersion) => {
793-
/******/ typeof console !== "undefined" && console.warn && console.warn(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));
802+
/******/ warn(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));
794803
/******/ };
795804
/******/ var get = (entry) => {
796805
/******/ entry.loaded = 1;
@@ -1208,7 +1217,11 @@ __webpack_require__.d(exports, {
12081217
/******/ if(!__webpack_require__.o(__webpack_require__.S, name)) __webpack_require__.S[name] = {};
12091218
/******/ // runs all init snippets from all modules reachable
12101219
/******/ var scope = __webpack_require__.S[name];
1211-
/******/ var warn = (msg) => (typeof console !== "undefined" && console.warn && console.warn(msg));
1220+
/******/ var warn = (msg) => {
1221+
/******/ typeof console !== "undefined" && console.warn ? console.warn(msg) : (() => {
1222+
/******/
1223+
/******/ })()
1224+
/******/ };
12121225
/******/ var uniqueName = "module-federation-bbb";
12131226
/******/ var register = (name, version, factory, eager) => {
12141227
/******/ var versions = scope[name] = scope[name] || {};
@@ -1289,7 +1302,7 @@ __webpack_require__.d(exports, {
12891302
/******/ };
12901303
/******/ var getSingletonVersion = (scope, scopeName, key, requiredVersion) => {
12911304
/******/ var version = findSingletonVersionKey(scope, key);
1292-
/******/ if (!satisfy(requiredVersion, version)) typeof console !== "undefined" && console.warn && console.warn(getInvalidSingletonVersionMessage(scope, key, version, requiredVersion));
1305+
/******/ if (!satisfy(requiredVersion, version)) warn(getInvalidSingletonVersionMessage(scope, key, version, requiredVersion));
12931306
/******/ return get(scope[key][version]);
12941307
/******/ };
12951308
/******/ var getStrictSingletonVersion = (scope, scopeName, key, requiredVersion) => {
@@ -1317,8 +1330,13 @@ __webpack_require__.d(exports, {
13171330
/******/ if(entry) return get(entry);
13181331
/******/ throw new Error(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));
13191332
/******/ };
1333+
/******/ var warn = (msg) => {
1334+
/******/ typeof console !== "undefined" && console.warn ? console.warn(msg) : (() => {
1335+
/******/
1336+
/******/ })()
1337+
/******/ };
13201338
/******/ var warnInvalidVersion = (scope, scopeName, key, requiredVersion) => {
1321-
/******/ typeof console !== "undefined" && console.warn && console.warn(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));
1339+
/******/ warn(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));
13221340
/******/ };
13231341
/******/ var get = (entry) => {
13241342
/******/ entry.loaded = 1;
@@ -1754,7 +1772,11 @@ __webpack_require__.d(exports, {
17541772
/******/ if(!__webpack_require__.o(__webpack_require__.S, name)) __webpack_require__.S[name] = {};
17551773
/******/ // runs all init snippets from all modules reachable
17561774
/******/ var scope = __webpack_require__.S[name];
1757-
/******/ var warn = (msg) => (typeof console !== "undefined" && console.warn && console.warn(msg));
1775+
/******/ var warn = (msg) => {
1776+
/******/ typeof console !== "undefined" && console.warn ? console.warn(msg) : (() => {
1777+
/******/
1778+
/******/ })()
1779+
/******/ };
17581780
/******/ var uniqueName = "module-federation-ccc";
17591781
/******/ var register = (name, version, factory, eager) => {
17601782
/******/ var versions = scope[name] = scope[name] || {};
@@ -1835,7 +1857,7 @@ __webpack_require__.d(exports, {
18351857
/******/ };
18361858
/******/ var getSingletonVersion = (scope, scopeName, key, requiredVersion) => {
18371859
/******/ var version = findSingletonVersionKey(scope, key);
1838-
/******/ if (!satisfy(requiredVersion, version)) typeof console !== "undefined" && console.warn && console.warn(getInvalidSingletonVersionMessage(scope, key, version, requiredVersion));
1860+
/******/ if (!satisfy(requiredVersion, version)) warn(getInvalidSingletonVersionMessage(scope, key, version, requiredVersion));
18391861
/******/ return get(scope[key][version]);
18401862
/******/ };
18411863
/******/ var getStrictSingletonVersion = (scope, scopeName, key, requiredVersion) => {
@@ -1863,8 +1885,13 @@ __webpack_require__.d(exports, {
18631885
/******/ if(entry) return get(entry);
18641886
/******/ throw new Error(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));
18651887
/******/ };
1888+
/******/ var warn = (msg) => {
1889+
/******/ typeof console !== "undefined" && console.warn ? console.warn(msg) : (() => {
1890+
/******/
1891+
/******/ })()
1892+
/******/ };
18661893
/******/ var warnInvalidVersion = (scope, scopeName, key, requiredVersion) => {
1867-
/******/ typeof console !== "undefined" && console.warn && console.warn(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));
1894+
/******/ warn(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));
18681895
/******/ };
18691896
/******/ var get = (entry) => {
18701897
/******/ entry.loaded = 1;

lib/config/normalization.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ const getNormalizedWebpackOptions = config => {
327327
hotUpdateChunkFilename: output.hotUpdateChunkFilename,
328328
hotUpdateGlobal: output.hotUpdateGlobal,
329329
hotUpdateMainFilename: output.hotUpdateMainFilename,
330+
ignoreBrowserWarnings: output.ignoreBrowserWarnings,
330331
iife: output.iife,
331332
importFunctionName: output.importFunctionName,
332333
importMetaName: output.importMetaName,

lib/sharing/ConsumeSharedRuntimeModule.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ class ConsumeSharedRuntimeModule extends RuntimeModule {
119119
"scope, scopeName, key, requiredVersion",
120120
[
121121
"var version = findSingletonVersionKey(scope, key);",
122-
"if (!satisfy(requiredVersion, version)) " +
123-
'typeof console !== "undefined" && console.warn && console.warn(getInvalidSingletonVersionMessage(scope, key, version, requiredVersion));',
122+
"if (!satisfy(requiredVersion, version)) warn(getInvalidSingletonVersionMessage(scope, key, version, requiredVersion));",
124123
"return get(scope[key][version]);"
125124
]
126125
)};`,
@@ -166,10 +165,17 @@ class ConsumeSharedRuntimeModule extends RuntimeModule {
166165
"throw new Error(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));"
167166
]
168167
)};`,
168+
`var warn = ${
169+
this.compilation.options.output.ignoreBrowserWarnings
170+
? runtimeTemplate.basicFunction("", "")
171+
: runtimeTemplate.basicFunction("msg", [
172+
'if (typeof console !== "undefined" && console.warn) console.warn(msg);'
173+
])
174+
};`,
169175
`var warnInvalidVersion = ${runtimeTemplate.basicFunction(
170176
"scope, scopeName, key, requiredVersion",
171177
[
172-
'typeof console !== "undefined" && console.warn && console.warn(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));'
178+
"warn(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));"
173179
]
174180
)};`,
175181
`var get = ${runtimeTemplate.basicFunction("entry", [

lib/sharing/ShareRuntimeModule.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,13 @@ class ShareRuntimeModule extends RuntimeModule {
7777
`if(!${RuntimeGlobals.hasOwnProperty}(${RuntimeGlobals.shareScopeMap}, name)) ${RuntimeGlobals.shareScopeMap}[name] = {};`,
7878
"// runs all init snippets from all modules reachable",
7979
`var scope = ${RuntimeGlobals.shareScopeMap}[name];`,
80-
`var warn = ${runtimeTemplate.returningFunction(
81-
'typeof console !== "undefined" && console.warn && console.warn(msg)',
82-
"msg"
83-
)};`,
80+
`var warn = ${
81+
this.compilation.options.output.ignoreBrowserWarnings
82+
? runtimeTemplate.basicFunction("", "")
83+
: runtimeTemplate.basicFunction("msg", [
84+
'if (typeof console !== "undefined" && console.warn) console.warn(msg);'
85+
])
86+
};`,
8487
`var uniqueName = ${JSON.stringify(uniqueName || undefined)};`,
8588
`var register = ${runtimeTemplate.basicFunction(
8689
"name, version, factory, eager",

schemas/WebpackOptions.check.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

schemas/WebpackOptions.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3162,6 +3162,10 @@
31623162
"hotUpdateMainFilename": {
31633163
"$ref": "#/definitions/HotUpdateMainFilename"
31643164
},
3165+
"ignoreBrowserWarnings": {
3166+
"description": "Ignore warnings in the browser.",
3167+
"type": "boolean"
3168+
},
31653169
"iife": {
31663170
"$ref": "#/definitions/Iife"
31673171
},
@@ -3364,6 +3368,10 @@
33643368
"hotUpdateMainFilename": {
33653369
"$ref": "#/definitions/HotUpdateMainFilename"
33663370
},
3371+
"ignoreBrowserWarnings": {
3372+
"description": "Ignore warnings in the browser.",
3373+
"type": "boolean"
3374+
},
33673375
"iife": {
33683376
"$ref": "#/definitions/Iife"
33693377
},

test/Defaults.unittest.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ describe("snapshots", () => {
343343
"hotUpdateChunkFilename": "[id].[fullhash].hot-update.js",
344344
"hotUpdateGlobal": "webpackHotUpdatewebpack",
345345
"hotUpdateMainFilename": "[runtime].[fullhash].hot-update.json",
346+
"ignoreBrowserWarnings": undefined,
346347
"iife": true,
347348
"importFunctionName": "import",
348349
"importMetaName": "import.meta",

test/Validation.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ describe("Validation", () => {
498498
expect(msg).toMatchInlineSnapshot(`
499499
"Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
500500
- configuration.output has an unknown property 'ecmaVersion'. These properties are valid:
501-
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? }
501+
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? }
502502
-> Options affecting the output of the compilation. \`output\` options tell webpack how to write the compiled files to disk.
503503
Did you mean output.environment (output.ecmaVersion was a temporary configuration option during webpack 5 beta)?"
504504
`)

test/__snapshots__/Cli.basictest.js.snap

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6076,6 +6076,19 @@ Object {
60766076
"multiple": false,
60776077
"simpleType": "string",
60786078
},
6079+
"output-ignore-browser-warnings": Object {
6080+
"configs": Array [
6081+
Object {
6082+
"description": "Ignore warnings in the browser.",
6083+
"multiple": false,
6084+
"path": "output.ignoreBrowserWarnings",
6085+
"type": "boolean",
6086+
},
6087+
],
6088+
"description": "Ignore warnings in the browser.",
6089+
"multiple": false,
6090+
"simpleType": "boolean",
6091+
},
60796092
"output-iife": Object {
60806093
"configs": Array [
60816094
Object {

0 commit comments

Comments
 (0)