@import不适用于PostCSS / CSSNext

时间:2019-06-14 21:06:48

标签: reactjs webpack postcss cssnext postcss-import

我将React与Webpack 4一起使用。我试图将BassCSS类导入我的index.css

@import "basscss"
@import "basscss-colors"
@import "basscss-background-colors"

但是,似乎未应用导入。 Webpack不会抛出任何错误消息或警告。

例如,BassCSS类red不会使我的标题文本变成红色:

import React from 'react'

const Header = () => (
  <header className="red">
    <h1>My App</h1>
  </header>
)

export default Header

index.css导入了我的index.js:

import './styles/index.css'

如果我注释掉导入并手动定义red类,它将使标题文本变为红色:

/* @import "basscss"
@import "basscss-colors"
@import "basscss-background-colors" */

.red {
  color: red;
}

我使用webpack为CSS文件配置了style-loadercss-loaderpostcss-loader

...
{
    test: /\.css$/,
    use: [
        'style-loader',
        'css-loader',
        'postcss-loader'
    ]
}
...

这是我的postcss.config.js的样子:

const postcssCssNext = require('postcss-cssnext')
const postcssImport = require('postcss-import')

module.exports = {
  plugins: [
    postcssCssNext,
    postcssImport
  ]
}

最后,我对package.json的依赖:

...
  "devDependencies": {
    "@babel/core": "^7.4.5",
    "@babel/plugin-proposal-class-properties": "^7.4.4",
    "@babel/preset-env": "^7.4.5",
    "@babel/preset-react": "^7.0.0",
    "babel-cli": "^6.26.0",
    "babel-loader": "^8.0.6",
    "css-loader": "^3.0.0",
    "dotenv": "^8.0.0",
    "file-loader": "^4.0.0",
    "html-webpack-plugin": "^3.2.0",
    "postcss": "^7.0.17",
    "postcss-cssnext": "^3.1.0",
    "postcss-import": "^12.0.1",
    "postcss-loader": "^3.0.0",
    "style-loader": "^0.23.1",
    "webpack": "^4.34.0",
    "webpack-cli": "^3.3.4",
    "webpack-dev-server": "^3.7.1"
  },
  "dependencies": {
    "basscss": "^8.1.0",
    "basscss-background-colors": "^2.1.0",
    "basscss-colors": "^2.2.0",
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "whatwg-fetch": "^3.0.0"
  }
...

1 个答案:

答案 0 :(得分:1)

更改

@import "basscss"
@import "basscss-colors"
@import "basscss-background-colors"

进入

@import "basscss";
@import "basscss-colors";
@import "basscss-background-colors";

css要求每个规则都以;结尾,但是最后一行是可选的