0

I'm creating a simple component to publish on npm and using rollup to build. The issue is index.js has nothing but import 'react'

Here's rollup config

import typescript from '@rollup/plugin-typescript';
import postcss from 'rollup-plugin-postcss';
import { defineConfig } from 'rollup';

export default defineConfig({
  input: 'src/index.ts',
  output: {
    dir: 'dist',
    format: 'es',
    name: 'beyonderui',
  },
  external: ['react', 'react-dom'],
  plugins: [
    postcss({ include: '**/*.css', extract: true, minimize: true }),
    typescript({ tsconfig: 'tsconfig.json' }),
  ],
});

Here's typescript config

{
  "compilerOptions": {
    "jsx": "react",
    "allowSyntheticDefaultImports": true,
    "target": "ES2020",
    "module": "Es6",
    "declaration": true,
    "declarationDir": "dist",
    "strict": true,
    "esModuleInterop": true,
    "emitDeclarationOnly": false
  }
}

and the entry file index.ts code

export * from './components/modals/reactModal/Modal'

modal component code

import * as React from 'react';
import {
  CSSProperties,
  ComponentType,
  Dispatch,
  ReactNode,
  SetStateAction,
  useEffect,
  useMemo,
  useRef,
  useState,
} from 'react';
import styles from './css/Modal.module.css';
import animations from './css/ModalAnimations.module.css';

type PropsType = {
  children?: ReactNode;
  show: boolean;
  setShow: Dispatch<SetStateAction<boolean>>;
  modalStyles?: CSSProperties;
  overlayStyles?: CSSProperties;
  overlayOpacity?: number;
  closeButtonStyles?: CSSProperties;
  closeButtonVariant?: number;
  CustomCloseButton?: ComponentType<any> | null;
  animateIn?: string;
  animateOut?: string;
  animateDuration?: number;
  onOpen?: () => void;
  onOpenStart?: () => void;
  onOpenEnd?: () => void;
  onClose?: () => void;
  onCloseStart?: () => void;
  onCloseEnd?: () => void;
};

type AnimationObject = {
  in: string[];
  out: string[];
  init: string[];
};

export default function Modal({
  children,
  show = false,
  setShow = () => {},
  modalStyles = {},
  overlayStyles = {},
  overlayOpacity = 0.3,
  closeButtonStyles = {},
  closeButtonVariant = 1,
  CustomCloseButton = null,
  animateIn = 'fadeIn-down',
  animateOut = 'fadeOut-down',
  animateDuration = 300,
  onOpen = () => {
    console.log('opening');
  },
  onOpenStart = () => {
    console.log('open start');
  },
  onOpenEnd = () => {
    console.log('open end');
  },
  onClose = () => {
    console.log('closing');
  },
  onCloseStart = () => {
    console.log('closing start');
  },
  onCloseEnd = () => {
    console.log('closing end');
  },
}: PropsType) {
...

return (
 ...
 )
}

I dont usually deep dive into libraries so I don't know what wrong I'm doing here. I can't find the solution by searching anywhere.

Another problem I encountered is final bundled index.js does not have an import to css file index.css Its code is

import * as React from 'react';
import { useRef, useState, useMemo, useEffect } from 'react';

var styles = {
  modal: 'Modal-module_modal__IfFoo',
  closeButton: 'Modal-module_closeButton__jZuqK',
  closeButtonVariant1: 'Modal-module_closeButtonVariant1__BV5Hk',
  closeButtonVariant2: 'Modal-module_closeButtonVariant2__xVtJs',
  overlay: 'Modal-module_overlay__721Fw',
};

var animations = {
  fadeIn: 'ModalAnimations-module_fadeIn__mqkPF',
  fadeOut: 'ModalAnimations-module_fadeOut__8Pnhq',
  left: 'ModalAnimations-module_left__RvQS0',
  right: 'ModalAnimations-module_right__8kLcG',
  up: 'ModalAnimations-module_up__4dHbH',
  down: 'ModalAnimations-module_down__Wbvrq',
  zoomIn: 'ModalAnimations-module_zoomIn__yRVif',
  zoomOut: 'ModalAnimations-module_zoomOut__bQifz',
  slideIn: 'ModalAnimations-module_slideIn__ePxNr',
  slideOut: 'ModalAnimations-module_slideOut__qY2YX',
};
...

It should have a line import './index.css' when I add this manually it apply all the styles but rollup clear this line on every build.

7
  • Hello can we see Modal component code please ? Commented Apr 2, 2024 at 14:29
  • @LansanaDiomande hello yes I've added that now Commented Apr 2, 2024 at 14:36
  • And what happen if you try with this in the index.ts: import Modal from './components/modals/reactModal/Modal';export {Modal} Commented Apr 2, 2024 at 14:39
  • this worked, thanks a lot. So I have to explicitely import and export every component from now on Commented Apr 2, 2024 at 14:54
  • yes and i give you a compact solution Commented Apr 2, 2024 at 14:55

1 Answer 1

1
export * as Modal from "./components/modals/reactModal/Modal";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.