我可以导入scss文件使用'撰写' Webpack声明?

时间:2016-06-08 15:27:32

标签: css sass webpack css-modules

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

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

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

2 个答案:

答案 0 :(得分:5)

<强> ./ 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成为无状态函数

答案 1 :(得分:0)

这是css-loader的问题,请尝试在importLoaders: 1, Webpack配置(https://webpack.js.org/loaders/css-loader/#importloaders)中添加选项css-loader

{
    loader: "css-loader",
    options: {
      modules: true,
      importLoaders: 1,
    },
  }