babel-preset-production-console-filter
v1.0.0
Published
A Babel preset to conditionally remove console logs in production builds.
Downloads
9
Maintainers
Readme
Here’s a sample README.md
file for your custom Babel preset:
babel-preset-production-console-filter
A simple Babel preset that conditionally removes console.*
statements in production builds, with the option to retain console logs using an environment variable.
Installation
You can install this preset using npm or yarn:
npm install babel-preset-production-console-filter --save-dev
or
yarn add babel-preset-production-console-filter --dev
Usage
To use the preset in your project, add it to your Babel configuration file (babel.config.js
or .babelrc.js
):
babel.config.js
Example
module.exports = {
presets: [
'@babel/preset-env',
'production-console-filter',
],
};
How It Works
This preset will automatically add the babel-plugin-transform-remove-console
plugin to your Babel configuration only when the following conditions are met:
- The
NODE_ENV
environment variable is set toproduction
. - The
RETAIN_CONSOLE_LOGS
environment variable is not set to'true'
.
If both conditions are true, console.*
statements will be removed from the output. Otherwise, the logs will be retained.
Example Scenarios
Production build without console logs:
NODE_ENV=production npm run build
In this case, the preset removes all
console.*
statements.Production build retaining console logs:
NODE_ENV=production RETAIN_CONSOLE_LOGS=true npm run build
Here, the preset does not remove any
console.*
statements due to theRETAIN_CONSOLE_LOGS
environment variable.Development build:
npm run dev
When
NODE_ENV
is not set toproduction
, console logs are always retained, regardless of theRETAIN_CONSOLE_LOGS
setting.
Environment Variables
NODE_ENV
: Set this to'production'
to enable the preset's removal of console logs.RETAIN_CONSOLE_LOGS
: Set this to'true'
to retain console logs in a production build.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Author
Ian Baker
Github
--- provides clear installation instructions, usage examples, and explains the preset's behavior, making it easy for others to use and understand.