babel-preset-production-console-filter v1.0.0
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-devor
yarn add babel-preset-production-console-filter --devUsage
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_ENVenvironment variable is set toproduction. - The
RETAIN_CONSOLE_LOGSenvironment 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 buildIn this case, the preset removes all
console.*statements.Production build retaining console logs:
NODE_ENV=production RETAIN_CONSOLE_LOGS=true npm run buildHere, the preset does not remove any
console.*statements due to theRETAIN_CONSOLE_LOGSenvironment variable.Development build:
npm run devWhen
NODE_ENVis not set toproduction, console logs are always retained, regardless of theRETAIN_CONSOLE_LOGSsetting.
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.
1 year ago