在React Native中记录当前目录/文件名
Logging Current Directory/Filename in React Native
我想知道是否有一种方法可以在React Native中记录当前文件名或目录。类似于NodeJS如何使用__filename
和__dirname
。
不,这是不可能的,因为所有的源代码都被捆绑在一个大文件中,然后提供了一个源映射,以便您可以在原始结构中调试它
您可以通过在代码中的某个位置调用以下代码片段来看到这一点:
console.log(new Error().stack);
在常规javascript中,你会得到带有文件和行号的整个跟踪,但在react native中,你得到的是:
Error
at Login (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:83009:9)
at instantiate (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:5712:18)
at new Login (eval at proxyClass (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:5730:16), <anonymous>:4:17)
at ReactCompositeComponentWrapper._constructComponentWithoutOwner (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:22051:19)
at ReactCompositeComponentWrapper._constructComponent (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:22033:13)
at ReactCompositeComponentWrapper.mountComponent (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:21952:15)
at Object.mountComponent (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:20425:29)
at ReactCompositeComponentWrapper.performInitialMount (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:22134:28)
at ReactCompositeComponentWrapper.mountComponent (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:22015:13)
at Object.mountComponent (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:20425:29)"
它显示真实的文件不见了。
您可以将以下babel的插件安装到您的包中:
$ npm i babel-plugin-transform-line
$ npm i babel-plugin-transform-dirname-filename
其中:
- babel-plugin转换行将
__line
重写为当前源行号静态值 - babel-plugin-transform-dirname filename将
__filename
和__dirname
重写为当前源文件名和目录
然后将插件添加到您的babel.config.js
:中
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: ['babel-plugin-transform-dirname-filename', 'babel-plugin-transform-line']
};
现在您可以使用代码中的变量:
console.log(__filename, __dirname, __line);
// Prints: /Users/user/TestApp/App.js /Users/user/TestApp 62
相关文章:
- 正在获取客户端计算机上特定目录中的文件名
- 使用Javascript读取当前目录的文件名
- 在node.js中,如何引用当前目录下的目录
- Firefox的问题是列出当前目录中的所有文件名
- 从流播放器输出当前剪辑文件名或 URL
- 如何在使用 ng-CSV 导出时将当前时间和日期提供给文件名
- 从远程服务器目录获取文件名列表
- 使用 Node js 显示目录中的文件名
- 通过输入文件名获取目录路径
- 如何使用 Gulp 在文件名前面加上它的父目录
- 如何防止 Ace 在当前目录中查找主题和模式
- 如何使用当前目录中的文件
- 404重定向工作在当前目录
- Javascript从当前目录读取文本文件
- 如何使用vbscript在usb驱动器的当前目录上创建快捷方式
- 预安装脚本如何设置NPM配置到当前目录
- 检查当前目录是否包含package.json和who's的名称等于值
- 在React Native中记录当前目录/文件名
- 具有父目录和当前文件名的动态href
- 获取当前目录中文件夹/文件的列表