Reactjs react CRA-从生成中排除调试代码
是否有任何方法可以在生产模式下从if语句中完全排除某些代码 我试着做这样的事情:Reactjs react CRA-从生成中排除调试代码,reactjs,optimization,production-environment,dead-code,react-loadable,Reactjs,Optimization,Production Environment,Dead Code,React Loadable,是否有任何方法可以在生产模式下从if语句中完全排除某些代码 我试着做这样的事情: import React from "react"; import Loadable from 'react-loadable'; function Loading() { return <div></div>; } let LoadableDebugBar; if (process.env.NODE_ENV !== 'production') { LoadableDebug
import React from "react";
import Loadable from 'react-loadable';
function Loading() {
return <div></div>;
}
let LoadableDebugBar;
if (process.env.NODE_ENV !== 'production') {
LoadableDebug = Loadable({
loader: () => import('./Debug'),
loading: Loading
});
} else{
LoadableDebug = Loadable({
loader: () => import('./BlankComponent'),
loading: Loading
});
}
export default LoadableDebug;
从“React”导入React;
从“react Loadable”导入Loadable;
函数加载(){
返回;
}
让LoadableDebugBar;
if(process.env.NODE_env!=“生产”){
LoadableDebug=可加载({
加载程序:()=>导入('./Debug'),
装货:装货
});
}否则{
LoadableDebug=可加载({
加载程序:()=>导入('./BlankComponent'),
装货:装货
});
}
导出默认LoadableDebug;
问题是,在构建应用程序时,您仍然可以看到来自调试组件的源代码。我们的想法是将其完全剥离,因为在生产模式下,该组件从未使用过
即使我懒散地加载,它仍然不能满足我,因为我想完全摆脱它,这样我就可以隐藏所有调试功能。用这种方法解决了这个问题:在我的构建代码中,不再有调试组件
import React, { Component } from 'react';
let DebugBarContainer;
if (process.env.NODE_ENV !== 'production') {
DebugBarContainer = require('../../containers/DebugBarContainer').default;
} else {
DebugBarContainer = () => null;
}
class App extends Component {
render() {
return (
<DebugBarContainer />
</div>
);
}
}
export default App;
import React,{Component}来自'React';
让我们用一个容器;
if(process.env.NODE_env!=“生产”){
DebugBarContainer=require('../../containers/DebugBarContainer')。默认值;
}否则{
DebugBarContainer=()=>null;
}
类应用程序扩展组件{
render(){
返回(
);
}
}
导出默认应用程序;