Skip to content

Commit

Permalink
feat: ignoreBrowserWarnings option to ignore browser console warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
indeediansbrett committed Oct 21, 2022
1 parent 9fcaa24 commit 0975d13
Show file tree
Hide file tree
Showing 39 changed files with 690 additions and 111 deletions.
8 changes: 8 additions & 0 deletions declarations/WebpackOptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2080,6 +2080,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 @@ -3277,6 +3281,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
281 changes: 180 additions & 101 deletions examples/module-federation/README.md

Large diffs are not rendered by default.

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
10 changes: 7 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 (warn && !satisfy(requiredVersion, version)) warn(getInvalidSingletonVersionMessage(scope, key, version, requiredVersion));",
"return get(scope[key][version]);"
]
)};`,
Expand Down Expand Up @@ -166,10 +165,15 @@ class ConsumeSharedRuntimeModule extends RuntimeModule {
"throw new Error(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));"
]
)};`,
`var warn = ${
this.compilation.options.output.ignoreBrowserWarnings
? "undefined"
: 'typeof console !== "undefined" && console.warn'
};`,
`var warnInvalidVersion = ${runtimeTemplate.basicFunction(
"scope, scopeName, key, requiredVersion",
[
'typeof console !== "undefined" && console.warn && console.warn(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));'
"warn && warn(getInvalidVersionMessage(scope, scopeName, key, requiredVersion));"
]
)};`,
`var get = ${runtimeTemplate.basicFunction("entry", [
Expand Down
11 changes: 6 additions & 5 deletions lib/sharing/ShareRuntimeModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ 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
? "undefined"
: 'typeof console !== "undefined" && console.warn'
};`,
`var uniqueName = ${JSON.stringify(uniqueName || undefined)};`,
`var register = ${runtimeTemplate.basicFunction(
"name, version, factory, eager",
Expand All @@ -92,7 +93,7 @@ class ShareRuntimeModule extends RuntimeModule {
)};`,
`var initExternal = ${runtimeTemplate.basicFunction("id", [
`var handleError = ${runtimeTemplate.expressionFunction(
'warn("Initialization of sharing external failed: " + err)',
'warn && warn("Initialization of sharing external failed: " + err)',
"err"
)};`,
"try {",
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 @@ -3144,6 +3144,10 @@
"hotUpdateMainFilename": {
"$ref": "#/definitions/HotUpdateMainFilename"
},
"ignoreBrowserWarnings": {
"description": "Ignore warnings in the browser.",
"type": "boolean"
},
"iife": {
"$ref": "#/definitions/Iife"
},
Expand Down Expand Up @@ -3343,6 +3347,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 { 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?, workerWasmLoading? }
object { 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?, 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 0975d13

Please sign in to comment.