Change the file extension from .babelrc to .babelrc.js

I am using Material UI and aiming to decrease the bundle size by loading components on demand.

In my project, I have a Babel configuration in a .babelrc file.

The current .babelrc setup is as follows:

{
  "presets": ["@babel/preset-env", "@babel/react"],
  "plugins": [
    ["@babel/plugin-syntax-dynamic-import"],
    ["react-hot-loader/babel"],
    ["import", {
      "libraryName": "antd",
      "style": true
    }],
    [
      "@babel/plugin-transform-runtime",
      {
        "regenerator": true
      }
    ]
  ]
}

Now, I need to include the following additions:

const plugins = [
  [
    'babel-plugin-import',
    {
      'libraryName': '@material-ui/core',
      // Use "'libraryDirectory': ''," if your bundler does not support ES modules
      'libraryDirectory': 'esm',
      'camel2DashComponentName': false
    },
    'core'
  ],
  [
    'babel-plugin-import',
    {
      'libraryName': '@material-ui/icons',
      // Use "'libraryDirectory': ''," if your bundler does not support ES modules
      'libraryDirectory': 'esm',
      'camel2DashComponentName': false
    },
    'icons'
  ]
];

module.exports = {plugins};

How can I achieve this? The .babelrc seems to function differently than the .babelrc.js file.

Answer №1

According to the information provided at https://babeljs.io/docs/en/config-files:

To ensure compatibility, .babelrc is equivalent to .babelrc.json.

Therefore, the first script is in JSON format while the second one follows CommonJS.

My suggestion is to simply copy the plugins section from your second script and paste it inside the "plugins" section of the first one. Use a JSON validation tool to verify that the result is accurate, as mentioned in [1]

Additionally, I recommend using the .babelrc.cjs (CommonJS) format since it's easier to format with tools like Prettier and supports features that JSON does not, such as comments. For more details, refer to [2]

[1] .babelrc or .babelrc.json

    {
      "presets": ["@babel/preset-env", "@babel/react"],
      "plugins": [
        ["@babel/plugin-syntax-dynamic-import"],
        ["react-hot-loader/babel"],
        ["import", {
          "libraryName": "antd",
          "style": true
        }],
        [
          "@babel/plugin-transform-runtime",
          {
            "regenerator": true
          }
        ],
      [
        "babel-plugin-import",
        {
          "libraryName": "@material-ui/core",
          "libraryDirectory": "esm",
          "camel2DashComponentName": false
        },
        "core"
      ],
      [
        "babel-plugin-import",
        {
          "libraryName": "@material-ui/icons",
          "libraryDirectory": "esm",
          "camel2DashComponentName": false
        },
        "icons"
      ]
      ]
    }

[2] .babelrc.js or .baberc.cjs

