Skip to content

Commit

Permalink
new: Transform async/await to promises for browsers.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Oct 16, 2020
1 parent daf961e commit 81c99f0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"@rollup/plugin-babel": "^5.2.1",
"@rollup/plugin-commonjs": "^15.1.0",
"@rollup/plugin-node-resolve": "^9.0.0",
"babel-plugin-transform-async-to-promises": "^0.8.15",
"babel-plugin-transform-dev": "^2.0.1",
"builtin-modules": "^3.1.0",
"execa": "^4.0.3",
Expand Down
14 changes: 11 additions & 3 deletions src/babel/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ export function getBabelInputConfig(
// The output config does all the transformation and downleveling through the preset-env.
// This is handled per output since we need to configure based on target + format combinations.
export function getBabelOutputConfig(
buildUnit: BuildUnit,
{ format, platform, support }: BuildUnit,
features: FeatureFlags,
): ConfigStructure {
const plugins: PluginItem[] = [];
const presets: PluginItem[] = [];
const isFuture = support !== 'legacy' && support !== 'stable';

// ENVIRONMENT

const { format, platform, support } = buildUnit;
const envOptions: PresetEnvOptions = {
// Prefer spec compliance over speed
spec: true,
Expand All @@ -157,13 +157,21 @@ export function getBabelOutputConfig(

// Use `Object.assign` when available
// https://babeljs.io/docs/en/babel-plugin-transform-destructuring#usebuiltins
if (buildUnit.support !== 'legacy' && buildUnit.support !== 'stable') {
if (isFuture) {
plugins.push(
['@babel/plugin-transform-destructuring', { useBuiltIns: true }],
['@babel/plugin-proposal-object-rest-spread', { useBuiltIns: true }],
);
}

// Transform async/await into Promises for browsers
if (platform === 'browser') {
plugins.push([
'babel-plugin-transform-async-to-promises',
{ inlineHelpers: true, target: isFuture ? 'es6' : 'es5' },
]);
}

// Support `__DEV__` shortcuts
plugins.push(['babel-plugin-transform-dev', { evaluate: false }]);

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2335,6 +2335,11 @@ babel-plugin-jest-hoist@^26.5.0:
"@types/babel__core" "^7.0.0"
"@types/babel__traverse" "^7.0.6"

babel-plugin-transform-async-to-promises@^0.8.15:
version "0.8.15"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-async-to-promises/-/babel-plugin-transform-async-to-promises-0.8.15.tgz#13b6d8ef13676b4e3c576d3600b85344bb1ba346"
integrity sha512-fDXP68ZqcinZO2WCiimCL9zhGjGXOnn3D33zvbh+yheZ/qOrNVVDDIBtAaM3Faz8TRvQzHiRKsu3hfrBAhEncQ==

babel-plugin-transform-dev@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-dev/-/babel-plugin-transform-dev-2.0.1.tgz#fec5bbfb6b9576cd8413df5bd0ae7aca32b0a2d4"
Expand Down

0 comments on commit 81c99f0

Please sign in to comment.