Webpack - how to determine output style of CSS file?

与世无争的帅哥 提交于 2020-02-25 05:16:48

问题


When using Gulp to convert Sass to CSS, I can choose output style of the CSS file between: nested, expanded, compact and compressed. Do I have the same opportunity using Webpack? If yes, can you show me how to configurate Webpack to achieve my goal?

Below is my current webpack.config.js - it converts sass to css, translate ES6 to ES5 and it works perfectly:

const ExtractTextPlugin = require("extract-text-webpack-plugin");

module.exports = {
    entry: [
        "./js/app.js",
        "./scss/main.scss"
    ],
    output: {
        filename: "./js/out.js"
    },
    watch: true,
    module: {
        loaders: [
            {
                test: /.js$/,
                exclude: /node_modules/,
                loader: "babel-loader",
                query: {
                    presets: ["es2015"]
                }
            }
        ],
        rules: [
            {
                test: /.scss$/,
                use: ExtractTextPlugin.extract({
                    fallbackLoader: "style-loader",
                    use: ["css-loader?-url", "sass-loader?-url"]
                })
            }
        ]
    },
    plugins: [
        new ExtractTextPlugin({
            filename: "./css/main.css",
            disable: false,
            allChunks: true
        })
    ]
}

Thanks in advance!


回答1:


You are using sass-loader, which uses node-sass under the hood. The readme says that you can pass options directly to node-sass by specifying an options property. You can pass in the outputStyle setting this way. It would look like this:

{
    test: /.scss$/,
    use: ExtractTextPlugin.extract({
        fallbackLoader: "style-loader",
        use: [{
            loader: 'css-loader'
        }, {
            loader: 'sass-loader',
            options: {
                outputStyle: 'expanded'
            }
        }]
    })
}


来源:https://stackoverflow.com/questions/46059365/webpack-how-to-determine-output-style-of-css-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!