我可以导入 scss 文件使用 Webpack 的 'composes' 语句吗?

Can I import scss file uses the 'composes' statement with Webpack?

我目前正在学习 CSS 模块,它们看起来会解决很多问题。但是所有的文档都是基于CSS,我想用SASS.

这可能吗?例如,我怎样才能使下面的语句起作用?

.normal {
    composes: common;
    composes: primary from "../shared/colors.scss";
 }

./scss/import.scss

.myImportClass {
    background-color: #f00
}

./scss/style.scss

.btn {
    padding: 20px;
    border: 1px solid #000;
}

.newBtn {
    composes: btn;
    composes: myImportClass from './import.scss';
    border: 5px solid #f00;
}

模块部分在您的 webpack.config.js

module: {
    rules: [
        {
            test: /\.scss/,
            use: [
                {
                    loader:'style-loader'
                },
                {
                    loader: 'css-loader',
                    options: {
                        sourceMap: true,
                        modules: true,
                        localIdentName: '[name]__[local]__[hash:base64:5]'
                    }
                },
                {
                    loader: 'sass-loader'
                }
            ]
        }
    ]
}

你的 teststyle.js (ES6 / ES7):

import React from 'react';
import style from './scss/style.scss';

const TestStyle = () => {
    render() {
        return (<div>
               <button className={style.newBtn}>my button</button>
        </div>)
    }
}

default export TestStyle;

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>webpack CSS composes</title>
</head>
<body>
    <div id="app"></div>
</body>
</html>

入口点app.js

import React from 'react';
import TestStyle from './teststyle';

React.render(
     <TestStyle />,
     document.getElementById('app')
);

//更新

  • 将 webpack 配置转换为 webpack 3

  • 使teststyle.js成为无状态函数

这是css-loader的问题,尝试在css-loader webpack config (https://webpack.js.org/loaders/css-loader/#importloaders)

中添加选项importLoaders: 1,
{
    loader: "css-loader",
    options: {
      modules: true,
      importLoaders: 1,
    },
  }