Rollup library importing file with named and default export not working

Viewed 17

I have a rollup library that is trying to use Antd react components. Without external component libraries, everything works fine. If I add the component library (material ui complains of a similar issue), it complains about trying to import variables that are not exported while importing the component library.

rollup.config.js:

export default [
  {
    input: 'src/index.ts',
    output: [
      {
        file: packageJson.main,
        format: 'cjs',
        sourcemap: true,
        globals: {
          react: 'React',
          'react-dom': 'ReactDOM',
        },
        plugins: [visualizer({ open: useVisualizer })],
      },
      {
        file: packageJson.module,
        format: 'esm',
        sourcemap: true,
        globals: {
          react: 'React',
          'react-dom': 'ReactDOM',
        },
      },
    ],
    external: ['react', 'react-dom'],
    plugins: [
      resolve({
        preferBuiltins: true
      }),
      commonjs({
        transformMixedEsModules: true,
        requireReturnsDefault: 'auto',
        esmExternals: true,
        // dynamicRequireTargets: ['node_modules/antd/es/**/*.js']
      }),
      typescript({ tsconfig: './tsconfig.json' }),
      folder(),
      postcss({
        plugins: [],
      }),
      eslint(),
      terser(),
    ],
  },
  {
    input: './src/index.ts',
    output: [{ file: 'dist/index.d.ts', format: 'esm' }],
    plugins: [dts()],
    external: [/\.css$/],
  },
];

Error message: [!] Error: 'Group' is not exported by node_modules/antd/es/radio/radio.js, imported by node_modules/antd/es/calendar/Header.js

However, when we look at the exports from radio.js, 'Group' is clearly exported from radio/index.js from within the radio directory:

import Group from './group';
import InternalRadio from './radio';
import Button from './radioButton';
export { Button, Group };
var Radio = InternalRadio;
Radio.Button = Button;
Radio.Group = Group;
Radio.__ANT_RADIO = true;
export default Radio;

Header.js imports this like so: import { Button, Group } from '../radio';, so the import SHOULD be going to radio/index.js, however, it appears it is actually going to radio/radio.js instead. No amount of rollup configuration has made this change so far. Any ideas how to get this to resolve to index.js instead of radio.js?

1 Answers

Turns out the normal node-resolve plugin does not resolve "local" imports (i.e. file system paths instead of package paths). The solution here is to also use rollup-plugin-local-resolve, which does resolve these paths.

Related