Skip to content

🌈一个生产环境删除console,开发环境扩展console支持打印颜色和代码位置的babel插件

License

Notifications You must be signed in to change notification settings

lingxiaoguang/babel-plugin-console-transform

Repository files navigation

console-transform

🌈一个生产环境删除console,开发环境扩展console支持打印颜色和代码位置的babel插件

🌰源码:

🌰生产环境:

🌰非生产环境:

📽️效果:

特性 😀

❌ 生产环境删除所有console,也可以通过glob字符串或者函数来自定义删除逻辑

🌈 非生产环境可以扩展console方法,默认提供了几种console方法,green、blue、orange、red、bgGreen、bgOrange、bgBlue、bgRed

🏷️ 扩展的方法会自动添加代码文件和行列数,如 /src/scripts/inde.js (10:5)

安装 😙

npm install --save-dev babel-plugin-console-transform

or

yarn add --dev babel-plugin-console-transform

使用 🤔

在.babelrc.js引入插件并配置

const consoleTransformPlugin = require('babel-plugin-console-transform');

module.exports = {
    plugins: [
        [
            consoleTransformPlugin,
            {
                env: 'production',
                removeMethods: ["*g*", (args) => args.includes('xxxx')],
                additionalStyleMethods: {
                    'success': 'padding:10px; color:#fff;background:green;',
                    'danger': 'padding:20px; background:red;font-size:30px; color:#fff;'
                }
            }
        ]
    ]
}

配置项 😎

配置项名称 是否必须 含义 类型 举例
env 运行环境 string production, development
removeMethods production下删除的方法,默认删除全部 Array<string | Function> ['log', 'warn'], [(args) => args.includes('dont remove'), 'log']
additionalStyleMethods 额外添加的console方法 {[key: string]: string} {'success': 'background: green;'}

其他说明::unamused:

🍅 env 配置时可以使用 process.env.NODE_ENV 指定

🥝 removeMethods 在 env 为 production 时才会生效,支持 glob模式 的字符串,比如 \*\*g**, **bg{Red,Green} 等,也支持函数,参数为console方法的参数,用于一些需要根据参数确定是否删除console的场景。

🍑 additionalStyleMethods可以扩展一些方法,而且可以覆盖原生的log等方法

🍇 所有扩展的方法都会额外打印文件路径和代码所在行列数

相关文章

如何用 typescript 写一个处理 console 的 babel 插件

贡献 ❤️

欢迎任何形式的贡献。你可以用 pull requestsissues 的方式提交任何想法。

License

MIT. Copyright (c) 2019 lingxiaoguang

About

🌈一个生产环境删除console,开发环境扩展console支持打印颜色和代码位置的babel插件

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published