├── .gitignore ├── test └── point_test.js ├── src ├── require-hook.js └── point.js ├── README.md ├── package.json └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .idea/ 3 | node_modules 4 | -------------------------------------------------------------------------------- /test/point_test.js: -------------------------------------------------------------------------------- 1 | /* global suite */ 2 | /* global test */ 3 | 4 | import assert from 'assert'; 5 | 6 | import { Point } from '../src/point'; 7 | 8 | 9 | suite('Point'); 10 | 11 | test('Instance properties', () => { 12 | let p = new Point(1, 5); 13 | assert.strictEqual(p.x, 1); 14 | assert.strictEqual(p.y, 5); 15 | }); 16 | -------------------------------------------------------------------------------- /src/require-hook.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | // The advantage of a require hook is that 4 | // you can start via normal Node.js 5 | 6 | require("babel-register")({ 7 | presets: [ 8 | "es2015-node5" 9 | ], 10 | }); 11 | 12 | var point = require('./point'); 13 | 14 | console.log('Case in point: ' + new point.Point(8, 2)); 15 | -------------------------------------------------------------------------------- /src/point.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env babel-node 2 | 3 | export class Point { 4 | constructor(x, y) { 5 | this.x = x; 6 | this.y = y; 7 | } 8 | toString() { 9 | return `new Point(${this.x}, ${this.y})`; 10 | } 11 | } 12 | 13 | if (require.main === module) { 14 | let pt = new Point(7,4); 15 | console.log(`My point: ${pt}`); 16 | } 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Node.js Babel dynamic demo 2 | 3 | Demonstrates how to use Babel dynamically (compiled at runtime) on Node.js. 4 | 5 | * Install: 6 | 7 | ``` 8 | cd node-babel-dynamic-demo/ 9 | npm install 10 | ``` 11 | 12 | * Run script: 13 | 14 | ``` 15 | npm run point 16 | ``` 17 | 18 | * Run unit test via mocha: 19 | 20 | ``` 21 | npm test 22 | ``` 23 | 24 | Thanks to npm scripts, everything you need is installed locally. -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "babel-cli": "^6.2.4", 4 | "babel-preset-es2015-node5": "^1.1.0" 5 | }, 6 | "//": "babel-register is needed for mocha", 7 | "devDependencies": { 8 | "mocha": "^2.2.5", 9 | "babel-register": "^6.2.0" 10 | }, 11 | "bin": { 12 | "point": "./src/point.js" 13 | }, 14 | "scripts": { 15 | "point": "babel-node ./src/point.js", 16 | "b": "babel-node", 17 | "test": "mocha --ui qunit --compilers js:babel-register" 18 | }, 19 | "babel": { 20 | "presets": [ 21 | "es2015-node5" 22 | ] 23 | }, 24 | "author": "Axel Rauschmayer", 25 | "license": "MIT" 26 | } 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Axel Rauschmayer 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | --------------------------------------------------------------------------------