React Native 如何运行打包.jsx文件

来源:互联网 发布:mac粉饼专柜价格表 编辑:程序博客网 时间:2024/06/09 10:44

最近在学习RN,发现项目里面的.jsx文件怎样都无法引用,后来网上搜索了一下,发现RN无法运行.jsx文件。在网上搜索到一位大神的博客,描述了如何让RN运行打包.jsx文件,copy一份作为记录,源博客链接:http://www.cnblogs.com/Grart/p/5033281.html

1.项目主文件夹\node_modules\react-native\packager\react-packager\src\Server\index.js找"var watchRootConfigs = opts.projectRoots.map(dir => {"这段,加上'.jsx'

var watchRootConfigs = opts.projectRoots.map(dir => {return {dir: dir,globs: ['**/*.js','**/*.json','**/*.jsx',].concat(assetGlobs),};});

2.项目主文件夹\node_modules\react-native\packager\react-packager\src\DependencyResolver\DependencyGraph\index

0.22版使用的是node-haste在\node_modules\react-native\node_modules\node-haste\lib\index

找"this._crawling = crawl(allRoots, {"加段,同样加上'jsx'

this._crawling = crawl(allRoots, {ignore: this._opts.ignoreFilePath,exts: ['js', 'jsx','json'].concat(this._opts.assetExts),fileWatcher: this._opts.fileWatcher,});

3.项目主文件夹\node_modules\react-native\packager\react-packager\src\DependencyResolver\DependencyGraph\ResolutionRequest.js

0.22版在\node_modules\react-native\node_modules\node-haste\lib\DependencyGraph
找"if (this._fastfs.fileExists(potentialModulePath)) {"改成

let file;let exts=["",    this._platform?('.' + this._platform + '.js'):null,    '.js',    '.json',    '.jsx'];for(let c=0;c<exts.length;c++){  if(null!=exts[c]        &&this._fastfs.fileExists(potentialModulePath + exts[c])        &&(file = potentialModulePath + exts[c]))      break;}if(!file){  throw new UnableToResolveError(    fromModule,    toModule,    `File ${potentialModulePath} doesnt exist`,  );}//以下为原来的代码//if (this._fastfs.fileExists(potentialModulePath)) {//  file = potentialModulePath;//} else if (this._platform != null &&//           this._fastfs.fileExists(potentialModulePath + '.' + this._platform + '.js')) {//  file = potentialModulePath + '.' + this._platform + '.js';//} else if (this._fastfs.fileExists(potentialModulePath + '.js')) {//  file = potentialModulePath + '.js';//} else if (this._fastfs.fileExists(potentialModulePath + '.json')) {//  file = potentialModulePath + '.json';//} else {//  throw new UnableToResolveError(//    fromModule,//    toModule,//    `File ${potentialModulePath} doesnt exist`,//  );//}


原创粉丝点击