Blog>
Snippets

Setting Up Babel with ESM for Redux Projects

Provide a code snippet to show how to configure Babel in a project using Redux, ensuring compatibility with ESM syntax.
// babel.config.js
module.exports = {
    presets: [
        ['@babel/preset-env', {
            targets: { esmodules: true }, // aiming for environments that support ES Modules
            modules: false // preserve ES modules for tree shaking in webpack and other bundlers
        }],
        '@babel/preset-react' // if using React with Redux
    ],
    plugins: [
        '@babel/plugin-proposal-class-properties', // for class property syntax
        '@babel/plugin-proposal-object-rest-spread', // for object spread
        '@babel/plugin-transform-runtime' // to enable the re-use of Babel's injected helper code to save on codesize
    ]
}
This is configuration for Babel. It sets up Babel with the necessary presets and plugins for a project using Redux with ESM syntax, ensuring it targets environments that support ESModules and preserves modules for bundlers to allow for tree shaking. The preset includes '@babel/preset-env' for compiling modern JavaScript, '@babel/preset-react' if the project uses React, and various plugins for supporting class properties and object rest spread, as well as optimizing runtime helpers.