/babel-comments

🐠 Babel is a compiler for writing next generation JavaScript.

Primary LanguageJavaScriptMIT LicenseMIT

babel comments

babel의 첫번째 μ»€λ°‹μ—μ„œλΆ€ν„° μ½”λ“œλ₯Ό λΆ„μ„ν•˜κ³  μ–΄λ–»κ²Œ κ΅¬ν˜„λ˜μ–΄μžˆκ³  λ™μž‘ν•˜λŠ” 원리λ₯Ό μ•Œμ•„λ΄…μ‹œλ‹€.

폴더 ꡬ쑰

/
  /lib
    /6to5
      /templates
      /transformers
      /traverse
      /browserify.js
      /middleware.js
      /node.js
      /transform.js
      /util.js
  /package.json

파일 λ‚΄μš©

νŒ¨ν‚€μ§€μ˜ λ‚΄μš©μ„ μ •μ˜ν•©λ‹ˆλ‹€. μ—”νŠΈλ¦¬ 파일이 ./lib/6to5/node.jsμž„μ„ μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€.

AST의 λ…Έλ“œ 쀑 es6의 λ¬Έλ²•μœΌλ‘œ μž‘μ„±λœ 것을 es5의 문법에 λ§žλŠ” λ…Έλ“œλ‘œ λ³€ν™˜ν•΄μ£ΌλŠ” ν•¨μˆ˜κ°€ μ„ μ–Έλ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.

μ•„λ§ˆ λ²”μš©μ μœΌλ‘œ μ‚¬μš©ν•˜κΈ° μœ„ν•΄ μ •μ˜λœ λ―Έλ“€μ›¨μ–΄νŒŒμΌμ΄λΌκ³  μƒκ°ν•©λ‹ˆλ‹€.

babel의 μ—”νŠΈλ¦¬ μ§€μ μž…λ‹ˆλ‹€. νŒ¨ν‚€μ§€μ—μ„œ μ‚¬μš©λ˜λŠ” ν•¨μˆ˜λ₯Ό μ„ μ–Έν•©λ‹ˆλ‹€.

es6 λ¬Έλ²•μœΌλ‘œ μ •μ˜λœ μ½”λ“œλ₯Ό es5 λ¬Έλ²•μœΌλ‘œ μ •μ˜λœ μ½”λ“œλ‘œ λ³€ν™˜ν•˜λŠ” ν•¨μˆ˜κ°€ μ„ μ–Έλ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€. es6 λ¬Έλ²•μ˜ μ½”λ“œλ₯Ό AST둜 λ³€ν™˜ν•˜λŠ” ν•¨μˆ˜λŠ” ./lib/6to5/transformers에 μ •μ˜λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€. λ‹€λ§Œ ASTλ‘œλΆ€ν„° ECMAScript μ½”λ“œ 생성은 μ™ΈλΆ€ νŒ¨ν‚€μ§€μ— μ˜μ‘΄ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

νŒŒμ„œ λ“±μ˜ ν•¨μˆ˜κ°€ κ΅¬ν˜„λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€. 참고둜 νŒŒμ„œμ—μ„œλŠ” μ™ΈλΆ€ λͺ¨λ“ˆ(esprima)을 μ‚¬μš©ν•©λ‹ˆλ‹€.

μ•„λž˜λŠ” 원 repo의 readme 파일 λ‚΄μš©μž…λ‹ˆλ‹€.


6to6

6to5 turns ES6 code into vanilla ES5, so you can use ES6 features today.

6to5 is:

  • Fast - 10x faster than Traceur.
  • Compact - maps directly to the equivalent ES5.
  • Easy - with Browserify support, Node API, Connect Middleware and a CLI.
  • Concise - we do not pollute any scope with unneccesary variables or functions declarations.

Features

Name Implemented
Arrow functions βœ“
Classes βœ“
Default parameters βœ“
Spread βœ“
Block binding βœ“
Property method assignment βœ“
Rest parameters βœ“
Template literals βœ“
Modules βœ“
Destructuring assignment
Generators

Installation

$ npm install -g 6to5

Usage

CLI

Compile the file script.js and output it to script-compiled.js.

$ 6to5 script.js -o script-compiled.js

Compile the entire src directory and output it to the lib directory.

$ 6to5 src -d lib

Compile the file script.js and output it to stdout.

$ 6to5 script.js

Node

var to5 = require("6to5");

to5.transform("code();");

to5.transformFileSync("script.js");

to5.transformFile("script.js", function (err, data) {

});
Options
to5.transform("code();", {
  // List of transformers to EXCLUDE
  // See `features` for valid names.
  blacklist: [],

  // List of transformers to ONLY use.
  // See `features` for valid names.
  whitelist: [],

  // Append source map and comment to bottom of returned output.
  sourceMap: false,

  // Filename for use in errors etc.
  filename: "unknown",

  // Format options
  // See https://github.com/Constellation/escodegen/wiki/API for options.
  format: {}
});

Require hook

All subsequent files required by node will be transformed into ES5 compatible code.

require("6to5/register");

Connect Middleware

var to5 = require("6to5");

app.use(6to5.middleware({
  transform: {
    // options to use when transforming files
  },
  src: "assets",
  dest: "cache"
}));

app.use(connect.static("cache"));

Browserify

CLI

$ browserify script.js -t 6to5/browserify --outfile bundle.js

Node

var to5 = require("6to5");
browserify()
  .transform(to5.browserify)
  .require("script.js", { entry: true })
  .bundle({ debug: true })
  .pipe(fs.createWriteStream("bundle.js"));

Caveats

Generators

Let

Comparison to Traceur