How can remove console.log in the production build of a React application created using create-react-app?

后端 未结 3 1584
难免孤独
难免孤独 2021-02-14 03:46

How can remove console.log in the production build of a React application created using create-react-app CRA?

3条回答
  •  暖寄归人
    2021-02-14 04:13

    The simple way is to create a new file in root named logger.js

    In logger.js

    var logger = function logger(...messages){
        if(process.env.NODE_ENV === 'development'){
            console.log(messages);
        }
    }
    exports.logger = logger;
    

    Now import this logger.js file to your components. For example my component file is demo.jsx

    import React from 'react';
    var logger = require('../logger.js').logger;
    
    class Demo extends React.Component{
        constructor(props){
        }
    
        render(){
            return(
                

    Hello i am demo component

    ); } componentDidMount(){ logger('I am printing only in developmet') } } export default Demo;

提交回复
热议问题