diff --git a/Gruntfile.js b/Gruntfile.js index 3f8ae47ff..7479e0402 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -25,12 +25,28 @@ module.exports = function (grunt) { grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), compile: { - 'android': {}, - 'ios': {}, - 'osx': {}, - 'windows': { useWindowsLineEndings: true }, - 'browser': {}, - 'electron': {} + android: { + targets: 'Android >= 4.4, ChromeAndroid >= 30' + }, + ios: { + targets: 'iOS >= 9' + }, + osx: { + // TODO: add actual targets based on platform support + // {} means transform any ES2015+ code to ES5 + targets: {} + }, + windows: { + targets: 'Explorer >= 11', + useWindowsLineEndings: true + }, + browser: { + // {} means transform any ES2015+ code to ES5 + targets: {} + }, + electron: { + targets: 'Electron >= 4' + } }, clean: ['pkg'] }); @@ -45,6 +61,7 @@ module.exports = function (grunt) { build({ platformName: this.target, + babel: { targets: this.data.targets }, platformVersion: grunt.option('platformVersion') || require(platformPkgPath).version, extraModules: collectModules(platformModulesPath) diff --git a/build-tools/build.js b/build-tools/build.js index 0b6fc8797..db0188c78 100644 --- a/build-tools/build.js +++ b/build-tools/build.js @@ -22,6 +22,7 @@ const bundle = require('./bundle'); const scripts = require('./scripts'); const modules = require('./modules'); const { pkgRoot } = require('./common'); +const babel = require('@babel/core'); module.exports = function build (userConfig) { const config = Object.assign({ preprocess: x => x }, userConfig); @@ -34,9 +35,24 @@ module.exports = function build (userConfig) { .then(([ scripts, modules, commitId ]) => { Object.assign(config, { commitId }); return bundle(scripts, modules, config); - }); + }) + .then(babelTransform(config)); }; function getCommitId () { return execa.stdout('git', ['rev-parse', 'HEAD'], { cwd: pkgRoot }); } + +function babelTransform (config) { + const targets = (config.babel || {}).targets; + if (!targets) return x => x; + + const babelOpts = { + retainLines: true, + presets: [ + ['@babel/preset-env', { targets }] + ] + }; + return inputCode => babel.transformAsync(inputCode, babelOpts) + .then(result => result.code); +} diff --git a/package.json b/package.json index 9dd7aa12d..4f6bd14ff 100644 --- a/package.json +++ b/package.json @@ -69,6 +69,8 @@ } ], "dependencies": { + "@babel/core": "^7.6.4", + "@babel/preset-env": "^7.6.3", "execa": "^1.0.0", "fs-extra": "^8.0.1", "globby": "^9.2.0"