add config.ignoreWarnings

deprecate stats.warningsFilter
This commit is contained in:
Tobias Koppers 2020-09-20 22:08:38 +02:00
parent 07fc554bef
commit ea075be453
15 changed files with 326 additions and 14 deletions

View File

@ -180,6 +180,30 @@ export type ExternalsType =
| "promise"
| "import"
| "script";
/**
* Ignore specific warnings.
*/
export type IgnoreWarnings = (
| RegExp
| {
/**
* A RegExp to select the origin file for the warning.
*/
file?: RegExp;
/**
* A RegExp to select the warning message.
*/
message?: RegExp;
/**
* A RegExp to select the origin module for the warning.
*/
module?: RegExp;
}
| ((
warning: import("../lib/WebpackError"),
compilation: import("../lib/Compilation")
) => boolean)
)[];
/**
* Filtering values.
*/
@ -588,6 +612,13 @@ export type EntryDynamicNormalized = () => Promise<EntryStaticNormalized>;
* The entry point(s) of the compilation.
*/
export type EntryNormalized = EntryDynamicNormalized | EntryStaticNormalized;
/**
* Ignore specific warnings.
*/
export type IgnoreWarningsNormalized = ((
warning: import("../lib/WebpackError"),
compilation: import("../lib/Compilation")
) => boolean)[];
/**
* Create an additional chunk which contains only the webpack runtime and chunk hash maps.
*/
@ -661,6 +692,10 @@ export interface WebpackOptions {
* Specifies the default type of externals ('amd*', 'umd*', 'system' and 'jsonp' depend on output.libraryTarget set to the same value).
*/
externalsType?: ExternalsType;
/**
* Ignore specific warnings.
*/
ignoreWarnings?: IgnoreWarnings;
/**
* Options for infrastructure level logging.
*/
@ -2548,6 +2583,10 @@ export interface WebpackOptionsNormalized {
* Specifies the default type of externals ('amd*', 'umd*', 'system' and 'jsonp' depend on output.libraryTarget set to the same value).
*/
externalsType?: ExternalsType;
/**
* Ignore specific warnings.
*/
ignoreWarnings?: IgnoreWarningsNormalized;
/**
* Options for infrastructure level logging.
*/

View File

@ -0,0 +1,39 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
/** @typedef {import("../declarations/WebpackOptions").IgnoreWarningsNormalized} IgnoreWarningsNormalized */
/** @typedef {import("./Compiler")} Compiler */
class IgnoreWarningsPlugin {
/**
* @param {IgnoreWarningsNormalized} ignoreWarnings conditions to ignore warnings
*/
constructor(ignoreWarnings) {
this._ignoreWarnings = ignoreWarnings;
}
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.compilation.tap("IgnoreWarningsPlugin", compilation => {
compilation.hooks.processWarnings.tap(
"IgnoreWarningsPlugin",
warnings => {
return warnings.filter(warning => {
return !this._ignoreWarnings.some(ignore =>
ignore(warning, compilation)
);
});
}
);
});
}
}
module.exports = IgnoreWarningsPlugin;

View File

@ -521,6 +521,11 @@ class WebpackOptionsApply extends OptionsApply {
}
new ResolverCachePlugin().apply(compiler);
if (options.ignoreWarnings && options.ignoreWarnings.length > 0) {
const IgnoreWarningsPlugin = require("./IgnoreWarningsPlugin");
new IgnoreWarningsPlugin(options.ignoreWarnings).apply(compiler);
}
compiler.hooks.afterPlugins.call(compiler);
if (!compiler.inputFileSystem) {
throw new Error("No input filesystem provided");

View File

@ -152,6 +152,31 @@ const getNormalizedWebpackOptions = config => {
externalsPresets => ({ ...externalsPresets })
),
externalsType: config.externalsType,
ignoreWarnings: config.ignoreWarnings
? config.ignoreWarnings.map(ignore => {
if (typeof ignore === "function") return ignore;
const i = ignore instanceof RegExp ? { message: ignore } : ignore;
return (warning, { requestShortener }) => {
if (!i.message && !i.module && !i.file) return false;
if (i.message && !i.message.test(warning.message)) {
return false;
}
if (
i.module &&
(!warning.module ||
!i.module.test(
warning.module.readableIdentifier(requestShortener)
))
) {
return false;
}
if (i.file && (!warning.file || !i.file.test(warning.file))) {
return false;
}
return true;
};
})
: undefined,
infrastructureLogging: nestedConfig(
config.infrastructureLogging,
infrastructureLogging => ({

View File

@ -5,6 +5,7 @@
"use strict";
const util = require("util");
const ModuleDependency = require("../dependencies/ModuleDependency");
const formatLocation = require("../formatLocation");
const { LogType } = require("../logging/Logger");
@ -1207,12 +1208,16 @@ const FILTER = {
/** @type {Record<string, Record<string, (thing: Object, context: UsualContext, options: UsualOptions) => boolean | undefined>>} */
const FILTER_RESULTS = {
"compilation.warnings": {
warningsFilter: (warning, context, { warningsFilter }) => {
const warningString = Object.keys(warning)
.map(key => `${warning[key]}`)
.join("\n");
return !warningsFilter.some(filter => filter(warning, warningString));
}
warningsFilter: util.deprecate(
(warning, context, { warningsFilter }) => {
const warningString = Object.keys(warning)
.map(key => `${warning[key]}`)
.join("\n");
return !warningsFilter.some(filter => filter(warning, warningString));
},
"config.stats.warningsFilter is deprecated in favor of config.ignoreWarnings",
"DEP_WEBPACK_STATS_WARNINGS_FILTER"
)
}
};

View File

@ -772,6 +772,55 @@
"type": "string",
"absolutePath": false
},
"IgnoreWarnings": {
"description": "Ignore specific warnings.",
"type": "array",
"items": {
"description": "Ignore specific warnings.",
"anyOf": [
{
"description": "A RegExp to select the warning message.",
"instanceof": "RegExp",
"tsType": "RegExp"
},
{
"type": "object",
"additionalProperties": false,
"properties": {
"file": {
"description": "A RegExp to select the origin file for the warning.",
"instanceof": "RegExp",
"tsType": "RegExp"
},
"message": {
"description": "A RegExp to select the warning message.",
"instanceof": "RegExp",
"tsType": "RegExp"
},
"module": {
"description": "A RegExp to select the origin module for the warning.",
"instanceof": "RegExp",
"tsType": "RegExp"
}
}
},
{
"description": "A custom function to select warnings based on the raw warning instance.",
"instanceof": "Function",
"tsType": "((warning: import('../lib/WebpackError'), compilation: import('../lib/Compilation')) => boolean)"
}
]
}
},
"IgnoreWarningsNormalized": {
"description": "Ignore specific warnings.",
"type": "array",
"items": {
"description": "A function to select warnings based on the raw warning instance.",
"instanceof": "Function",
"tsType": "((warning: import('../lib/WebpackError'), compilation: import('../lib/Compilation')) => boolean)"
}
},
"Iife": {
"description": "Wrap javascript code into IIFE's to avoid leaking into global scope.",
"type": "boolean"
@ -3614,6 +3663,9 @@
"externalsType": {
"$ref": "#/definitions/ExternalsType"
},
"ignoreWarnings": {
"$ref": "#/definitions/IgnoreWarningsNormalized"
},
"infrastructureLogging": {
"$ref": "#/definitions/InfrastructureLogging"
},
@ -3756,6 +3808,9 @@
"externalsType": {
"$ref": "#/definitions/ExternalsType"
},
"ignoreWarnings": {
"$ref": "#/definitions/IgnoreWarnings"
},
"infrastructureLogging": {
"$ref": "#/definitions/InfrastructureLogging"
},

View File

@ -105,6 +105,7 @@ describe("Defaults", () => {
"web": true,
},
"externalsType": "var",
"ignoreWarnings": undefined,
"infrastructureLogging": Object {
"debug": false,
"level": "info",

View File

@ -24,7 +24,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 should be an object:
object { amd?, bail?, cache?, context?, dependencies?, devServer?, devtool?, entry?, experiments?, externals?, externalsPresets?, externalsType?, infrastructureLogging?, loader?, mode?, module?, name?, node?, optimization?, output?, parallelism?, performance?, plugins?, profile?, recordsInputPath?, recordsOutputPath?, recordsPath?, resolve?, resolveLoader?, snapshot?, stats?, target?, watch?, watchOptions? }
object { amd?, bail?, cache?, context?, dependencies?, devServer?, devtool?, entry?, experiments?, externals?, externalsPresets?, externalsType?, ignoreWarnings?, infrastructureLogging?, loader?, mode?, module?, name?, node?, optimization?, output?, parallelism?, performance?, plugins?, profile?, recordsInputPath?, recordsOutputPath?, recordsPath?, resolve?, resolveLoader?, snapshot?, stats?, target?, watch?, watchOptions? }
-> Options object as provided by the user."
`)
);
@ -33,7 +33,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 should be an object:
object { amd?, bail?, cache?, context?, dependencies?, devServer?, devtool?, entry?, experiments?, externals?, externalsPresets?, externalsType?, infrastructureLogging?, loader?, mode?, module?, name?, node?, optimization?, output?, parallelism?, performance?, plugins?, profile?, recordsInputPath?, recordsOutputPath?, recordsPath?, resolve?, resolveLoader?, snapshot?, stats?, target?, watch?, watchOptions? }
object { amd?, bail?, cache?, context?, dependencies?, devServer?, devtool?, entry?, experiments?, externals?, externalsPresets?, externalsType?, ignoreWarnings?, infrastructureLogging?, loader?, mode?, module?, name?, node?, optimization?, output?, parallelism?, performance?, plugins?, profile?, recordsInputPath?, recordsOutputPath?, recordsPath?, resolve?, resolveLoader?, snapshot?, stats?, target?, watch?, watchOptions? }
-> Options object as provided by the user."
`)
);
@ -196,7 +196,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 has an unknown property 'postcss'. These properties are valid:
object { amd?, bail?, cache?, context?, dependencies?, devServer?, devtool?, entry?, experiments?, externals?, externalsPresets?, externalsType?, infrastructureLogging?, loader?, mode?, module?, name?, node?, optimization?, output?, parallelism?, performance?, plugins?, profile?, recordsInputPath?, recordsOutputPath?, recordsPath?, resolve?, resolveLoader?, snapshot?, stats?, target?, watch?, watchOptions? }
object { amd?, bail?, cache?, context?, dependencies?, devServer?, devtool?, entry?, experiments?, externals?, externalsPresets?, externalsType?, ignoreWarnings?, infrastructureLogging?, loader?, mode?, module?, name?, node?, optimization?, output?, parallelism?, performance?, plugins?, profile?, recordsInputPath?, recordsOutputPath?, recordsPath?, resolve?, resolveLoader?, snapshot?, stats?, target?, watch?, watchOptions? }
-> Options object as provided by the user.
For typos: please correct them.
For loader options: webpack >= v2.0.0 no longer allows custom properties in configuration.
@ -426,7 +426,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 has an unknown property 'debug'. These properties are valid:
object { amd?, bail?, cache?, context?, dependencies?, devServer?, devtool?, entry?, experiments?, externals?, externalsPresets?, externalsType?, infrastructureLogging?, loader?, mode?, module?, name?, node?, optimization?, output?, parallelism?, performance?, plugins?, profile?, recordsInputPath?, recordsOutputPath?, recordsPath?, resolve?, resolveLoader?, snapshot?, stats?, target?, watch?, watchOptions? }
object { amd?, bail?, cache?, context?, dependencies?, devServer?, devtool?, entry?, experiments?, externals?, externalsPresets?, externalsType?, ignoreWarnings?, infrastructureLogging?, loader?, mode?, module?, name?, node?, optimization?, output?, parallelism?, performance?, plugins?, profile?, recordsInputPath?, recordsOutputPath?, recordsPath?, resolve?, resolveLoader?, snapshot?, stats?, target?, watch?, watchOptions? }
-> Options object as provided by the user.
The 'debug' property was removed in webpack 2.0.0.
Loaders should be updated to allow passing this option via loader options in module.rules.
@ -482,7 +482,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[1] should be an object:
object { amd?, bail?, cache?, context?, dependencies?, devServer?, devtool?, entry?, experiments?, externals?, externalsPresets?, externalsType?, infrastructureLogging?, loader?, mode?, module?, name?, node?, optimization?, output?, parallelism?, performance?, plugins?, profile?, recordsInputPath?, recordsOutputPath?, recordsPath?, resolve?, resolveLoader?, snapshot?, stats?, target?, watch?, watchOptions? }
object { amd?, bail?, cache?, context?, dependencies?, devServer?, devtool?, entry?, experiments?, externals?, externalsPresets?, externalsType?, ignoreWarnings?, infrastructureLogging?, loader?, mode?, module?, name?, node?, optimization?, output?, parallelism?, performance?, plugins?, profile?, recordsInputPath?, recordsOutputPath?, recordsPath?, resolve?, resolveLoader?, snapshot?, stats?, target?, watch?, watchOptions? }
-> Options object as provided by the user."
`)
);
@ -568,7 +568,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 has an unknown property 'rules'. These properties are valid:
object { amd?, bail?, cache?, context?, dependencies?, devServer?, devtool?, entry?, experiments?, externals?, externalsPresets?, externalsType?, infrastructureLogging?, loader?, mode?, module?, name?, node?, optimization?, output?, parallelism?, performance?, plugins?, profile?, recordsInputPath?, recordsOutputPath?, recordsPath?, resolve?, resolveLoader?, snapshot?, stats?, target?, watch?, watchOptions? }
object { amd?, bail?, cache?, context?, dependencies?, devServer?, devtool?, entry?, experiments?, externals?, externalsPresets?, externalsType?, ignoreWarnings?, infrastructureLogging?, loader?, mode?, module?, name?, node?, optimization?, output?, parallelism?, performance?, plugins?, profile?, recordsInputPath?, recordsOutputPath?, recordsPath?, resolve?, resolveLoader?, snapshot?, stats?, target?, watch?, watchOptions? }
-> Options object as provided by the user.
Did you mean module.rules?"
`)
@ -582,7 +582,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 has an unknown property 'splitChunks'. These properties are valid:
object { amd?, bail?, cache?, context?, dependencies?, devServer?, devtool?, entry?, experiments?, externals?, externalsPresets?, externalsType?, infrastructureLogging?, loader?, mode?, module?, name?, node?, optimization?, output?, parallelism?, performance?, plugins?, profile?, recordsInputPath?, recordsOutputPath?, recordsPath?, resolve?, resolveLoader?, snapshot?, stats?, target?, watch?, watchOptions? }
object { amd?, bail?, cache?, context?, dependencies?, devServer?, devtool?, entry?, experiments?, externals?, externalsPresets?, externalsType?, ignoreWarnings?, infrastructureLogging?, loader?, mode?, module?, name?, node?, optimization?, output?, parallelism?, performance?, plugins?, profile?, recordsInputPath?, recordsOutputPath?, recordsPath?, resolve?, resolveLoader?, snapshot?, stats?, target?, watch?, watchOptions? }
-> Options object as provided by the user.
Did you mean optimization.splitChunks?"
`)
@ -596,7 +596,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 has an unknown property 'noParse'. These properties are valid:
object { amd?, bail?, cache?, context?, dependencies?, devServer?, devtool?, entry?, experiments?, externals?, externalsPresets?, externalsType?, infrastructureLogging?, loader?, mode?, module?, name?, node?, optimization?, output?, parallelism?, performance?, plugins?, profile?, recordsInputPath?, recordsOutputPath?, recordsPath?, resolve?, resolveLoader?, snapshot?, stats?, target?, watch?, watchOptions? }
object { amd?, bail?, cache?, context?, dependencies?, devServer?, devtool?, entry?, experiments?, externals?, externalsPresets?, externalsType?, ignoreWarnings?, infrastructureLogging?, loader?, mode?, module?, name?, node?, optimization?, output?, parallelism?, performance?, plugins?, profile?, recordsInputPath?, recordsOutputPath?, recordsPath?, resolve?, resolveLoader?, snapshot?, stats?, target?, watch?, watchOptions? }
-> Options object as provided by the user.
Did you mean module.noParse?"
`)

View File

@ -563,6 +563,71 @@ Object {
"multiple": false,
"simpleType": "string",
},
"ignore-warnings": Object {
"configs": Array [
Object {
"description": "A RegExp to select the warning message.",
"multiple": true,
"path": "ignoreWarnings[]",
"type": "RegExp",
},
],
"description": "A RegExp to select the warning message.",
"multiple": true,
"simpleType": "string",
},
"ignore-warnings-file": Object {
"configs": Array [
Object {
"description": "A RegExp to select the origin file for the warning.",
"multiple": true,
"path": "ignoreWarnings[].file",
"type": "RegExp",
},
],
"description": "A RegExp to select the origin file for the warning.",
"multiple": true,
"simpleType": "string",
},
"ignore-warnings-message": Object {
"configs": Array [
Object {
"description": "A RegExp to select the warning message.",
"multiple": true,
"path": "ignoreWarnings[].message",
"type": "RegExp",
},
],
"description": "A RegExp to select the warning message.",
"multiple": true,
"simpleType": "string",
},
"ignore-warnings-module": Object {
"configs": Array [
Object {
"description": "A RegExp to select the origin module for the warning.",
"multiple": true,
"path": "ignoreWarnings[].module",
"type": "RegExp",
},
],
"description": "A RegExp to select the origin module for the warning.",
"multiple": true,
"simpleType": "string",
},
"ignore-warnings-reset": Object {
"configs": Array [
Object {
"description": "Clear all items provided in configuration. Ignore specific warnings.",
"multiple": false,
"path": "ignoreWarnings",
"type": "reset",
},
],
"description": "Clear all items provided in configuration. Ignore specific warnings.",
"multiple": false,
"simpleType": "boolean",
},
"infrastructure-logging-debug": Object {
"configs": Array [
Object {

View File

@ -886,6 +886,22 @@ chunk trees.js (trees) 71 bytes [rendered]
./trees/3.js 14 bytes [built] [code generated]"
`;
exports[`StatsTestCases should print correct stats for ignore-warnings 1`] = `
"asset main.js 943 bytes [emitted] (name: main)
orphan modules 8.53 KiB [orphan] 9 modules
./index.js + 9 modules 8.7 KiB [built] [code generated]
WARNING in ./module.js?4 3:12-20
Should not import the named export 'homepage' (imported as 'homepage') from default-exporting module (only default export is available soon)
@ ./index.js 4:0-20
WARNING in ./module2.js?1 3:12-16
Should not import the named export 'name' (imported as 'name') from default-exporting module (only default export is available soon)
@ ./index.js 6:0-21
webpack x.x.x compiled with 2 warnings in X ms"
`;
exports[`StatsTestCases should print correct stats for immutable 1`] = `
"asset c30341ca2ed860460ce7.js 12.5 KiB [emitted] [immutable] (name: main)
asset b815a02217b4cae51059.js 884 bytes [emitted] [immutable]"

View File

@ -0,0 +1,9 @@
import "./module?1";
import "./module?2";
import "./module?3";
import "./module?4";
import "./module2?1";
import "./module2?2";
import "./module2?3";
import "./module2?4";

View File

@ -0,0 +1,3 @@
import { homepage } from "../../../package.json";
console.log(homepage);

View File

@ -0,0 +1,3 @@
import { name } from "../../../package.json";
console.log(name);

View File

@ -0,0 +1,17 @@
/** @type {import("../../../").Configuration} */
module.exports = {
entry: "./index.js",
ignoreWarnings: [
{
module: /module2\.js\?[34]/
},
{
module: /[13]/,
message: /homepage/
},
/The 'mode' option has not been set/,
warning => {
return warning.module.identifier().endsWith("?2");
}
]
};

30
types.d.ts vendored
View File

@ -1803,6 +1803,28 @@ declare interface Configuration {
*/
externalsType?: ExternalsType;
/**
* Ignore specific warnings.
*/
ignoreWarnings?: (
| RegExp
| {
/**
* A RegExp to select the origin file for the warning.
*/
file?: RegExp;
/**
* A RegExp to select the warning message.
*/
message?: RegExp;
/**
* A RegExp to select the origin module for the warning.
*/
module?: RegExp;
}
| ((warning: WebpackError, compilation: Compilation) => boolean)
)[];
/**
* Options for infrastructure level logging.
*/
@ -9499,6 +9521,14 @@ declare interface WebpackOptionsNormalized {
*/
externalsType?: ExternalsType;
/**
* Ignore specific warnings.
*/
ignoreWarnings?: ((
warning: WebpackError,
compilation: Compilation
) => boolean)[];
/**
* Options for infrastructure level logging.
*/