Can I Get Next.Js to exclude .babelrc during build?
Asked Answered
S

3

5

I'd like to not include .babelrc during build since SWC is disabled as a replacement for Babel. I only need .babelrc for a plugin for dev testing purposes that is not supported by SWC yet. I am told to check the doc about ignored compiler options but the page is down, and I could not find a solution from the nextjs document on disabling SWC and its feedback thread.

Disabling SWC as replacement for Babel Ignored Compiler Options Next.js page down

Selfhood answered 21/3, 2022 at 21:1 Comment(0)
A
6

Super hacky, but you could modify the build script in package.json to temporarily rename the config file before the build then restore it after:

{

  "scripts": {
    "dev": "next dev",
    "build": "mv .babelrc .babel_ && next build; mv .babel_ .babelrc",
    "start": "next start",
    "lint": "next lint"
  }

}

It's not a cross-platform solution, however.

Anthropo answered 22/3, 2022 at 1:36 Comment(3)
Will this ignore code coverage during the build?Chatty
But when you run npm run dev you got the same issue?Chatty
@MenaiAlaEddine-Aladdin I don't use Babel, so I don't the know how this workaround affects your situation. You might get better answers over at Next's discussion forum.Anthropo
L
3

I think you can add .babelrc to .gitignore, and it won't be used during build.

Liquidambar answered 22/3, 2022 at 0:7 Comment(2)
I still want to commit .babelrc to my git repo thoughSelfhood
@EdenChan for this specific case, then i think you can use Mark's solutionLiquidambar
E
0

If you use Babel in package.json then Next.js will not be able to find Babel config and will use SWC compiler.

https://babeljs.io/docs/configuration#packagejson

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

With the above configuration, you don't need to install any extra plugins for SWC and Babel.

Happy Coding!

Euhemerize answered 1/8 at 5:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.