egoist / tsup

The simplest and fastest way to bundle your TypeScript libraries.
https://tsup.egoist.dev
MIT License
8.46k stars 209 forks source link

Is it possible to build a react component with css.module ? #1101

Closed h2ck4u closed 3 months ago

h2ck4u commented 3 months ago

hello. I'm creating a react library using tsup in a turborepo environment.

I checked that the bundle result contains the classname of the cssModule as an object.

// src/atom/input/input.module.css
var input_default = {
  warn: "input_warn",
  invalid: "input_invalid",
  disabled: "input_disabled",
  md: "input_md",
  sm: "input_sm",
  lg: "input_lg",
};

I deployed the bundled library as an npm package and imported it in MyApp to use it.

import MyInput from '@myreact-lib/input''

const com = () => {
  return <MyInput size="lg"/>
}

//render result
<input className="input_lg"> </input>

When I checked the rendered component, the style was not applied.

This is because the stylesheet included in input.module.css was not included in the bundle.

Is there a way to include styles when deploying a React component library using TSUP? Please help me.

// this is my tsup.config.js
export default defineConfig({
  dts: true,
  sourcemap: false,
  clean: true,
  treeshake: true,
  skipNodeModulesBundle: false,
  format: ['esm', 'cjs'],
  external: ['react', 'react-dom'],
  loader: {
    '.css': 'default'
  },
  tsconfig: './tsconfig.json',
});

Upvote & Fund

Fund with Polar

h2ck4u commented 3 months ago

i solved with tsup.config.js

i change loader 'css': 'default' > 'css: 'copy',

  loader: {
    '.css': 'default'
  },
mayank1513 commented 2 months ago

You can also check out this repo - https://github.com/react18-tools/turborepo-template/

gopal-virtual commented 1 month ago

while @h2ck4u's solution works, but it

on the other hand local-css option does solve the above two problem, but not automatically imported inside the js file, so I've to manually import it in consumer app.

mayank1513 commented 1 month ago

I have built a plugin for this - https://github.com/react18-tools/esbuild-plugin-react18-css/

Example - https://github.com/react18-tools/turborepo-template/

gopal-virtual commented 4 weeks ago

@mayank1513, appreciate your effort. But your library still has the last problem I mentioned (i.e. manual import for css is needed). I am looking for css injection in JS.

mayank1513 commented 4 weeks ago

@mayank1513, appreciate your effort. But your library still has the last problem I mentioned (i.e. manual import for css is needed). I am looking for css injection in JS.

Noted! Please create a feature request. I think this is very much doable and in the next version we could try to have this feature as well.