是否可以在生产中使用babel-node [英] Is it okay to use babel-node in production

查看:134
本文介绍了是否可以在生产中使用babel-node的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我只是想知道,我可以吗?在生产中运行 babel-node服务器 而不是 节点服务器 我还有哪些其他选项可以在节点中运行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"

以下是我的开发依赖项

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


推荐答案

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

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

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

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


根据 http://babeljs.io/ docs / setup /#babel_register babel-register 不适合生产用途—对于简单的情况,主要推荐使用require钩子。

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

for Babel 6 +

for Babel 6+

截至Babel 6,默认情况下不包含任何转换。所以我们首先安装 babel-cli babel-preset-es2015

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

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

.babelrc 文件中添加转换—这是我们上面下载的perst模块。请查看预设的完整列表,了解哪一个最适合您。

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

{
  "presets": ["es2015"]
}

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

Add a build script to your package.json. Below src is your input files and build is the transformed output files

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

然后构建它!

$ npm run build

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

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

$ npm start

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

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

require("babel/register");




扩展名为 .es6的节点所需的所有后续文件 .es .jsx .js 将由Babel转换。 polyfill 也是自动需要的。

All subsequent files required by node with the extensions .es6, .es, .jsx and .js will be transformed by Babel. The polyfill is also automatically required.

您将能够将源文件保存在ES6中,但仍然使用节点server.js执行它们

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

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

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 requires will be transformed by Babel...

以下是典型设置的外观

server.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

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

启动它!

$ node server.js

这篇关于是否可以在生产中使用babel-node的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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