简体   繁体   English

Webpack将scss拆分为单独的css文件

[英]Webpack split scss into separate css files

I'm writing a React application, using webpack and sass. 我正在编写一个React应用程序,使用webpack和sass。 Now i want to try those CSS Modules , but i've faced some troubles. 现在我想尝试那些CSS模块 ,但我遇到了一些麻烦。 First, as i understand, i can't use .scss modules, that's why my idea is to convert .scss files to .css files (eg. link.scss -> link.css, main.scss -> main.css, etc.) and then connect them to React component. 首先,据我所知,我不能使用.scss模块,这就是为什么我的想法是将.scss文件转换为.css文件(例如.link.scss - > link.css,main.scss - > main.css,等)然后将它们连接到React组件。 But i can't make webpack split my .scss files into separate .css files. 但是我不能让webpack将我的.scss文件拆分成单独的.css文件。 I've tried different configurations, but all the time i get main.css, which concatenates all .scss files. 我尝试了不同的配置,但我一直得到main.css,它连接所有的.scss文件。 I guess this is not the best way for me. 我想这对我来说不是最好的方式。

So, could you help me to get a correct configuration to split all .scss files to .css. 那么,你能帮助我获得一个正确的配置来将所有.scss文件拆分为.css。 Or maybe there is another way to use .scss modules in my app. 或者也许在我的应用程序中有另一种方法可以使用.scss模块。 Thanks in advance. 提前致谢。

This is my webpack configuration 这是我的webpack配置

var webpack = require('webpack');
var path = require('path');
var ExtractTextPlugin = require("extract-text-webpack-plugin");

module.exports = {
    devtool: 'inline-source-map',
    entry: [
        'webpack-hot-middleware/client',
        './src/index.jsx'
    ],
    output: {
        path: path.join(__dirname, './dist'),
        filename: 'bundle.js',
        publicPath: '/'
    },
    plugins: [
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoErrorsPlugin(),
        new ExtractTextPlugin("[name].[chunkhash].css", { allChunks: true })
    ],
    module: {
        loaders: [
            {
                test: /\.jsx$/,
                loader: 'babel-loader',
                exclude: /node_modules/,
                query: {
                    presets: ['react', 'es2015', 'react-hmre']
                }
            },
            {
                test: /\.scss$/,
                loader: ExtractTextPlugin.extract("style-loader", "css!autoprefixer-loader?browsers=last 15 versions!sass")
            },
            {
                test: /\.css$/,
                loader: "style!css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]"
            }
        ]
    }
}

And here is my output in console: 这是我在控制台中的输出:

Hash: d1d3f358c1680ba4f634
Version: webpack 1.13.1
Time: 4297ms
                        Asset       Size  Chunks             Chunk Names
                    bundle.js     2.5 MB       0  [emitted]  main
main.582da1db7866d6b8684d.css  373 bytes       0  [emitted]  main
   [0] multi main 40 bytes {0} [built]
    + 324 hidden modules
Child extract-text-webpack-plugin:
        + 2 hidden modules
Child extract-text-webpack-plugin:
        + 2 hidden modules

You can use sass with css modules together. 可以将sass与css模块一起使用。

It doesn't work for you because you did not specify the css modules parameter for the css loader in the sass loader definition. 它不适合您,因为您没有在sass loader定义中为css加载器指定css modules参数。

Below are two ways to configure sass with css modules: 以下是使用css模块配置sass的两种方法:

With ExtractTextPlugin: 使用ExtractTextPlugin:

{
    test: /\.scss$/,
    loader: ExtractTextPlugin.extract('style', '!css-loader?modules&localIdentName=[name]__[local]___[hash:base64:5]&sourceMap!sass?sourceMap')
}

Without ExtractTextPlugin: 没有ExtractTextPlugin:

{
   test: /\.scss$/,
   loaders: ['style', 'css?modules&localIdentName=[name]__[local]___[hash:base64:5]&sourceMap', 'sass?sourceMap']
}

Note that when using the ExtractTextPlugin it will create one css file. 请注意,使用ExtractTextPlugin时,它将创建一个css文件。 If you want a number of css output files you could define the ExtractTextPlugin multiple times (see this for reference). 如果您需要多个css输出文件,可以多次定义ExtractTextPlugin(请参阅参考)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM