├── .gitignore ├── .npmignore ├── CHANGELOG.md ├── README.md ├── index.js ├── package-lock.json ├── package.json └── tests ├── acceptance ├── load2Test.js └── loadTest.js └── runner.js /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | node_modules 3 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | tests -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ### 7.0.0: 2 | - [ENHANCEMENT] Upgrade to Babel 7. Thanks to [@pahan35](https://github.com/pahan35). 3 | 4 | ### 6.0.3: 5 | - [ENHANCEMENT] Dependencies update. 6 | 7 | ### 6.0.2: 8 | - [BUGFIX] ````filename` of undefined``` bug fix 9 | 10 | ### 6.0.0: 11 | * [ENHANCEMENT] [Babel 6 support.](http://babeljs.io/blog/2015/10/29/6.0.0/) 12 | * [ENHANCEMENT] [All Babel 6 options support.](https://babeljs.io/docs/usage/options/) 13 | * [FEATURE] Source map support for stack traces. 14 | 15 | ### 5.0.1 16 | * [BUGFIX] removed unnecessary console.log 17 | 18 | ### 5.0.0 19 | * [BREAKING ENHANCEMENT] Follow babeljs major semver 20 | * [BREAKING ENHANCEMENT] changed `experimental` to `stage` which now accepts an integer. `stage: 0` is the same as `experimental: true` 21 | * [FEATURE] added options for `ignore`, `only` and `extensions`. 22 | 23 | ### 2.1.0 24 | * [ENHANCEMENT] Bumped Babel version. 25 | * [ENHANCEMENT] Made `experimental` flag configurable. 26 | * [ENHANCEMENT] Made `loose` flag configurable. 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sails-hook-babel 2 | [![npm version](https://badge.fury.io/js/sails-hook-babel.svg)](https://npmjs.org/package/sails-hook-babel) [![Dependency Status](https://img.shields.io/david/sane/sails-hook-babel.svg?style=flat)](https://david-dm.org/sane/sails-hook-babel) 3 | 4 | *Needs at least Sails version 0.11.0 to work* 5 | 6 | [Sails JS](http://sailsjs.org) hook to activate ES6/7 Javascript code for your whole sails app, via [https://babeljs.io/](https://babeljs.io/). 7 | 8 | ### Installation 9 | 10 | `npm install sails-hook-babel` 11 | 12 | ### Usage 13 | 14 | Just lift your app as normal, and enjoy the future of Javascript today. To see what is possible, see: https://babeljs.io/docs/en/learn 15 | 16 | ### Configuration 17 | 18 | By default, configuration lives in `sails.config.babel`. The configuration key (`babel`) can be changed by setting `sails.config.hooks['sails-hook-babel'].configKey`. 19 | 20 | Parameter | Type | Details 21 | -------------- | ------------------- |:--------------------------------- 22 | compile | ((boolean)) | Whether or not sails should compile future JS code. Defaults to `true`. 23 | polyfill | ((boolean)) | Whether or not use `@babel/polyfill`. Defaults to `false`. 24 | presets | ((array)) | Which presets to transpile your code with. Defaults to [@babel/preset-env](https://babeljs.io/docs/en/babel-preset-env/). 25 | 26 | Also you can use [other Babel 7 options](https://babeljs.io/docs/en/options). 27 | 28 | That’s it! 29 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var path = require('path'), 2 | sourceMapSupport = require('source-map-support'), 3 | babelRegisterCache = require('@babel/register/lib/cache'); 4 | 5 | module.exports = function(sails) { 6 | 7 | return { 8 | 9 | /** 10 | * Default configuration 11 | * 12 | * We do this in a function since the configuration key for 13 | * the hook is itself configurable, so we can't just return 14 | * an object. 15 | */ 16 | defaults: { 17 | 18 | __configKey__: { 19 | // Turn babel compile on by default 20 | compile: true, 21 | // Doesn't import polyfill by default 22 | polyfill: false, 23 | // Activates preset tranformations 24 | presets: ['@babel/env'] 25 | } 26 | }, 27 | 28 | /** 29 | * Initialize the hook 30 | * @param {Function} cb Callback for when we're done initializing 31 | */ 32 | configure: function() { 33 | 34 | var config = sails.config[this.configKey]; 35 | 36 | // If the hook has been deactivated, just return 37 | if (!config.compile) { 38 | sails.log.verbose('Babel hook deactivated.'); 39 | } else { 40 | 41 | if (sails.config.environment != 'production') { 42 | 43 | sourceMapSupport.install({ 44 | retrieveSourceMap: function(file) { 45 | 46 | var cache = babelRegisterCache.get(), 47 | sourceMap = null; 48 | 49 | Object.keys(cache).some(function(hash) { 50 | 51 | var fileCache = cache[hash]; 52 | 53 | if (typeof fileCache == 'undefined' || typeof fileCache.options == 'undefined' 54 | || fileCache.options.filename != file 55 | ) { 56 | return false; 57 | } 58 | 59 | sourceMap = { 60 | url: file, 61 | map: fileCache.map 62 | }; 63 | 64 | return true; 65 | }); 66 | 67 | return sourceMap; 68 | } 69 | }); 70 | } 71 | 72 | if (config.polyfill) { 73 | require('@babel/polyfill'); 74 | } 75 | 76 | delete config.polyfill; 77 | delete config.compile; 78 | 79 | require('@babel/register')(config); 80 | 81 | sails.log.verbose('Babel hook activated. Enjoy ES6/7 power in your Sails app.'); 82 | } 83 | }, 84 | }; 85 | }; 86 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sails-hook-babel", 3 | "version": "7.0.0", 4 | "description": "Sails JS hook to activate babel for the whole api folder", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node tests/runner", 8 | "test-all": "node tests/runner all", 9 | "autotest": "mocha --watch --reporter spec tests/**/*Test.js" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git://github.com/sane/sails-hook-babel.git" 14 | }, 15 | "keywords": [ 16 | "sails", 17 | "hook", 18 | "babel", 19 | "transpiler", 20 | "ES6", 21 | "ES2015", 22 | "ES7", 23 | "sails-hook" 24 | ], 25 | "author": "Markus Padourek", 26 | "license": "MIT", 27 | "bugs": { 28 | "url": "https://github.com/sane/sails-hook-babel/issues" 29 | }, 30 | "homepage": "https://github.com/sane/sails-hook-babel", 31 | "sails": { 32 | "isHook": true 33 | }, 34 | "dependencies": { 35 | "@babel/core": "^7.0.0", 36 | "@babel/polyfill": "^7.0.0", 37 | "@babel/preset-env": "^7.0.0", 38 | "@babel/register": "^7.0.0", 39 | "source-map-support": "^0.5.9" 40 | }, 41 | "devDependencies": { 42 | "glob": "^5.0.3", 43 | "mocha": "^2.2.1", 44 | "sails": "~0.11.0" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /tests/acceptance/load2Test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Module dependencies 3 | */ 4 | 5 | var Sails = require('sails').Sails; 6 | // var assert = require('chai').assert; 7 | 8 | describe('Empty test', function() { 9 | 10 | var sails; 11 | 12 | beforeEach(function () { 13 | // Hook will timeout in 10 seconds 14 | this.timeout(10000); 15 | 16 | // Attempt to lift sails 17 | Sails().lift({ 18 | hooks: { 19 | // Load the hook 20 | "babel": require('../../'), 21 | // Skip grunt 22 | "grunt": false 23 | }, 24 | log: {level: "error"} 25 | },function (err, _sails) { 26 | if (err) return err; 27 | sails = _sails; 28 | //return done(); 29 | }); 30 | }); 31 | 32 | afterEach(function () { 33 | if (sails) { 34 | sails.lower(); 35 | } 36 | //return done(); 37 | }); 38 | 39 | it ('Somehow need this test for mocha to finish', function() { 40 | return true; 41 | }); 42 | 43 | }); 44 | -------------------------------------------------------------------------------- /tests/acceptance/loadTest.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Module dependencies 3 | */ 4 | 5 | var Sails = require('sails').Sails; 6 | 7 | describe('Acceptance tests', function() { 8 | 9 | var sails; 10 | 11 | beforeEach(function () { 12 | // Hook will timeout in 10 seconds 13 | // this.timeout(10000); 14 | 15 | // Attempt to lift sails 16 | Sails().lift({ 17 | hooks: { 18 | // Load the hook 19 | "babel": require('../../'), 20 | // Skip grunt 21 | "grunt": false 22 | }, 23 | log: {level: "error"} 24 | },function (err, _sails) { 25 | if (err) return err; 26 | sails = _sails; 27 | //return done(); 28 | }); 29 | }); 30 | 31 | afterEach(function () { 32 | if (sails) { 33 | sails.lower(); 34 | } 35 | //return done(); 36 | }); 37 | 38 | it ('sails loads traceur hook and does not crash', function() { 39 | return true 40 | }); 41 | 42 | }); 43 | -------------------------------------------------------------------------------- /tests/runner.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var glob = require('glob'); 4 | var Mocha = require('mocha'); 5 | var fs = require('fs'); 6 | var path = require('path'); 7 | 8 | var mocha = new Mocha({ 9 | timeout: 5000, 10 | reporter: 'spec' 11 | }); 12 | 13 | // var arg = process.argv[2]; 14 | // var root = 'tests/{unit,acceptance}'; 15 | var root = 'tests/acceptance'; 16 | 17 | function addFiles(mocha, files) { 18 | glob.sync(root + files).forEach(mocha.addFile.bind(mocha)); 19 | } 20 | 21 | addFiles(mocha, '/**/*Test.js'); 22 | 23 | mocha.run(function(failures) { 24 | process.on('exit', function() { 25 | process.exit(failures); 26 | }); 27 | }); 28 | --------------------------------------------------------------------------------