Skip to content

Commit a696359

Browse files
tmontbjornstar
authored andcommitted
suppress experimental warnings on node 18+
fixes #293
1 parent d791afa commit a696359

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

lib/suppress-experimental-warnings.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Source: https://github.com/nodejs/node/issues/30810#issue-533506790
22

33
module.exports = p => {
4-
const { emitWarning } = p;
4+
const { emitWarning, emit } = p;
55

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

1515
return emitWarning(warning, ...args);
1616
};
17+
18+
const hasMessage = (obj, message) => {
19+
return (
20+
obj &&
21+
typeof obj === 'object' &&
22+
typeof obj.message === 'string' &&
23+
obj.message.includes(message)
24+
);
25+
};
26+
27+
// prevent experimental warning message spam on node 18+
28+
// adapted from https://github.com/yarnpkg/berry/blob/f19f67ef26f004a0b245c81a36158afc45a70504/packages/yarnpkg-pnp/sources/loader/applyPatch.ts#L414-L435
29+
p.emit = (name, data, ...args) => {
30+
if (name === 'warning' && hasMessage(data, 'Custom ESM Loaders is an experimental feature')) {
31+
return false;
32+
}
33+
34+
return emit.call(p, name, data, ...args);
35+
};
1736
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const tap = require('tap');
2+
const { spawn } = require('../utils');
3+
4+
tap.test('Should suppress experimental warning spam', t => {
5+
spawn('env.js', out => {
6+
if (out.match(/ExperimentalWarning/)) return t.fail('Should not log an ExperimentalWarning');
7+
8+
return {
9+
exit: t.end.bind(t)
10+
};
11+
});
12+
});

0 commit comments

Comments
 (0)