什么是“注册"?意思是"babel/register" [英] What does "register" mean in "babel/register"

查看:183
本文介绍了什么是“注册"?意思是"babel/register"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在Babel中进行运行时转换,您需要并使用babel-core/register.我不知道register在这种意义上的含义,即实际定义.

In order to do runtime transformations in Babel you need to require and use babel-core/register. I have no idea what register means in this sense, i.e. the actual definition.

该页面不是很有帮助.

这实际上是什么意思?

推荐答案

好,所以babel的目的是将js当前代码转换为给定环境,工具和框架的可理解的js版本.重新使用.此处列出的内容有所不同, 如何在您选择的工具中使用Babel. 在节点环境中,babel并不作为其核心API的一部分而存在,因此需要首先将其作为npm软件包添加(我的示例适用于@babel 7.x).而且由于babel是分开的以容纳不同的工具,因此我们需要添加@ babel/core作为核心功能以及@ babel/preset-env,以实现ES2015 +的转换.

ok, so the purpose of babel as you know is to transpile your js current code to an understandable version of js for the given environment, tool, framework you're using. Those vary as listed here, How to use Babel with your tool of choice. In the node environment, babel does not exist as part of its core APIs, so it needs to be added as an npm package first, (my example is for @babel 7.x ). And because babel is separated to accommodate different tools, we need to add @babel/core for the core functionality along with @babel/preset-env which enables transforms for ES2015+.

npm install @babel/core @babel/preset-env --save-dev

npm i -D @babel/core @babel-preset

现在,当我们使用babel时,我们希望通过在根目录在此找到的更多信息

now as we use babel we want the tell node about the available preset by setting up a .bablerc file in the root directory, more on that found here

{
  "presets": ["@babel/preset-env"]
}

现在进入寄存器方面.由于ES6模块具有在此处解释的性质,如果需要的话要在没有构建步骤(如webpack或汇总)的情况下运行babel,并使用babel即时运行文件,我们需要将babel注册到节点的运行时中,require钩子会将自身绑定到节点的require并在运行时自动编译文件.这等效于CoffeeScript的coffee-script/register. 来自babel使用文档的参考在这里找到babel寄存器.因此,与以前的npm安装一起,我们还需要添加@babel/register:

now to get to the register aspect. Because of the nature of ES6 modules as explained here, if we want to run babel without a build step like webpack or rollup, and run files using babel 'on the fly' we need to register babel into node's runtime The require hook will bind itself to node’s require and automatically compile files on at runtime. This is equivalent to CoffeeScript’s coffee-script/register. reference from babel usage docs for babel-register found here. Therefore, along with the previous npm install, we'll also need to add @babel/register:

npm install @babel/register --save-dev

npm i -D @babel/register

现在我们可以使用它,可以通过两种方式之一在应用程序文件中要求"@ bable/register",要么在文件中(通常在index.js文件中,该文件是您应用程序的入口,并包含要求添加到其他文件)或在使用cli时将其添加.

Now we can use it, either require "@bable/register" in the application files by one of two means, either in a file (generally, in the index.js file that is the entry point in your app and contains requires to other files) or add it in when using the cli.

// in .js file
require("@babel/register");

// in command line, don't add it to the .js file but instead use the -r flag for require
npx -r @babel/register index.js

(有关npx的更多信息,可以在此处找到 )

作为添加.bablerc的选项,可以通过在package.json文件中添加"babel"属性作为选项来跳过,例如.

As an option to adding the .bablerc, it can be skipped by instead adding a "babel" property within the package.json file as an option, ex.

//package.json in root
...,
"babel": {
  "presets":[
    "@babel/preset-env"
   ]
},
...

尽管上述内容适用于babel 7,但可以从一位出色的老师的github中找到babel 6中的一个示例,

While the above was for babel 7, an example in babel 6 can be found from a great teacher's github, Josh Miller, here (view his package.json file) Hope this helps for an understanding of 'register' needs.

这篇关于什么是“注册"?意思是"babel/register"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