Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

javascript - Is it okay to use babel-node in production

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

I am just wondering, can I run this in production as babel-node server rather than node server What other options do I have to run ES6 in node?

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"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


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

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+

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

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"]
}

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"
}

Then build it!

$ npm run build

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

$ npm start

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

require("babel/register");

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.

You will be able to keep your source files in ES6 but still execute them using 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 requires will be transformed by Babel...

Here's what a typical setup might look like

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

fire it up!

$ node server.js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...