Search code examples
reactjsbabeljsrolluprollupjs

Rollup.js can not detect default exports when trying to build component library with React.js


I am trying to create component library with React.js, I am using Rollup.js for this. When I am building the app, I see this error in the console. It seems Rollup can not detect default exports, I tried to add a .babelrc file configuration but unfortunately it did not work. What am I doing wrong and how can this problem be solved?

enter image description here

//rollup.config.js

import babel from 'rollup-plugin-babel';
import resolve from '@rollup/plugin-node-resolve';
import external from 'rollup-plugin-peer-deps-external';
import { terser } from 'rollup-plugin-terser';
import postcss from 'rollup-plugin-postcss';
import commonjs from '@rollup/plugin-commonjs';


export default [
    {
        input: './src/index.js',
        output: [
            {
                file: 'dist/index.js',
                format: 'cjs',
            },
            {
                file: 'dist/index.es.js',
                format: 'es',
                exports: 'named',
            }
        ],
        plugins: [
            postcss({
                plugins: [],
                minimize: true,
            }),
            babel({
                exclude: "node_modules/**",
                presets: ["@babel/preset-react"],
            }),
            external(),
            resolve(),
            terser(),
        ]
    }
];

//package.json

{
  "name": "test",
  "version": "0.1.0",
  "private": false,
  "dependencies": {
    "@babel/preset-stage-1": "^7.8.3",
    "@rollup/plugin-commonjs": "^21.0.3",
    "@rollup/plugin-replace": "^2.4.2",
    "@testing-library/jest-dom": "^5.16.3",
    "@testing-library/react": "^12.1.4",
    "@testing-library/user-event": "^13.5.0",
    "babel-preset-latest": "^6.24.1",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "5.0.0",
    "react-transform-hmr": "^1.0.4",
    "typescript": "^4.6.3",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "tsc",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "storybook": "start-storybook -p 6006 -s public",
    "build-storybook": "build-storybook -s public",
    "build-lib": "rollup -c"
  },
  "files": [
    "dist"
  ],
  "main": "dist/index.js",
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ],
    "overrides": [
      {
        "files": [
          "**/*.stories.*"
        ],
        "rules": {
          "import/no-anonymous-default-export": "off"
        }
      }
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@babel/plugin-external-helpers": "^7.16.7",
    "@babel/preset-react": "^7.16.7",
    "@rollup/plugin-node-resolve": "^11.2.1",
    "@storybook/addon-actions": "^6.4.19",
    "@storybook/addon-essentials": "^6.4.19",
    "@storybook/addon-interactions": "^6.4.19",
    "@storybook/addon-links": "^6.4.19",
    "@storybook/builder-webpack5": "^6.4.19",
    "@storybook/manager-webpack5": "^6.4.19",
    "@storybook/node-logger": "^6.4.19",
    "@storybook/preset-create-react-app": "^4.1.0",
    "@storybook/react": "^6.4.19",
    "@storybook/testing-library": "^0.0.9",
    "rollup": "^2.70.1",
    "rollup-plugin-babel": "^4.4.0",
    "rollup-plugin-node-resolve": "^5.2.0",
    "rollup-plugin-peer-deps-external": "^2.2.4",
    "rollup-plugin-postcss": "^4.0.2",
    "semantic-release": "^19.0.2",
    "webpack": "^5.70.0"
  }
}


Solution

  • The error is telling you what you need to know. To explain what that means, there is no export default ... in react-dom (you can verify this by looking at the source code).

    There are several ways to fix this:

    1. Play around with some of the @rollup/plugin-commonjs settings. Sorry I can't tell you which one will work, maybe start with defaultIsModuleExports.

    2. Import the method that you need. With react-dom, you're likely only using render, so import it like this: import { render } from 'react-dom'

    3. Import everything and alias as ReactDOM: import * as ReactDOM from 'react-dom'