rollup plugin babel removing constant declarations

15 Views Asked by At

A custom babel transformer is adding constant declaration but rollup/plugin-babel is removing it somehow.

Input greet.ts snippet

export async function greet(name: string) {
  console.log(await digest(null));
  const foo = fooCall();
  const bar = barCall(name);
  const b = toHex(foo, await digest(await foo), await bar);
  return b;
}

I am expecting following output.

async function greet(name) {
  const _digest = await digest(null);
  console.log(_digest);
  const foo = fooCall();
  const bar = barCall(name);
  const _foo = await foo,
    _digest2 = await digest(_foo),
    _bar = await bar;
  const b = toHex(foo, _digest2, _bar);
  return b;
}

This custom plugin was provided by one dev on github babel site and works fine standalone (babel cli) but something in rollup removes the constant declaration shown here:-

The problem:-

async function greet(name) {
  const _digest = await digest();
  console.log(_digest);
  const foo = fooCall();
  const bar = barCall();
  await foo;
    await digest();
    await bar;
  const b = toHex();
  return b;
}

rollup.config.mjs

import { babel } from '@rollup/plugin-babel';
import commonjs from '@rollup/plugin-commonjs';

const config = {
  input: 'src/index.js',
  output: {
    dir: 'dist',
    format: 'es',
    strict: false
  },
  plugins: [
    commonjs(),
    babel({
      babelHelpers: 'bundled',
      extensions: ['.ts', '.mjs', '.js']
    })
  ]
};

export default config;

babel.config.js

'use strict';
// @ts-check

/** @type {babel.ConfigFunction} */
module.exports = (api) => {
  return {
    presets: [
     
      [
        '@babel/preset-typescript',
        {
          allowDeclareFields: true
          // loose: true
        }
      ]
    ],
    plugins: [
       ['./babel.await-in-expr.mjs', {}]
    ]
  };
};
0

There are 0 best solutions below