DEV: Refactor deprecation silencer (#22526)

- Unify the silencing methods, use a WeakMap to remember the seen objects
- Export a proper plugin and use the absolute path in the config, instead
  of the proprietary config from `broccoli-babel-transpiler`

The latter causes problems in Embroider which doesn't use the broccoli
based babel pipeline.
This commit is contained in:
Godfrey Chan 2023-07-11 09:01:51 -07:00 committed by GitHub
parent bdb9ee8507
commit cfa2f1fea8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 41 deletions

View File

@ -17,9 +17,8 @@ module.exports = function (defaults) {
const vendorJs = discourseRoot + "/vendor/assets/javascripts/"; const vendorJs = discourseRoot + "/vendor/assets/javascripts/";
// Silence deprecations which we are aware of - see `lib/deprecation-silencer.js` // Silence deprecations which we are aware of - see `lib/deprecation-silencer.js`
const ui = defaults.project.ui; DeprecationSilencer.silence(console, "warn");
DeprecationSilencer.silenceUiWarn(ui); DeprecationSilencer.silence(defaults.project.ui, "writeWarnLine");
DeprecationSilencer.silenceConsoleWarn();
const isProduction = EmberApp.env().includes("production"); const isProduction = EmberApp.env().includes("production");
const isTest = EmberApp.env().includes("test"); const isTest = EmberApp.env().includes("test");
@ -95,7 +94,7 @@ module.exports = function (defaults) {
}, },
babel: { babel: {
plugins: [DeprecationSilencer.generateBabelPlugin()], plugins: [require.resolve("./lib/deprecation-silencer")],
}, },
// We need to build tests in prod for theme tests // We need to build tests in prod for theme tests

View File

@ -5,52 +5,57 @@ const SILENCED_WARN_PREFIXES = [
"DEPRECATION: Invoking the `<LinkTo>` component with positional arguments is deprecated", "DEPRECATION: Invoking the `<LinkTo>` component with positional arguments is deprecated",
]; ];
let consoleWarnSilenced = false; class DeprecationSilencer {
constructor() {
module.exports = class DeprecationSilencer { this.silenced = new WeakMap();
static silenceUiWarn(ui) {
const oldWriteWarning = ui.writeWarnLine.bind(ui);
ui.writeWarnLine = (message, ...args) => {
if (
!SILENCED_WARN_PREFIXES.some((prefix) => message.startsWith(prefix))
) {
return oldWriteWarning(message, ...args);
}
};
} }
static silenceConsoleWarn() { silence(object, method) {
if (consoleWarnSilenced) { if (this.alreadySilenced(object, method)) {
return; return;
} }
/* eslint-disable no-console */
const oldConsoleWarn = console.warn.bind(console); let original = object[method];
console.warn = (message, ...args) => {
if ( object[method] = (message, ...args) => {
!SILENCED_WARN_PREFIXES.some((prefix) => message.startsWith(prefix)) if (!this.shouldSilence(message)) {
) { return original.call(object, message, ...args);
return oldConsoleWarn(message, ...args);
} }
}; };
/* eslint-enable no-console */
consoleWarnSilenced = true;
} }
/** alreadySilenced(object, method) {
* Generates a dummy babel plugin which applies the console.warn silences in worker let set = this.silenced.get(object);
* processes. Does not actually affect babel output.
*/ if (!set) {
static generateBabelPlugin() { set = new Set();
return { this.silenced.set(object, set);
_parallelBabel: { }
requireFile: require.resolve("./deprecation-silencer"),
buildUsing: "babelShim", if (set.has(method)) {
}, return true;
}; } else {
set.add(method);
return false;
}
} }
static babelShim() { shouldSilence(message) {
DeprecationSilencer.silenceConsoleWarn(); return SILENCED_WARN_PREFIXES.some((prefix) => message.startsWith(prefix));
return {};
} }
}
const DEPRECATION_SILENCER = new DeprecationSilencer();
/**
* Export a dummy babel plugin which applies the console.warn silences in worker
* processes. Does not actually affect babel output.
*/
module.exports = function () {
DEPRECATION_SILENCER.silence(console, "warn");
return {};
};
module.exports.silence = function silence(...args) {
DEPRECATION_SILENCER.silence(...args);
}; };