Next.js Discord

Discord Forum

What is the best practice to move css in dist folder npmjs package

Answered
Anay-208 posted this in #help-forum
Open in Discord
I'm making a npmjs package for a react component. I need to include css in it. So what options do I have? Ts can't add css files in dist folder.
My options are to add mv command in build command, or are there any other good practices?

This is my globals.d.ts:
declare module '*.module.css' {
    const classes: { [key: string]: string };
    export default classes;
}
Answered by linesofcode
Something like this for example: https://github.com/TimMikeladze/mui-joy-confirm

Has a storybook to go along with it, even though it's a really small component. Without it would be a major pain in the butt to test
View full answer

64 Replies

What bundler are you using? Tsup has an option to embed stylesheets automatically and you just have to import your css file inside the component.
@not-milo.tsx What bundler are you using? Tsup has an option to embed stylesheets automatically and you just have to import your css file inside the component.
I’m using just tsc. I can also use webpack. Can you share further instructions or link?
You'll have to wait a couple of hours but when I get back to my desk I'll show you something
@Anay-208 this is for you
Just follow the instructions in the readme. It will take you from zero to a component published to npm
There’s a section on css
I don't need to add anything else. It's all there ✨
@not-milo.tsx I don't need to add anything else. It's all there ✨
What do you mean? It’s not in the build
Well, I actually want to learn to build from scratch. Would this guide be good? https://blog.nonstopio.com/a-step-by-step-guide-to-publishing-your-own-react-npm-package-fa2b7f1d149
if you can recommend a guide, which goes through doing it with ts and with a development server
@Anay-208 What do you mean? It’s not in the build
I meant, everything you need is in Tim's repository. Including how to manage and bundle styles with your package.
i suggest learning how npm packages and bundling stuff works
but dont build your first npm package using that knowledge
take the path of least resistance
and use an off the shelf solution
to focus on coding and adding value to your product
instead of messing around in package configs
@linesofcode but dont build your first npm package using that knowledge
I’ve built my first package already, but not with react. But alright, I’ll use your repo for now
Giant panda
you can just use vite for build
/// <reference types="vitest" />
import { defineConfig } from "vite";
import dts from "vite-plugin-dts";
import { peerDependencies } from "./package.json";

export default defineConfig({
  build: {
    lib: {
      entry: "./src/index.ts", // Specifies the entry point for building the library.
      name: "ui", // Sets the name of the generated library.
      fileName: (format) => `index.${format}.js`, // Generates the output file name based on the format.
      formats: ["cjs", "es"], // Specifies the output formats (CommonJS and ES modules).
    },
    rollupOptions: {
      external: [...Object.keys(peerDependencies)], // Defines external dependencies for Rollup bundling.
    },
    sourcemap: true, // Generates source maps for debugging.
    emptyOutDir: true, // Clears the output directory before building.
  },
  test: {
    globals: true,
    environment: "jsdom",
    setupFiles: "./setupTests.ts",
  },
  plugins: [dts()], // Uses the 'vite-plugin-dts' plugin for generating TypeScript declaration files (d.ts).
});
this is my setup for react+styled component lib
Oh nvm linesofcode repo also use that
I'll use that as a template
Giant panda
my advice would be to try without using template ( if you're just new to the bundler thing)
Giant panda
https://tomaszs2.medium.com/a-complete-guide-on-how-to-build-a-react-library-with-vite-1ce8507c922e there are enough guide available on that topic you can extend it based on your requirements
@Giant panda https://tomaszs2.medium.com/a-complete-guide-on-how-to-build-a-react-library-with-vite-1ce8507c922e there are enough guide available on that topic you can extend it based on your requirements
for some reason, it doesn't add files like.d.ts
    "build": "tsc && vite build",
Giant panda
what's your tsconfig
@Giant panda what's your tsconfig
Its default genereated by vite
{
  "compilerOptions": {
    "target": "ES2020",
    "useDefineForClassFields": true,
    "lib": ["ES2020", "DOM", "DOM.Iterable"],
    "module": "ESNext",
    "skipLibCheck": true,

    /* Bundler mode */
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",

    /* Linting */
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noFallthroughCasesInSwitch": true
  },
  "include": ["src"],
  "references": [{ "path": "./tsconfig.node.json" }]
}
and we can directly npm publish this right, so that the user can import the build folder
await reply()
@Anay-208 await reply()
Giant panda
did you provide this
  "main": "dist/index.cjs.js",
  "module": "dist/index.es.js",
  "types": "dist/index.d.ts",
in your package.json
this is my tsconfig
{
  "compilerOptions": {
    "target": "ES5", // Specifies the JavaScript version to target when transpiling code.
    "useDefineForClassFields": true, // Enables the use of 'define' for class fields.
    "lib": ["ES2020", "DOM", "DOM.Iterable"], // Specifies the libraries available for the code.
    "module": "ESNext", // Defines the module system to use for code generation.
    "skipLibCheck": true, // Skips type checking of declaration files.

    /* Bundler mode */
    "moduleResolution": "bundler", // Specifies how modules are resolved when bundling.
    "allowImportingTsExtensions": true, // Allows importing TypeScript files with extensions.
    "resolveJsonModule": true, // Enables importing JSON modules.
    "isolatedModules": true, // Ensures each file is treated as a separate module.
    "noEmit": true, // Prevents TypeScript from emitting output files.
    "jsx": "react-jsx", // Configures JSX support for React.

    /* Linting */
    "strict": true, // Enables strict type checking.
    "noUnusedLocals": true, // Flags unused local variables.
    "noUnusedParameters": true, // Flags unused function parameters.
    "noFallthroughCasesInSwitch": true, // Requires handling all cases in a switch statement.
    "declaration": true // Generates declaration files for TypeScript.
  },
  "include": ["src"], // Specifies the directory to include when searching for TypeScript files.
  "exclude": ["src/**/__docs__", "src/**/__test__"]
}
The issue is that .d.ts file is not generated
You have to instruct tsc to emit those declaration files. See: https://www.typescriptlang.org/docs/handbook/declaration-files/dts-from-js.html#tsconfig
tsup needs to have dts: true as well I believe
(if thats what he's using)
@Anay-208 ts "build": "tsc && vite build",
I think he's relying just on tsc and using vite for the actual build
Oh
he could have literally just cloned the repo I provided
and/or copied the configs
and everything would have been solved
@linesofcode he could have literally just cloned the repo I provided
I want to learn to create from scratch
@Anay-208 I want to learn to create from scratch
Giant panda
still not working ?
Oh please don't go down the Storybook route just yet. You don't need it. It's meant for managing really big component libraries
ehh
I build a lot of one-off ui components inside of storybook
but it's definetly something you can shoot yourself in the foot with
Something like this for example: https://github.com/TimMikeladze/mui-joy-confirm

Has a storybook to go along with it, even though it's a really small component. Without it would be a major pain in the butt to test
Answer
I never had any benefit in using it with small scale projects. Too much boilerplate just to get started. But yeah, it makes testing a little bit easier
I want to learn it actually
so I can do it in future
No time I guess to make it from scratch. using a template only