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"]
}
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.
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.
JSX performs optimization while compiling the source code to JavaScript. The generated code runs faster than an equivalent code written directly in JavaScript.
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.
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.
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.
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?
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With