Skip to content

Commit

Permalink
suppress experimental warnings on node 18+
Browse files Browse the repository at this point in the history
  • Loading branch information
tmont committed Jun 12, 2022
1 parent d791afa commit 5e2f053
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions bin/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('hello world');
21 changes: 20 additions & 1 deletion lib/suppress-experimental-warnings.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Source: https://github.com/nodejs/node/issues/30810#issue-533506790

module.exports = p => {
const { emitWarning } = p;
const { emitWarning, emit } = p;

p.emitWarning = (warning, ...args) => {
if (args[0] === 'ExperimentalWarning') {
Expand All @@ -14,4 +14,23 @@ module.exports = p => {

return emitWarning(warning, ...args);
};

const hasMessage = (obj, message) => {
return (
obj &&
typeof obj === 'object' &&
typeof obj.message === 'string' &&
obj.message.includes(message)
);
};

// prevent experimental warning message spam on node 18+
// adapted from https://github.com/yarnpkg/berry/blob/f19f67ef26f004a0b245c81a36158afc45a70504/packages/yarnpkg-pnp/sources/loader/applyPatch.ts#L414-L435
p.emit = (name, data, ...args) => {
if (name === 'warning' && hasMessage(data, 'Custom ESM Loaders is an experimental feature')) {
return false;
}

emit.call(p, name, data, ...args);
};
};
12 changes: 12 additions & 0 deletions test/spawn/experimental-warnings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const tap = require('tap');
const { spawn } = require('../utils');

tap.test('Should suppress experimental warning spam', t => {
spawn('env.js', out => {
if (out.match(/ExperimentalWarning/)) return t.fail('Should not log an ExperimentalWarning');

return {
exit: t.end.bind(t)
};
});
});

0 comments on commit 5e2f053

Please sign in to comment.