{
  "presets": ["@babel/preset-env", "@babel/react"],
  "plugins": [
    ["@babel/plugin-syntax-dynamic-import"],
    ["react-hot-loader/babel"],
    ["import", {
      "libraryName": "antd",
      "style": true
    }],
    [
      "@babel/plugin-transform-runtime",
      {
        "regenerator": true
      }
    ]
  ]

const presets
const plugins = [
  [
    'babel-plugin-import',
    {
      'libraryName': '@material-ui/core',
      // Use "'libraryDirectory': ''," if your bundler does not support ES modules
      'libraryDirectory': 'esm',
      'camel2DashComponentName': false
    },
    'core'
  ],
  [
    'babel-plugin-import',
    {
      'libraryName': '@material-ui/icons',
      // Use "'libraryDirectory': ''," if your bundler does not support ES modules
      'libraryDirectory': 'esm',
      'camel2DashComponentName': false
    },
    'icons'
  ]
];

module.exports = {plugins};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating Flexnav Menu Buttons that automatically adjust their width based on the content within them

Is it possible to use flexnav without fixed width buttons? I would like all navigation items to only use the necessary width to display their text and padding. Currently, I have to divide 100% by the number of navigation items we have. This value is set w ...

Using NodeJS to perform asynchronous tasks with setImmediate while also incorporating private class

Today marks my first time experimenting with setImmediate. I've come to realize that it may not be able to run private class methods. Can someone shed some light on this? Why is that the case? Not Functioning Properly When trying to use a private cl ...

Error: Attempting to access property 'question' of an undefined value

Trying to render information from a local .json file using Javascript functions, I encountered an error in the console for const answer despite being defined. I temporarily commented it out to test the function, only to receive the same TypeError for quest ...

Confused about how to utilize the variable `${PORT}`?

Currently, I am following a tutorial in which the instructor inputs the following code snippet: let PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log('Server beating ...

The functionality to apply a color class to a navbar item when clicked is malfunctioning

I'm attempting to create a navigation bar using text that toggles the visibility of different divs. I want the selected option to appear in red, indicating it has been chosen, while deselecting any other options. I am new to JavaScript and thought add ...

Troubleshooting: Why is my Local Image not displaying in ReactJS

I am facing an issue with displaying images in my React application using an array of image paths. Below is the code snippet I have written: import React, { Component } from 'react'; class ItemsList extends Component { constructor() { ...

Integrate geographic data in GeoJSON format into a Leaflet map by using the Django template

In my Django view, I am fetching results of an SQL query and rendering it on the index.html page of my web map. The POST request successfully returns the acreage calculated from the SQL query to the page. I am also attempting to display the geojson data fr ...

Dynamic radio group validation with jQuery

Looking for assistance with validating a dynamically generated form that contains pairs of radio groups. I've made progress but hit a roadblock. The scenario is as follows: when the user selects the first radio in a pair, a hidden droplist should appe ...

`How to implement a dark mode feature in Tailwind CSS with Next.js and styled-jsx`

My website is created using Next.js and Tailwind CSS. I followed the default setup instructions to add them to my project. In order to style links without adding inline classes to each one, I also integrated styled-jsx-plugin-postcss along with styled-jsx ...

Browsing through items within arrays to evaluate their values

I am facing an issue with two arrays that consist of objects. The first array contains restaurant objects with attributes such as name and averagePrice, while the second array has price objects (cheap, medium, expensive) with properties like label, lowEnd, ...

Angular: The Process of Completely Updating a Model Object

Within my application, there is an object named eventData which acts as a singleton and is injected into multiple controllers through a resolve function. This eventData contains various sets of data such as drop down list values along with the main model. ...

Troubleshooting Problem with Bootstrap CSS Menu Box Format

I'm having trouble creating a simple menu for my Bootstrap site. What I want to achieve is something like this: https://i.sstatic.net/abZXC.png This is what I have accomplished so far: https://i.sstatic.net/JFVC2.png I've attempted to write th ...

Struggling to update a scope variable when utilizing Firebase's Facebook authentication with AngularJS

Hey there... I'm currently exploring AngularJS and attempting to implement Facebook's connect feature using Firebird. Almost everything is running smoothly - the connection to my Facebook account is successful, and the information is retrieved w ...

What is the best way to stop Quasar dropdown list from moving along with the page scroll?

I am facing an issue with my code while using Quasar (Vue 3.0). The code snippet in question is: <q-select filled v-model="model" :options="options" label="Filled" /> When the drop-down menu is open and I scroll the pag ...

Retrieve all users along with their respective posts, ensuring that each post is also accompanied by its corresponding comments in

In my Laravel project, I have set up Eloquent models for User, Post, and Comment. The relationships are as follows: User model public function posts(){ return $this->hasMany('App\Post'); } public function comments(){ return $t ...

Even with React.memo(), functional components continue to trigger re-renders

I am facing an issue with my button component that contains a button inside it. The button has a state isActive and a click function passed to it. Upon clicking the button, the isActive flag changes, triggering the app to fetch data accordingly. However, t ...

Encountering net::ERR_CONNECTION_RESET and experiencing issues with fetching data when attempting to send a post request

I am facing a React.js task that involves sending a POST request to the server. I need to trigger this POST request when a user clicks on the submit button. However, I keep encountering two specific errors: App.js:19 POST http://localhost:3001/ net::ERR_CO ...

Merge the date within every individual element

I am trying to combine two dates together. I thought my code was correct, as I first created the current year and then added 1 month to get to 2019. However, for some reason, the result is not displaying. function combineDates() { var currentDate = ...

JavaScript array increasing its length too soon

I am experiencing issues with the length of an array in a condition-based scenario. The problem I am facing is that the array does not increase at the expected time. To better illustrate my issue, I have created a Plunker with sample code: Upon clicking " ...

Tips for adjusting the position of an infowindow in ArcGIS

I have implemented the use of infowindow in arcgis to display certain information. https://i.sstatic.net/Dnpas.jpg Currently, the infowindow is appearing directly over my icon. Is there a way to adjust its position if it covers the icon? ...