Rollup and React are having trouble compiling JSX code

While attempting to incorporate Rollup with React, I've come across an issue when Rollup encounters JSX.

Unexpected token... export default () => <p>M...

I've created a repository that triggers this error. Most of the documentation and examples I could find on using Rollup with React don't seem to be updated with the latest version of Babel, which makes me think it might be related to Babel.

rollup.config.js:

import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import babel from 'rollup-plugin-babel';
import pkg from './package.json';

export default [{
        input: 'src/index.js',
        output: {
        name: 'index',
        file: pkg.main,
        format: 'umd'
    },
    plugins: [
        resolve(),
        commonjs(),
        babel({ 
            exclude: 'node_modules/**',
            presets: ['@babel/env', '@babel/preset-react']
        })
    ],
    external: [
        'react',
        'prop-types',
    ],
    globals: {
        react: "React"
    }
},
];

.babelrc:

{
  "presets": [
  ["@babel/env", { "modules": false }], "@babel/preset-react"]
}

Answer №1

To solve this issue, you will need to adjust the order of two plugins.

The original configuration is as follows:

plugins: [
    resolve(),
    commonjs(),
    babel({ 
        exclude: 'node_modules/**',
        presets: ['@babel/env', '@babel/preset-react']
    })
],

The updated configuration should look like this:

plugins: [
    resolve(),
    babel({ 
        exclude: 'node_modules/**',
        presets: ['@babel/env', '@babel/preset-react']
    }),
    commonjs()
],

Credit goes to vladshcherbin.

Answer №2

If you're encountering this problem, the recommendation is to place commonjs before babel as stated in the documentation. You can resolve the issue by adding the following

commonjs({ include: /node_modules/ })
.

This link provides a detailed explanation: https://github.com/rollup/plugins/issues/805#issuecomment-779902868

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

Tips for executing an npm command within a C# class library

I am currently developing a project in a class library. The main objective of this project is to execute a JavaScript project using an npm command through a method call in C#. The npm command to run the JavaScript project is: npm start The JavaScript ...

A guide on adding a href link to a button within a stepper in a React Material-UI

I am looking to create a functionality where, upon clicking the "Book now" button at the end of a Stepper, the user is redirected to another page instead of seeing the message "Thank you for your interest". Is it possible to achieve this by adding an href= ...

Tips for Customizing the Width of Your Material UI Alert Bar

At the moment, I have refrained from using any additional styling or .css files on my webpage. The width of the Alert element currently spans across the entire page. Despite my attempts to specify the width in the code snippet below, no noticeable change ...

Is there any way to deactivate the saved query in react-admin without having to develop a new component?

The latest version of react-admin, version 4, introduced a new feature that allows saving filters. I'm curious about how to disable this functionality without having to create an additional filter button. https://i.stack.imgur.com/uTrUe.gif ...

MUI React - Issue with changing SvgIcon color using override

Currently, I am utilizing MUI version 5 within a React project. My objective is to customize the icon color for the Error Alert by using General StylesOverrides. Although I successfully altered the color on the Alert component, I am unable to discover a ...

Unpacking the information in React

My goal is to destructure coinsData so I can access the id globally and iterate through the data elsewhere. However, I am facing an issue with TypeScript on exporting CoinProvider: Type '({ children }: { children?: ReactNode; }) => void' is no ...

Tips for selecting a specific input field in a ReactJS component with multiple inputs

I am currently developing a ReactJS application where I have implemented a component responsible for generating an HTML table. Within this component, I utilize Map to create rows using a child component. These rows contain multiple input fields that users ...

Alert: Refs cannot be assigned to function components. Any attempt to access this ref will result in failure. Have you considered using React.forwardRef()? displayed in the @Mui Component

Is anyone familiar with this particular component? <Controller control={control} {...register("DOB", {})} render={({ field }) => ( <LocalizationProvider dateAdapter={AdapterDayjs}> <DatePicker ...

Creating a Modal using Typescript with NextJS

Currently, I'm working on creating a modal within my app using NextJS with Typescript. Unfortunately, I've been struggling to eliminate the warning associated with my modal selector. Can someone provide guidance on how to properly type this? cons ...

Tips for customizing the time selector in material-ui-time-picker

Is there a way to enable keyboard input control for the material-ui-time-picker? Currently, it only allows editing when clicking on the clock interface. Here is a snippet of my code: import React, { Component } from "react"; import { TimePicker } from " ...

When I try to start the editor with HTML content using the convertFromHTML method, I encounter an error stating that

I am encountering an error when trying to initialize my Editor state with a HTML markup. at renderToString (/home/al/Documents/node/admin-next/node_modules/react-dom/cjs/react-dom-server.node.development.js:3988:27) at render (/home/al/Documents/node/admin ...

Utilizing Material-UI Table in a class component with tailored pagination functionalities

Is there a way to implement the MATERIAL-UI table on a class component? Most tutorials and official documentation show it as a functional component, leading to difficulties when dealing with react hooks in a class component. I am looking for guidance on h ...

dotenv fails to execute when using root privileges

I have a node application developed using create-react-app. Initially, I set it up to run on port 80 by adding a .env file with the following content: PORT=80 However, when attempting to start the app using npm start, I encountered an error: ? A ...

What are the reasons behind the difficulty in deploying my Vite app on Vercel?

Currently, I am working on developing a social media app with Vite. However, I am facing challenges when trying to deploy it. I have included my vite.config.js file below for better insight into the issue at hand. The main issue is that while the ./ route ...

What is the process for retrieving the component along with the data?

As I work on compiling a list of div elements that require content from my firebase cloud storage, I find myself unsure about how to effectively run the code that retrieves data from firebase and returns it in the form of MyNotes. Consider the following: ...

How to ensure the backdrop fully covers the popover appearance

How can I make the backdrop loading screen cover the popover as well? Currently, the loading screen only covers the main page but not the popover. I tried using the following code but it didn't work: import * as React from 'react'; import Po ...

The React Bootstrap modal refuses to fully close no matter how many times I click away or even on the close button

I am facing an issue with a modal in my React component. When I open the modal, it does not completely close when I try to click away or hit the close button. The backdrop disappears but the modal itself remains on the screen. (Screenshots attached for r ...

What steps can be taken to resolve the Metamask Failed to retrieve error?

I encountered a MetaMask - RPC Error: Failed to fetch after calling the contract.methods.ownerOf function 2500 times. Strangely, when I call the method about 200 times, there is no error. How can I resolve this issue? (ownerOf refers to etherscan contract ...

Running a child process within a React application

I'm currently in search of the best module to use for running a child process from within a React application. Here's what I need: I want a button that, when clicked, will execute "npm test" for my application and generate a report that can be r ...

Ways to return success state to component without the need to modify the store

I am currently working on a simple form in react-redux with the goal of adding a user to the database and displaying a success message upon successful registration. However, I am unsure of the most effective approach to achieve this. Here is what I have so ...