# 自定义Babel配置

Examples

  • Customizing babel configuration

Next.js includes the next/babelpreset to your app, which includes everything needed to compile React applications and server-side code. But if you want to extend the default Babel configs, it's also possible.

To start, you only need to define a .babelrcfile (or babel.config.js) at the top of your app. If such a file is found, it will be considered as the source of truth, and therefore it needs to define what Next.js needs as well, which is the next/babelpreset.

Here's an example .babelrcfile:

{
  "presets": ["next/babel"],
  "plugins": []
}

You can take a look at this file to learn about the presets included by next/babel.

To add presets/plugins without configuring them, you can do it this way:

{
  "presets": ["next/babel"],
  "plugins": ["@babel/plugin-proposal-do-expressions"]
}

To add presets/plugins with custom configuration, do it on the next/babelpreset like so:

{
  "presets": [
    [
      "next/babel",
      {
        "preset-env": {},
        "transform-runtime": {},
        "styled-jsx": {},
        "class-properties": {}
      }
    ]
  ],
  "plugins": []
}

To learn more about the available options for each config, visit their documentation site.

Next.js uses the currentNode.js version for server-side compilations.

The modulesoption on "preset-env"should be kept to false, otherwise webpack code splitting is turned off.

Last Updated: 5/13/2023, 8:55:38 PM