diff --git a/package.json b/package.json index 85fefe74f..874ddf736 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/babel/config.ts b/src/babel/config.ts index 59abce983..021a548e3 100644 --- a/src/babel/config.ts +++ b/src/babel/config.ts @@ -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, @@ -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 }]); diff --git a/yarn.lock b/yarn.lock index 4e6a00564..18f95d88d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"