From f6ec0d05351db89dbfc510148b3fdf4c89e647b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20von=20der=20Gr=C3=BCn?= Date: Thu, 18 Jul 2019 18:51:53 +0200 Subject: [PATCH] Run client-side JS through Babel if requested --- Gruntfile.js | 29 +++++++++++++++++++++++------ build-tools/build.js | 22 +++++++++++++++++++++- package.json | 2 ++ test/build.js | 2 ++ 4 files changed, 48 insertions(+), 7 deletions(-) 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..d3c44eb44 100644 --- a/build-tools/build.js +++ b/build-tools/build.js @@ -17,6 +17,7 @@ * under the License. */ +const babel = require('@babel/core'); const execa = require('execa'); const bundle = require('./bundle'); const scripts = require('./scripts'); @@ -34,9 +35,28 @@ 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, + // Do not force strict mode as that breaks some platforms + modules: false + }] + ] + }; + 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" diff --git a/test/build.js b/test/build.js index 5a54eb073..ba8bd0e56 100755 --- a/test/build.js +++ b/test/build.js @@ -25,6 +25,8 @@ function buildCordovaJsTestBundle (bundlePath) { platformName: 'test', platformVersion: 'N/A', extraModules: collectTestBuildModules(), + // {} means transform any ES2015+ code to ES5 + babel: { targets: {} }, preprocess (f) { // Do not instrument our test dummies if (f.path.includes('src/legacy-exec/test/')) return f;