Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run client-side JS through Babel if desired #213

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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