【问题标题】:How to use css @import in rollup?如何在汇总中使用 css @import?
【发布时间】:2018-08-04 18:31:36
【问题描述】:

我尝试创建一个简单的汇总应用程序的配置,但在使用 css 时遇到了一些问题。

这是我的 css 文件:

@import "normalize.css";
@import "typeface-roboto";

html, body, #root {
  height: 100%;
  font-family: Roboto, serif;
}

body {
  background: url('./images/background.jpg');
}

我所拥有的只是关于未找到资源的 3 个错误。

这是我的配置文件:

import resolve from 'rollup-plugin-node-resolve'
import commonjs from 'rollup-plugin-commonjs'
import babel from 'rollup-plugin-babel'
import replace from 'rollup-plugin-replace'
import postcss from 'rollup-plugin-postcss'
import html from 'rollup-plugin-fill-html'
import url from "rollup-plugin-url"

process.env.NODE_ENV = 'development'

const CWD = process.cwd()
const Paths = {
  SRC: `${CWD}/src`,
  DIST: `${CWD}/dist-rollup`,
  NODE_MODULES: `${CWD}/node_modules`
}
Object.assign(Paths, {
  INPUT: Paths.SRC + '/index.js',
  OUTPUT: Paths.DIST + '/index.js'
})


export default {
  input: Paths.INPUT,
  output: {
    file: Paths.OUTPUT,
    format: 'iife', // immediately-invoked function expression — suitable for <script> tags
    // sourcemap: true
  },
  plugins: [
    html({
      template: `${Paths.SRC}/template.html`,
      filename: 'index.html'
    }),
    postcss({
      modules: true,
      plugins: [
      ]
    }),
    url({
      limit: 10 * 1024, // inline files < 10k, copy files > 10k
    }),
    resolve(), // tells Rollup how to find date-fns in node_modules
    babel({
      exclude: 'node_modules/**'
    }),
    commonjs(), // converts date-fns to ES modules
    replace({ 'process.env.NODE_ENV': JSON.stringify('development') })
  ]
}

我曾尝试使用一些插件,例如rollup-plugin-rebasepostcss-assets,但不幸的是,它对我没有帮助。

【问题讨论】:

  • 你在哪里将你的 css 添加到你的模块/应用程序中?如果它在 src/index.js 中导入,你可以分享 index.js 代码吗?

标签: css import rollup rollupjs


【解决方案1】:

也许我选择了不常用的方式,但对我来说,单一的工作解决方案是使用带有 2 个插件的 postcss:postcss-imports 用于@import 语法,postcss-url 用于 url。

但也有困难。 Postcss-url 不想像我期望的那样工作。 我不得不使用这个插件的 3 个实例:

[
  postcssUrl(), // Find files' real paths.
  postcssUrl({
    url: 'copy',
    basePath: 'src',
    useHash: true,
    assetsPath: 'dist'
  }), // Copy to required destination.
  postcssUrl({
    url (asset) {
      const rebasedUrl = `dist/${path.basename(asset.absolutePath)}`

      return `${rebasedUrl}${asset.search}${asset.hash}`
    }
  }) // Fix final paths.
]

你可能会在https://github.com/pashaigood/bundlers-comparison看到它的复杂性

当然,如果您知道,我会很高兴看到更简单的解决方案,请与我分享。

【讨论】:

    【解决方案2】:

    我发现 css-import 运行良好,NPM 包提供了一个 cssimport 命令行界面,它接受一个主 CSS 文件,其中包括 @import 语句和一个可选的目录列表,用于搜索 CSS ;它将合并的 CSS 输出到标准输出,可以写入单个文件。

    我使用以下在我的build 目录中输出单个main.css 文件,在node_modules 下搜索导入的文件:

    cssimport main.css ./node_modules/ &gt; ./build/main.css

    使用 rollup 时,您可以使用 rollup-plugin-execute plugin 来执行 shell 命令,作为 rollup 构建过程的一部分。例如:

        plugins: [
          ...
          execute('npx cssimport main.css ./node_modules/ > build/main.css')
        ]
    

    【讨论】:

    • 你的代码也不适合我,所以我只使用cp:execute('cp node_modules/bootstrap/dist/css/bootstrap.min.css ./chrome-ext/build/popup/'),
    猜你喜欢
    • 2020-10-27
    • 2023-02-13
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 2021-05-13
    • 2021-05-09
    • 1970-01-01
    • 2019-03-04
    相关资源
    最近更新 更多