programing tip

프로덕션에서 바벨 노드를 사용해도 되나요?

itbloger 2020. 9. 24. 07:37
반응형

프로덕션에서 바벨 노드를 사용해도 되나요?


ES6 구문을 지원하기 위해 babel-node 및 babelify 변환으로 browserify를 사용하여 사이트를 개발하고 있습니다.

노드에서 ES6를 실행하는 데 필요한 다른 옵션이 babel-node server 아닌 프로덕션에서 이것을 실행할 수 있는지 궁금 node server 합니다.

다음은 빌드 및 개발 시작을 위해 실행중인 명령입니다.

// 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그것을 클라이언트에게 발송하십시오.


서버 측 코드의 경우 babel-cli를 사용하여 일반 빌드를 수행합니다.

에 따르면 http://babeljs.io/docs/setup/#babel_register , babel-register되어 생산에는 적합하지 - 훅은 주로 간단한 경우에 권장됩니다 필요합니다.

Babel 6+ 용

Babel 6부터는 기본적으로 변환이 포함되지 않습니다. 이제 babel-clibabel-preset-es2015.

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

.babelrc파일에 변형을 추가하십시오. 이것은 위에서 다운로드 한 perst 모듈입니다. 자신에게 가장 적합한 프리셋 을 확인 하려면 전체 사전 설정 목록을 살펴보십시오 .

{
  "presets": ["es2015"]
}

build스크립트를 추가 합니다 package.json. 아래 src는 입력 파일이며 build변환 된 출력 파일입니다.

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

그런 다음 구축하십시오!

$ npm run build

그런 다음 코드를 실행하십시오. 이 시점에서 build디렉토리 의 파일을 실행하고 싶을 것입니다.

$ npm start

Babel <= 5의 경우 require 후크를 사용하십시오.

require("babel/register");

확장자가 .es6 , .es , .jsx.js 인 노드에 필요한 모든 후속 파일 은 Babel에 의해 변환됩니다. polyfill는 자동으로 필요합니다.

소스 파일을 ES6에 보관할 수 있지만 여전히 다음을 사용하여 실행할 수 있습니다. node server.js


귀하의 의견에 따르면 약간의 문제가있는 것 같습니다. 위의 노란색으로 강조 표시된 부분에 특히주의하십시오. 첫 번째 파일은 노드 자체에서 실행되는 ES5 만 될 수 있습니다. 이후의 모든 요구 사항 은 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

이 주제에 대한 블로그 게시물을 작성 했습니다.

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.

This is an example of how you could set up the npm scripts to run your app with node instead of 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


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

  • babel-node does 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.

On the other hand,

  • using babel-node directly simplifies development - you won't need "build" scripts, and you won't have separate src/lib and dist directories
  • if you import from local files, will you import from src/myutils, or from lib/myutils? Using babel-node eliminates that problem.

I only use Babel for modules support. Now V8 just released support for modules on 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.


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

To answer your question briefly: no, babel-node shouldn't be invoked explicitly by you. babel-node is a private library that's consumed by 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.

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.

참고URL : https://stackoverflow.com/questions/30773756/is-it-okay-to-use-babel-node-in-production

반응형