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 d44ac89
Show file tree
Hide file tree
Showing 3 changed files with 42 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
18 changes: 17 additions & 1 deletion build-tools/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
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

0 comments on commit d44ac89

Please sign in to comment.