Hadi
Hadi

Reputation: 46

React component library , Vite builds the css file but it has no effect on component

I want to create a React Component library with vite, I have a component.tsx and style.css file. The problem is that the styles are working in dev mode and when vite build the project , a css file will generate in dist folder then I run npm link to use it in a test project but when add to test project css doesn't change anything. I test it in next and reactJs projects.

I tried css module file but it didn't fix. my file structure is like this :

src -> components -> (index.ts , component.tsx , style.css)

i use index file to import css file and export component and its type

export type {ItemsType} from "./Component" ;
export {Component} from "./Component"
import "./inputs.css";

vite config.ts

import { resolve } from 'path'
import react from '@vitejs/plugin-react'
import { defineConfig } from 'vite'
import dts from 'vite-plugin-dts'
import EsLint from 'vite-plugin-linter'
import tsConfigPaths from 'vite-tsconfig-paths'
const { EsLinter, linterPlugin } = EsLint
import * as packageJson from './package.json'


// https://vitejs.dev/config/
export default defineConfig((configEnv) => ({
  plugins: [
    react(),
    tsConfigPaths(),
    linterPlugin({
      include: ['./src}/**/*.{ts,tsx}'],
      linters: [new EsLinter({ configEnv })],
    }),
    dts({
      include: ['src/components/'],
    }),
],
  build: {
    lib: {
      entry: resolve('src', 'components/index.ts'),
      name: 'ReactComponent',
      formats: ['es', 'umd'],
      fileName: (format) => `react-component.${format}.js`,
    },
    rollupOptions: {
      external: [...Object.keys(packageJson.peerDependencies)],
    },
  }
}))

Upvotes: 1

Views: 3086

Answers (1)

ultegro778
ultegro778

Reputation: 29

does it build file called css file in dist folder? if so, you have declared key in package.json like this:

....
"dist/style.css": "dist/style.css"
....

in my case css file called style.css

and in another project you have to import the style.css

import { Component } from 'my-library';
import 'my-library/dist/style.css';

Upvotes: 0

Related Questions