Skip to content

Commit

Permalink
Run client-side JS through Babel if requested
Browse files Browse the repository at this point in the history
  • Loading branch information
raphinesse committed Oct 12, 2019
1 parent 74fdba8 commit f6ec0d0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
29 changes: 23 additions & 6 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
});
Expand All @@ -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)
Expand Down
22 changes: 21 additions & 1 deletion build-tools/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* under the License.
*/

const babel = require('@babel/core');
const execa = require('execa');
const bundle = require('./bundle');
const scripts = require('./scripts');
Expand All @@ -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);
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions test/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f6ec0d0

Please sign in to comment.