Javascript 在生产中使用 babel-node 可以吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30773756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:43:09  来源:igfitidea点击:

Is it okay to use babel-node in production

javascriptnode.jsbrowserifybabeljs

提问by svnm

I have been developing a site using babel-node and browserify with the babelify transform, to support ES6 syntax.

我一直在使用 babel-node 和带有 babelify 转换的 browserify 开发一个站点,以支持 ES6 语法。

I am just wondering, can I run this in production asbabel-node serverrather thannode serverWhat other options do I have to run ES6 in node?

我只是想知道,我可以在生产中运行它babel-node server而不是node server在节点中运行 ES6 的其他选项吗?

Here are the commands I am running for build and start in development

这是我运行的用于构建和开始开发的命令

// npm run build
browserify -t [babelify] client.js > public/js/bundle.js",

// npm start
babel-node server.js"

Here are my dev dependencies

这是我的开发依赖项

"babel": "^4.0.1",
"babelify": "^5.0.3",
"browserify": "^8.0.3"

回答by Thank you

For the client side code, you're doing the correct thing. babelifyit and ship it to the client.

对于客户端代码,您正在做正确的事情。babelify并将其发送给客户。



For the server side code, I would just do a regular build using babel-cli

对于服务器端代码,我只会使用babel-cli进行常规构建

According to http://babeljs.io/docs/setup/#babel_register, babel-registeris not meant for production use— The require hook is primarily recommended for simple cases.

http://babeljs.io/docs/setup/#babel_registerbabel-register不能用于生产环境-钩推荐用于简单的情况下的需要。

for Babel 6+

通天塔 6+

As of Babel 6, no transformations are included by default. So let's start by installing babel-cliand babel-preset-es2015.

从 Babel 6 开始,默认情况下不包含任何转换。所以让我们从安装babel-cli和开始babel-preset-es2015

$ npm install --save-dev babel-cli babel-preset-es2015

Add a transformation to your .babelrcfile — this is the prest module we downloaded above. Take a look at the full list of presetsto see which one(s) are a best fit for you.

向您的.babelrc文件添加转换——这是我们在上面下载的 perst 模块。查看完整的预设列表,看看哪些(哪些)最适合您。

{
  "presets": ["es2015"]
}

Add a buildscript to your package.json. Below srcis your input files and buildis the transformed output files

build脚本添加到您的package.json. 下面src是您的输入文件和build转换后的输出文件

"scripts": {
  "build": "babel src -d build"
}

Then build it!

然后建造它!

$ npm run build

Then run your code. At this point, you'll want to be executing the files in your builddirectory

然后运行你的代码。此时,您需要执行目录中的build文件

$ npm start

for Babel <= 5, just use the require hook.

对于 Babel <= 5,只需使用 require 钩子。

require("babel/register");

All subsequent files required by node with the extensions .es6, .es, .jsxand .jswill be transformed by Babel. The polyfillis also automatically required.

node 需要的所有后续文件扩展名为.es6.es.jsx.js都将被 Babel 转换。该填充工具也需要自动。

You will be able to keep your source files in ES6 but still execute them using node server.js

您将能够将源文件保留在 ES6 中,但仍然使用 node server.js



According to your comments, you seem to be having a little trouble. Pay particular attention to the yellow highlighted part above. Your first file can only be ES5, which is run by node itself. All subsequent requireswill be transformed by Babel...

根据您的评论,您似乎遇到了一些麻烦。请特别注意上面黄色突出显示的部分。您的第一个文件只能是 ES5,它由节点本身运行。所有后续的需求都会被 Babel 转化...

Here's what a typical setup might look like

以下是典型设置的样子

server.js

服务器.js

// only ES5 is allowed in this file
require("babel/register");

// other babel configuration, if necessary

// load your app
var app = require("./app.js");

app.js

应用程序.js

// this file will be loaded through babel
// you can now use ES6 here and in every other include

fire it up!

燃烧起来;动起来!

$ node server.js

回答by cuadraman

I just wrote a blog post on this topic

我刚刚写了一篇关于这个主题的博客文章

Babeljs CLI documentationwarns the following:

Babeljs CLI 文档警告如下:

babel-node not meant for production use

