diff --git a/build/Gruntfile.coffee b/build/Gruntfile.coffee index abeb13f14..d74c0b50e 100644 --- a/build/Gruntfile.coffee +++ b/build/Gruntfile.coffee @@ -1,6 +1,8 @@ fs = require 'fs' path = require 'path' os = require 'os' +glob = require 'glob' +usesBabel = require './lib/uses-babel' # Add support for obselete APIs of vm module so we can make some third-party # modules work under node v0.11.x. @@ -15,6 +17,7 @@ packageJson = require '../package.json' _.extend(global, require('harmony-collections')) unless global.WeakMap? module.exports = (grunt) -> + grunt.loadNpmTasks('grunt-babel') grunt.loadNpmTasks('grunt-coffeelint') grunt.loadNpmTasks('grunt-lesslint') grunt.loadNpmTasks('grunt-cson') @@ -77,6 +80,11 @@ module.exports = (grunt) -> dest: appDir ext: '.js' + babelConfig = + options: {} + dist: + files: [] + lessConfig = options: paths: [ @@ -141,6 +149,13 @@ module.exports = (grunt) -> pegConfig.glob_to_multiple.src.push("#{directory}/lib/*.pegjs") + for jsFile in glob.sync("#{directory}/lib/**/*.js") + if usesBabel(jsFile) + babelConfig.dist.files.push({ + src: [jsFile] + dest: jsFile + }) + grunt.initConfig pkg: grunt.file.readJSON('package.json') @@ -148,6 +163,8 @@ module.exports = (grunt) -> docsOutputDir: 'docs/output' + babel: babelConfig + coffee: coffeeConfig less: lessConfig @@ -229,7 +246,7 @@ module.exports = (grunt) -> stderr: false failOnError: false - grunt.registerTask('compile', ['coffee', 'prebuild-less', 'cson', 'peg']) + grunt.registerTask('compile', ['babel', 'coffee', 'prebuild-less', 'cson', 'peg']) grunt.registerTask('lint', ['coffeelint', 'csslint', 'lesslint']) grunt.registerTask('test', ['shell:kill-atom', 'run-specs']) diff --git a/build/lib/uses-babel.coffee b/build/lib/uses-babel.coffee new file mode 100644 index 000000000..e7b9050a1 --- /dev/null +++ b/build/lib/uses-babel.coffee @@ -0,0 +1,18 @@ +fs = require 'fs' + +BABEL_PREFIXES = [ + "'use babel'" + '"use babel"' + '/** use babel */' +].map(Buffer) + +PREFIX_LENGTH = Math.max(BABEL_PREFIXES.map((prefix) -> prefix.length)...) + +buffer = Buffer(PREFIX_LENGTH) + +module.exports = (filename) -> + file = fs.openSync(filename, 'r') + fs.readSync(file, buffer, 0, PREFIX_LENGTH) + fs.closeSync(file) + BABEL_PREFIXES.some (prefix) -> + prefix.equals(buffer.slice(0, prefix.length)) diff --git a/build/package.json b/build/package.json index cb41bb0a4..07e9fa7ad 100644 --- a/build/package.json +++ b/build/package.json @@ -12,8 +12,9 @@ "formidable": "~1.0.14", "fs-plus": "2.x", "github-releases": "~0.2.0", + "glob": "^5.0.14", "grunt": "~0.4.1", - "grunt-electron-installer": "^0.37.0", + "grunt-babel": "^5.0.1", "grunt-cli": "~0.1.9", "grunt-coffeelint": "git+https://github.com/atom/grunt-coffeelint.git#cfb99aa99811d52687969532bd5a98011ed95bfe", "grunt-contrib-coffee": "~0.12.0", @@ -21,6 +22,7 @@ "grunt-contrib-less": "~0.8.0", "grunt-cson": "0.14.0", "grunt-download-atom-shell": "~0.14.0", + "grunt-electron-installer": "^0.37.0", "grunt-lesslint": "0.17.0", "grunt-peg": "~1.1.0", "grunt-shell": "~0.3.1",