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

问题描述 投票:4回答:1

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

这可能吗?例如,我怎样才能使以下语句有效?

.normal {
    composes: common;
    composes: primary from "../shared/colors.scss";
 }
css sass webpack css-modules
1个回答
5
投票

./伸出双手/import.伸出双手

.myImportClass {
    background-color: #f00
}

./伸出双手/style.伸出双手

.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
  • make text style.is是一个无状态函数
© www.soinside.com 2019 - 2024. All rights reserved.