1.0.0 • Published 10 months ago

babel-preset-production-console-filter v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
10 months ago

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 to production.
  • 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

  1. Production build without console logs:

    NODE_ENV=production npm run build

    In this case, the preset removes all console.* statements.

  2. 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 the RETAIN_CONSOLE_LOGS environment variable.

  3. Development build:

    npm run dev

    When NODE_ENV is not set to production, console logs are always retained, regardless of the RETAIN_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.