You should not be using babel-node in production. It is unnecessarily heavy, with high memory usage due to the cache being stored in memory. You will also always experience a startup performance penalty as the entire app needs to be compiled on the fly.

babel-node 不适合生产使用

您不应该在生产中使用 babel-node。由于缓存存储在内存中,它不必要地繁重,内存使用率高。由于整个应用程序需要即时编译,因此您也将始终遇到启动性能损失。

This is an example of how you could set up the npm scripts to run your app with node instead of babel-node.

这是一个如何设置 npm 脚本以使用 node 而不是 babel-node 运行应用程序的示例。

"scripts": {
  "clean": "rm -rf build && mkdir build",
  "build-css": "node-sass scss/app.scss public/css/app.css",
  "build-server": "babel -d ./build ./server -s",
  "build": "npm run clean && npm run build-css && npm run build-server",
  "lint": "eslint source/ --quiet",
  "start": "node ./build/index.js",
  "debug": "node --debug ./build/index.js",
  "test": "for i in $(ls tests/); do babel-node \"./tests/${i}\" | faucet ; done",
  "validate": "npm run lint; npm run test && npm outdated --depth 0"
},

You can find more details on the blog post

您可以在博客文章中找到更多详细信息

回答by Dan Dascalescu

It's important to weigh the pros and cons of using babel-node in production.

权衡在生产中使用 babel-node 的利弊很重要。

  • babel-nodedoes add between half a second to one second to the startup cost, on commodity hardware. But if your app is a long-running server, that startup cost won't matter much.
  • Try to measure the extra memory consumption. For my app for example (reading and processing time series data), it was only 20MB. Depending on your situation, this may or may not be significant.
  • babel-node在商品硬件上,确实会增加半秒到一秒的启动成本。但是,如果您的应用程序是一个长期运行的服务器,那么启动成本就不那么重要了。
  • 尝试测量额外的内存消耗。以我的应用为例(读取和处理时间序列数据),它只有 20MB。根据您的情况,这可能重要也可能不重要。

On the other hand,

另一方面,

  • using babel-node directly simplifies development - you won't need "build" scripts, and you won't have separate src/liband distdirectories
  • if you importfrom local files, will you import from src/myutils, or from lib/myutils? Using babel-nodeeliminates that problem.
  • 直接使用 babel-node 简化了开发——你不需要“构建”脚本,也不需要单独的src/libdist目录
  • 如果您import来自本地文件,您是从src/myutils还是从导入lib/myutils?使用babel-node消除了这个问题。

I only use Babel for modules support. Now V8 just released support for moduleson January 10, 2017. Hopefully we'll see modules support in Node under a flag in a few months, rendering my reason for using Babel moot.

我只使用 Babel 来支持模块。现在 V8 刚刚在 2017 年 1 月 10 日发布了对模块的支持。希望我们能在几个月后看到 Node 中的模块支持在一个标志下,这说明我使用 Babel 的理由没有实际意义。

回答by wle8300

@cuadraman's answer is more accurate than @naomik.

@cuadraman 的回答比 @naomik 更准确。

To answer your question briefly: no, babel-nodeshouldn't be invoked explicitly by you. babel-nodeis a private library that's consumed by babel-cli.

简要回答您的问题:不,您babel-node不应明确调用。babel-node是由babel-cli.

The official tutorial has everything you need to get up and running on node (not browser-side!): https://github.com/babel/example-node-server. READ IT! I found so many misleading blog tutorials that used round about ways, and found this article the easiest to follow.

官方教程包含在节点(不是浏览器端!)上启动和运行所需的一切:https: //github.com/babel/example-node-server。阅读!我发现了很多误导性的博客教程,它们使用了各种方式,并且发现这篇文章最容易理解。

Bonus: contrary to what many people think, all of the transpiling magic can be installed locally (using npm install --save-dev babel-cli nodemon babel-preset-es2015 babel-preset-stage-2). No need to install Babel or any of its helper modules globally! Pretty nifty.

奖励:与许多人的想法相反,所有转译魔法都可以在本地安装(使用npm install --save-dev babel-cli nodemon babel-preset-es2015 babel-preset-stage-2)。无需全局安装 Babel 或其任何辅助模块!很漂亮。