<table>
<tr><th>Compatible Runners</th><td>

- [Gulp](05_Packages/02_crafty-runner-gulp.md)
- [Webpack](05_Packages/02_crafty-runner-webpack.md)

</td></tr>
<tr><th>Test Runners</th><td>

- [Jest](05_Packages/05_crafty-preset-jest.md)
- [Vitest](05_Packages/05_crafty-preset-vitest)

</td></tr>
</table>

[TOC]

## Description

**Babel** is the leading tool to compile EcmaScript 2015+ to EcmaScript 5, combined with **ESLint** to lint your code, you get the best preset to get started.

## Features

`@swissquote/crafty-preset-babel` is able to configure **Babel** with **Webpack**. This preset also supports **Gulp** but in this case it concatenates and minifies the files, it doesn't resolve imports.

[Our Babel preset](05_Packages/10_babel-preset-swissquote.md)

[Read more](./JavaScript_Features.md)

## Linting

In `@swissquote/crafty-preset-babel` JavaScript is linted with **ESLint**, a powerful linter that supports plugins, our configuration follows the Swissquote JavaScript Guideline through our `@swissquote/crafty-preset-eslint` preset.

[Read more](../05_crafty-preset-eslint/JavaScript_Linting.md)

## Installation

```bash
npm install @swissquote/crafty-preset-babel --save
```

```javascript
module.exports = {
  presets: [
    "@swissquote/crafty-preset-babel",
    "@swissquote/crafty-runner-webpack", // optional
    "@swissquote/crafty-runner-gulp", // optional
  ],
  js: {
    app: {
      runner: "webpack", // Webpack or Gulp (optional if you have a single runner defined)
      source: "js/app.js",
    },
  },
};
```

## Usage

### With Webpack

Both runners have the same features in regards to Babel support.
They will resolve your modules recursively and bundle them in one file or more if you do some code-splitting.

#### JavaScript External assets

By default, all bundlers include all external dependencies in the final bundle, this works fine for applications, but if you wish to build a multi-tenant application or a library, you don't wish to include all dependencies, because you'll end up with the same dependency duplicated.

The `externals` option allows you to define a list of libraries that are provided and should not be embedded in the build, here is an example :

```javascript
module.exports = {
    ...
    // patterns are strings or globs
    externals: ["react", "react-dom", "squp", "squp/**"],
    ...
    js: {
        app: {
            // You can provide more items here, they will be merged with the main list for this bundle
            externals: ["my-plugin", "my-plugin/**"]
            ...
        }
    }
    ...
}
```

In this example `react`, `react-dom` and all modules starting with `squp/` will be treated as external

### With Gulp

Gulp will not bundle your files like Webpack does, instead it will generate one output file per input file.
This is useful if you are creating a library as it's the role of the final application to tree-shake what it doesn't need from your library.

Tree-shaking is powerful but is sub-optimal on big files as some code patterns are recognized as side-effects and thus aren't removed from your bundle even if they aren't used.

## Usage with Jest and Vitest

If you include `crafty-preset-babel` with either `crafty-preset-jest` or
`crafty-preset-vitest`, running `crafty test` will add support for `.js` and
`.jsx` source and test files.

When `crafty-preset-typescript` is also active, Babel keeps owning only the
`.js` and `.jsx` test path. TypeScript files stay on the TypeScript and Vitest
transform path.

## Configuration

### Bundle options

| Option   | Type    | Optional ? | Runner | Description                                                                                                        |
| -------- | ------- | ---------- | ------ | ------------------------------------------------------------------------------------------------------------------ |
| `concat` | Boolean | Yes        | Gulp   | This will merge all files together, outputting a single file. (This doesn't resolve imports, use Webpack for this) |

### Adding Babel plugins and presets

You can add, replace or remove plugins and add options to our default Babel configuration.
To see which plugins are already included, you can go to the [Swissquote Preset for Babel](05_Packages/10_babel-preset-swissquote.md) page.

```javascript
module.exports = {
  /**
   * Represents the extension point for Babel configuration
   * @param {Crafty} crafty - The instance of Crafty.
   * @param {Object} bundle - The bundle that is being prepared for build (name, input, source, destination)
   * @param {Object} babelConfig - The current Babel configuration
   */
  babel(crafty, bundle, babelConfig) {
    babelConfig.plugins.push(
      require.resolve("@babel/plugin-transform-property-literals"),
    );
  },
};
```

After you did `npm install --save-dev babel-plugin-transform-es5-property-mutators` before, Babel will now use this plugin as well in each run.

This method is called once per bundle, so you can customize each bundle's configuration differently.
