babeljsrollupbabel-preset-envnullish-coalescing

Why is my nullish-coalascing operators not transpiled by the rollup plugin for babel?


I have a svelte project that I would like to transpile nullish-coalascing operators (es2020) to es5 using the rollup plugin @rollup/plugin-babel which is enabled by default in @babel/preset-env since babel v7.6.3.

However in my project it does not work. I have a nullish-coalascing operator in a script tag in a component like this:

<script>
    let count = 0;
    let aCool;
    let canio;
    const increment = () => {
        if (count % 6 === 0) {
            canio = 1;
        }
        count += 1;
        aCool = canio ?? 0;
    };

    console.log('aCool', aCool);
</script>

A minimal svelte project can be found here.

Babel seems to run and add the polyfills from core-js but it does nothing with the ?? operator. Why is it not transpiled?


Solution

  • I found out the reason why it does not work is because I forgot to set .svelte as a file extension that Babel should transpile in the rollup plugin hence it doesn't even look at the svelte code. Thanks @JLHwung!!