@index.css 中的导入不适用于汇总

问题描述 投票:0回答:2

我目前正在尝试用 React 和 Rollup 做一个 React 组件库。 它工作得很好,我可以导入 scss 或 css 文件我的反应组件。 但是当我尝试导入我的

index.css
文件外部文件时,当我使用汇总构建时它们仍然被导入,但是在dist文件夹中没有与此导入相关的文件。

这是我的 rollup.config.js 文件

import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import typescript from "@rollup/plugin-typescript";
import dts from "rollup-plugin-dts";
import postcss from "rollup-plugin-postcss";
import { terser } from "rollup-plugin-terser";
import peerDepsExternal from "rollup-plugin-peer-deps-external";

const packageJson = require("./package.json");

export default [
  {
    input: "src/index.ts",
    output: [
      {
        file: packageJson.main,
        format: "cjs",
        sourcemap: true,
      },  
      {
        file: packageJson.module,
        format: "esm",
        sourcemap: true,
      },
    ],
    plugins: [
      peerDepsExternal(),
      resolve(),
      commonjs(),
      typescript({
        tsconfig: "./tsconfig.json",
        exclude: ["**/src/stories/**", "**/*.stories.tsx"],
      }),
      postcss({
        extract: true
      }),
      terser(),
    ],
  },
  {
    input: "dist/esm/types/index.d.ts",
    output: [{ file: "dist/index.d.ts", format: "esm" }],
    plugins: [dts()],
    external: [/\.css$/, /\.scss$/],
  },
];

我的

index.ts

import "./index.css"
export * from "./components"

我的

index.css

@import './toto.css'

toto.css

.toto {
    color: aquamarine;
}

如您所见,

toto.css
文件已导入,但在生成的文件夹中无处我找不到该文件或其 css 属性。

谢谢

css reactjs rollup postcss
2个回答
1
投票

ok,我发现问题出在哪里了,我只是忘了在我的 postcss config.js 文件中添加 'postcss-import': {}

它解决了这个问题。


-2
投票

你能更详细地解释你的答案吗?我有同样的问题,但我只有 rollup.config.js 文件。

export default [
    {
        input: "src/index.ts",
        output: [
            {
                file: packageJson.main,
                format: "cjs",
                sourcemap: true,
            },
            {
                file: packageJson.module,
                format: "esm",
                sourcemap: true,
            },
        ],
        plugins: [resolve(), commonjs(), typescript({ tsconfig: "./tsconfig.json" }), postcss()],
        external: ["react", "react-dom"],
    },
    {
        input: "dist/esm/index.d.ts",
        output: [{ file: "dist/index.d.ts", format: "esm" }],
        plugins: [dts()],
        external: [/\.css$/],
    },
];

我需要创建 postcss config.js 文件吗?如果是,那么它的内容是什么?

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.