简体   繁体   English

在 npm 或 webpack 中分离开发和生产文件

[英]Separate Develop and Production files in npm or webpack

I have seen in react native project, we use index.android.js and index.ios.js to separate index.js file for android and ios.我在 react native 项目中看到,我们使用index.android.jsindex.ios.js来分隔 android 和 ios 的index.js文件。

Now is it possible for develop and production environment to separate their files ??现在develop环境和production环境可以分开文件吗?? I mean does webpack or a plugin or maybe node have any ability to separate them like index.develop.js and index.js (for production) and require it like require('index') ?我的意思是webpack或插件或node是否有能力将它们分开,如index.develop.jsindex.js (用于生产)并像require('index')一样require('index')它?

Yes sure, using process.env.NODE_ENV you can dynamically require() :是的,使用process.env.NODE_ENV你可以动态地require()

  const module = process.env.NODE_ENV === "production" 
    ? require("./production")
     : require("./development");

Then if production mode is on, the comparison will be inlined, and the require("./development") will be optimized away.然后,如果生产模式打开,比较将被内联,并且require("./development")将被优化掉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM