Skip to content

Commit

Permalink
Enable distinction between production and development logs
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkelM committed May 24, 2024
1 parent c97f609 commit 328a222
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@

/* c8 ignore start - The console reroutings cannot be tested correctly */
// ---------- Console rerouting ----------
var oldLog = console.log;
const oldLog = console.log;
console.log = function () {
if (arguments[0] !== "[random-youtube-video]:") {
Array.prototype.unshift.call(arguments, '[random-youtube-video]:');
// The last argument to console.log is a boolean that determines if the message should be shown in production
const showInProduction = arguments[arguments.length - 1] === true;
// If we are either in development or the message should be shown in production, log the message
if (process.env.NODE_ENV !== 'production' || showInProduction) {
if (arguments[0] !== "[random-youtube-video]:") {
Array.prototype.unshift.call(arguments, '[random-youtube-video]:');
}
// Remove the showInProduction flag from the arguments so it doesn't get logged
if (typeof arguments[arguments.length - 1] === 'boolean') {
Array.prototype.pop.call(arguments);
}
oldLog.apply(this, arguments);
}
oldLog.apply(this, arguments);
}
/* c8 ignore stop */

Expand Down
1 change: 1 addition & 0 deletions webpack.dev.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = env => {
env.mode = mode;
env.devtool = devtool;
env.optimization = optimization;
env.NODE_ENV = mode;

return merge(common(env), {
mode,
Expand Down
1 change: 1 addition & 0 deletions webpack.prod.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const common = require('./webpack.common.cjs');
module.exports = env => {
let mode = "production";
env.mode = mode;
env.NODE_ENV = mode;

return merge(common(env), {
mode
Expand Down

0 comments on commit 328a222

Please sign in to comment.