Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rollup + React not compiling JSX

I'm trying to use Rollup + React but I'm encounting an error when rollup encounters JSX.

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

I have a repo that triggers the error. All documentation/examples I've found using Rollup + React don't use the latest Babel so it might have something to do with 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"]
}
like image 625
Brad Woods Avatar asked Oct 19 '18 00:10

Brad Woods


People also ask

Does JSX need to be compiled?

React uses a special syntax that resembles HTML to declare components. This markup, called JSX, is embedded in the component JavaScript and needs to be compiled to JavaScript before it's usable by the browser.

How does JSX get compiled?

We need transpilers (a compiler that translates one form of syntax into another) like Babel or TypeScript to compile JSX into a browser-compatible version. This occurs during the build process, so the browser will never know JSX was present in the first place.

Does JSX compile to JS?

JSX performs optimization while compiling the source code to JavaScript. The generated code runs faster than an equivalent code written directly in JavaScript.

What is rollup in react?

Rollup is a module bundler for JavaScript which compiles small pieces of code into something larger and more complex, such as a library or application.

How to create a react app with rollup?

Once Rollup is installed, create your React application manually by creating a new folder and name it react-rollup-test : Inside the directory, create a new package.json file by running the npm init command. You can go with the default settings offered by the command: Once done, let’s install the required packages for your React app.

Does rollup + react use the latest Babel?

I have a repo that triggers the error. All documentation/examples I've found using Rollup + React don't use the latest Babel so it might have something to do with Babel.

Does react 17 support the JSX transform?

Although React 17 doesn’t contain new features, it will provide support for a new version of the JSX transform. In this post, we will describe what it is and how to try it. What’s a JSX Transform?

Does rollup work with React-Redux?

After adding react-redux to investigate a reported but with context, I discoverted that Rollup has issues with redux. reduxjs/react-redux#643 Saving before upgrading rollup. For React v#16.2.0 the lib file name is slightly different ( react.js -> index.js)


1 Answers

The solution to this is two swap the order of 2 of the plugins

from:

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

to:

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

Thanks vladshcherbin.

like image 127
Brad Woods Avatar answered Sep 17 '22 12:09

Brad Woods