我有2个项目,一个用于React Native,另一个用于React。 来自React Web Project(例如rp1.js)的源文件正在使用来自React Native项目的源文件mycommonrn.js(component)。 问题是mycommonrn.js文件正在使用NPM模块(react-native-vector-icons)。我在两个单独安装了该模块:
(a)在React Native项目
(b)在React项目下的node_modules中。
当从React Web项目使用mycommonrn.js时,我需要从(b)中的node_modules中提取该NPM模块。 但这种情况并没有发生,而是以某种方式从(a)中撤出。
导致问题的import语句位于mycommonrn.js
import Ionicons from 'react-native-vector-icons/dist/Ionicons';
我正在尝试指示webpack babel-loader以 NOT 查看react-native项目中的node_modules ..但是它无效......
我的应用程序使用Create React App设置重新连接,以覆盖CRA强加的目录监狱。
我的配置如下,您可以看到我明确告诉babel-loader“排除”给我带来麻烦的node_modules目录(为了便于阅读,标有**)。
由于某些原因,明确排除不起作用。 (CRA设置不使用.babelrc - 这就是我不在帖子中发帖的原因。)
提前感谢任何帮助。
└─ 1
│ └─ oneOf
│ ├─ 0
│ │ ├─ test
│ │ │ ├─ 0
│ │ │ ├─ 1
│ │ │ ├─ 2
│ │ │ └─ 3
│ │ ├─ loader: /home/v/devel/mine/ltproject/mob/rnweb/aa1a/csr-cra/node_modules/url-loader/index.js
│ │ └─ options
│ │ ├─ limit: 10000
│ │ └─ name: static/media/[name].[hash:8].[ext]
│ ├─ 1
│ │ ├─ test
│ │ ├─ include
│ │ │ ├─ 0: /home/v/devel/mine/ltproject/mob/rnweb/aa1a/csr-cra/src
│ │ │ ├─ 1: /home/v/devel/mine/ltproject/mob/rn.common/src/js.app
│ │ │ ├─ 2: /home/v/devel/mine/ltproject/mob/rnweb/aa1a/app.src
│ │ │ ├─ 3: /home/v/devel/mine/ltproject/mob/rnweb/web.common/wc.src
│ │ │ └─ 4: /home/v/devel/mine/ltproject/mob/rnweb/aa1a/csr-cra/node_modules/react-native-vector-icons
│ │ ├─ loader: /home/v/devel/mine/ltproject/mob/rnweb/aa1a/csr-cra/node_modules/babel-loader/lib/index.js
│ │ ├─ options
│ │ │ ├─ babelrc: false
│ │ │ ├─ presets
│ │ │ │ └─ 0: /home/v/devel/mine/ltproject/mob/rnweb/aa1a/csr-cra/node_modules/babel-preset-react-app/index.js
│ │ │ └─ cacheDirectory: true
│ │ └─ **exclude: /home/v/devel/mine/ltproject/mob/rn.common/node_modules**
通过重新连接注入的config-overides.js(略有改变创建反应应用webpack)
/* adds and slightly overwrites
React Create App webpack
*/
const path = require('path');
const resolve=path.resolve;
const fs=require('fs');
const paths= require ('react-scripts/config/paths');
const wpmerge= require('webpack-merge');
var treeify = require('treeify');
module.exports = {
webpack: function(baseConfig, env) {
const config = Object.assign({}, baseConfig);
config.resolve.alias.web_common = path.resolve('./src/wc.src');
config.resolve.alias.app_src = path.resolve('./src/app.src');
config.resolve.alias.rnjs_common = path.resolve('./src/js.app');
/* get rid of facebooks plugin that causes error if something
outside of the src folder...
There SEEMS to be ONLY 1 plugin there,
so insteading of removing by index, I am resetting
array therefore there are should be NO resolve plugins
in my webpack config
*/
config.resolve.plugins=[];
//find existing js formater rule
let jsRulesJSFormaterIdx = -1;
jsRulesJSFormaterIdx=config.module.rules.findIndex(
(rule) =>{
if (rule.test // rule.test is a reg expr
&& rule.test.exec
&& rule.test.exec('./something.js')) {
return true;
}
}
);
let oneOfIdx=1;
let jsRulesBabelLoaderIdx = -1;
jsRulesBabelLoaderIdx=config.module.rules[oneOfIdx].oneOf.findIndex(
(rule) =>{
if (rule.test // rule.test is a reg expr
&& rule.test.exec
&& rule.test.exec('./something.js')) {
return true;
}
}
);
const pathsToMySourcesArr=
[paths.appSrc,
fs.realpathSync(paths.appSrc+'/js.app'),
fs.realpathSync(paths.appSrc+'/app.src'),
fs.realpathSync(paths.appSrc+'/wc.src'),
fs.realpathSync(paths.appNodeModules+'/react-native-vector-icons')
];
/* various contraptions on how to exclude a particular node module path
(I cannot get it work...)
the include part works
https://github.com/webpack/webpack/issues/2031
*/
console.log(paths);
let addtlIncludeConfig={
include:pathsToMySourcesArr,
/* EXCLUDE BELOW DOES NOT HELP TO EXCLUDE RN node_modules */
exclude: fs.realpathSync(paths.appPath+'../../../../rn.common/node_modules/')
};
config.module
.rules[oneOfIdx].oneOf[jsRulesBabelLoaderIdx]=
wpmerge( config.module.rules[oneOfIdx].oneOf[jsRulesBabelLoaderIdx],
addtlIncludeConfig);
config.module
.rules[jsRulesJSFormaterIdx]
.include=pathsToMySourcesArr;
console.log("config-overrides: the result config: ",
treeify.asTree(config,true,false));
return config;
},
}
}