简体   繁体   English

react-native 中的 Metro bundler 是什么?

[英]What is metro bundler in react-native?

I am learning React Native.我正在学习 React Native。

I can't find a proper documentation for metro bundler.我找不到 Metro bundler 的正确文档。 So, I have few questions on it.所以,我有几个问题。 As the name suggest it creates a bundle.顾名思义,它创建了一个包。

  1. But where is the bundle file located ?但是捆绑文件在哪里?
  2. Is this same as webpack ?这和 webpack 一样吗?
  3. What is the use of that bundle file ?那个包文件有什么用?

A React Native app is a compiled app that is running some Javascript. React Native 应用程序是运行一些 Javascript 的已编译应用程序。 Whenever you build and run your React Native project, a packager starts up called Metro.每当您构建和运行 React Native 项目时,都会启动一个名为 Metro 的打包程序。 You've probably seen this output in your terminal before, letting your know the packager is running.您之前可能已经在终端中看到过这个输出,让您知道打包程序正在运行。

The packager does a few things:打包器做了几件事:

Combines all your Javascript code into a single file, and translates any Javascript code that your device won't understand (like JSX or some of the newer JS syntax).将您所有的 Javascript 代码合并到一个文件中,并翻译您的设备无法理解的任何 Javascript 代码(如 JSX 或一些较新的 JS 语法)。

Converts assets (eg PNG files) into objects that can be displayed by an Image component.将资产(例如 PNG 文件)转换为可以由 Image 组件显示的对象。

reference: https://hackernoon.com/understanding-expo-for-react-native-7bf23054bbcd参考: https : //hackernoon.com/understanding-expo-for-react-native-7bf23054bbcd

Metro is a JavaScript bundler which takes in options, an entry file, and gives you a JavaScript file including all JavaScript files back. Metro 是一个 JavaScript 打包器,它接收选项、一个入口文件,并为您提供一个包含所有 JavaScript 文件的 JavaScript 文件。 Every time you run a react native project, a compilation of many javascript files are done into a single file.每次运行 React Native 项目时,都会将许多 javascript 文件编译成一个文件。 This compilation is done by a bundler which is called Metro.这个编译是由一个叫做 Metro 的打包器完成的。

Answers to your questions:回答您的问题:

1> Bundled file is located on the device itself on which you are building your app and is stored in different formats like in case of Android Plain bundling in which .bundle is created. 1> 捆绑文件位于您正在构建应用程序的设备上,并以不同的格式存储,例如在创建 .bundle 的 Android 普通捆绑的情况下。 Another format is of Indexed RAM bundle in which file is stored as binary file.另一种格式是索引 RAM 包,其中文件存储为二进制文件。

2> Webpack is also a similar type of module bundler which does bundling to ReactJS web platform and its modules are accessible through browser. 2> Webpack 也是一种类似的模块打包器,它绑定到 ReactJS Web 平台,并且它的模块可以通过浏览器访问。 Bundling process is while similar to metro.捆绑过程与地铁相似。

3> These bundled files are indexed and stored in a particular numerical format and thus its easy at the run time to arrange JS files in order. 3> 这些捆绑文件被索引并以特定的数字格式存储,因此在运行时很容易按顺序排列 JS 文件。

There are multiple functions of Metro bundler and you can read about the role of Metro in React Native here : https://medium.com/@rishabh0297/role-of-metro-bundler-in-react-native-24d178c7117e Metro bundler 有多种功能,您可以在此处阅读 Metro 在 React Native 中的作用: https : //medium.com/@rishabh0297/role-of-metro-bundler-in-react-native-24d178c7117e

Hope it helps.希望能帮助到你。

Metro team keeps improving its documentation, now you can find some really good explanations at https://facebook.github.io/metro/docs/concepts (link updated): Metro 团队不断改进其文档,现在您可以在https://facebook.github.io/metro/docs/concepts 上找到一些非常好的解释(链接已更新):

Metro is a JavaScript bundler. Metro 是一个 JavaScript 打包器。 It takes in an entry file and various options, and gives you back a single JavaScript file that includes al l your code and its dependencies.它接受一个入口文件和各种选项,并返回一个包含所有代码及其依赖项的JavaScript 文件

So yes, it is a sort of Webpack, but for React Native apps :)所以是的,它是一种 Webpack,但对于 React Native 应用程序:)

But where is the bundle file located?但是捆绑文件在哪里?

Once the bundler is started, you can check its contents at http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false (like webpack, it is served from memory, so it is not being written on your project's folder).一旦 bundler 启动,你可以在http://localhost:8081/index.bundle?platform=ios&dev=true& minify =false 上检查它的内容(就像 webpack,它是从内存中提供的,所以它不会被写入你的项目的文件夹)。

What is the use of that bundle file ?那个包文件有什么用?

This file is installed in the device for its code to be executed there.该文件安装在设备中,以便在那里执行其代码。 Remember that when you are writing code for a React Native application, your code is not "translated" to Java / Swift / whatever .请记住,当您为 React Native 应用程序编写代码时,您的代码不会“转换”为 Java/Swift/whatever The Native Modules will send events to a Javascript thread, and the JS thread will execute your bundled React Native code. Native Modules 将事件发送到 Javascript 线程,JS 线程将执行您捆绑的 React Native 代码。

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

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