diff --git a/packages/meteor/deprecate.js b/packages/meteor/deprecate.js index 1e213ceeab..a21e641b94 100644 --- a/packages/meteor/deprecate.js +++ b/packages/meteor/deprecate.js @@ -1,3 +1,26 @@ +if (Meteor.isServer && Meteor.isDevelopment) { + if (typeof __meteor_runtime_config__ === 'object') { + var noDeprecation = process.env.METEOR_NO_DEPRECATION || process.noDeprecation; + if (noDeprecation === 'true' || noDeprecation === 'false') { + noDeprecation = noDeprecation === 'true'; + } + __meteor_runtime_config__.noDeprecation = noDeprecation; + } +} + +function oncePerArgument(func) { + var cache = new Map(); + + return function _oncePerArgument() { + var key = JSON.stringify(arguments); + if (!cache.has(key)) { + var result = func.apply(this, arguments); + cache.set(key, result); + } + return cache.get(key); + }; +} + function cleanStackTrace(stackTrace) { if (!stackTrace || typeof stackTrace !== 'string') return []; var lines = stackTrace.split('\n'); @@ -20,6 +43,14 @@ function cleanStackTrace(stackTrace) { return trace.join('\n'); } +var onceWarning = oncePerArgument(function _onceWarning(message) { + console.warn.apply(console, message); +}); + +function onceFixDeprecation() { + onceWarning(['Deprecation warnings are hidden but crucial to address for future Meteor updates.', '\n', 'Remove the `METEOR_NO_DEPRECATION` env var to reveal them, then report or fix the issues.']); +} + Meteor.deprecate = function () { if (!Meteor.isDevelopment) { return; @@ -28,10 +59,21 @@ Meteor.deprecate = function () { var stackStrace = cleanStackTrace(new Error().stack || ''); var messages = Array.prototype.slice.call(arguments); // Convert arguments to array + if (typeof __meteor_runtime_config__.noDeprecation === 'string') { + var noDeprecationPattern = new RegExp(__meteor_runtime_config__.noDeprecation); + if (noDeprecationPattern.test(stackStrace)) { + onceFixDeprecation(); + return; + } + } else if (typeof __meteor_runtime_config__.noDeprecation === 'boolean' && __meteor_runtime_config__.noDeprecation) { + onceFixDeprecation(); + return; + } if (stackStrace.length > 0) { messages.push('\n\n', 'Trace:', '\n', stackStrace); } + messages.push('\n\n', 'To disable warnings, set the `METEOR_NO_DEPRECATION` to `true` or a regex pattern.', '\n'); - console.warn.apply(console, ['[DEPRECATION]'].concat(messages)); + onceWarning(['[DEPRECATION]'].concat(messages)); } };