45. What are presets in Babel and how do you use them?

Presets in Babel are collections of plugins that are configured to perform specific transpilation tasks. A preset is the simplest way to configure Babel, allowing for the transformation of JavaScript code using predefined sets of plugins.

Popular presets:

  1. @babel/preset-env: Transforms modern JavaScript to ES5-compatible code, automatically adjusting configuration based on target browsers.
  2. @babel/preset-react: Transforms JSX and other React-specific syntax.
  3. @babel/preset-typescript: Transforms TypeScript code to JavaScript.

How to use presets:

  1. Installing presets:
npm install --save-dev @babel/preset-env
  1. Configuring the .babelrc file:
{ "presets": ["@babel/preset-env"] }
  1. Transpiling code with Babel:
npx babel src --out-dir lib

Presets simplify Babel configuration, allowing you to quickly start using modern JavaScript without the need to manually set up multiple plugins.

devFlipCards 2024

Do you accept cookies?

Cookies are small amounts of data saved locally on you device, which helps our website - it saves your settings like theme or language. It helps in adjusting ads and in traffic analysis. By using this site, you consent cookies usage.