8

I've made a small npm package that exports a single component. The component is styled using SCSS modules, and bundled together using Rollup. It all seemed to work fine, but once I imported it into a NextJS project I noticed that the styles aren't being applied to the SSR'd document sent down from NextJS (they do eventually get applied, but there is a blip where the component is un-styled).

This is my first time creating an npm package and my first time using Rollup, and I'm having a tough time figuring out how to fix this issue. Maybe I need to not bundle the CSS into the .js files and instruct users to import the css files themselves? I was trying to avoid having that extra step, though. Perhaps using CSS-in-JS could solve the issue, but I'd rather not add a dependency for that if I can avoid it.

Any pointers would be much appreciated!

My Rollup config looks like this:

const config = [
  {
    input: "src/lib/index.ts",
    output: [
      {
        file: pkg.main,
        format: "cjs",
        sourcemap: true,
      },
      {
        file: pkg.module,
        format: "esm",
        sourcemap: true,
      },
    ],
    plugins: [
      peerDepsExternal(),
      resolve(),
      commonjs(),
      url(),
      svgr(),
      typescript({ tsconfig: "./tsconfig.json", rootDir: "src/lib" }),
      postcss({
        modules: true,
      }),
      terser(),
      bundleSize(),
      visualizer(),
    ],
  },
  {
    input: "dist/esm/types/index.d.ts",
    output: [{ file: "dist/index.d.ts", format: "esm" }],
    plugins: [
      dts(),
      del({ hook: "buildEnd", targets: ["dist/cjs/types", "dist/esm/types"] }),
    ],
  },
];

And I'm not sure if it's relevant, but my tsconfig looks like this:

{
  "compilerOptions": {
    "target": "es5",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "jsx": "react",
    "module": "ESNext",
    "declaration": true,
    "declarationDir": "types",
    "sourceMap": true,
    "outDir": "dist",
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "removeComments": true,
    "plugins": [{ "name": "typescript-plugin-css-modules" }]
  },
  "include": ["src/lib/customModules.d.ts", "src/**/*"]
}
user1877760
  • 263
  • 2
  • 9

0 Answers0