简体   繁体   English

Webpack2将sass / scss编译为js而不是css

[英]Webpack2 compiles sass/scss into js instead of css

I have written a webpack script which should compile all .scss files into a single css file. 我已经编写了一个webpack脚本,该脚本应该将所有.scss文件编译成单个css文件。 The reason why I chose webpack instead of gulp/grunt is because I thought it would be easier to just have a single config file in which all the compilation is done. 我之所以选择webpack而不是gulp / grunt的原因是因为我认为仅使用一个配置文件即可完成所有编译会更容易。

The problem I am facing is that scss files are getting compiled to javascript code. 我面临的问题是scss文件正在被编译为javascript代码。 Is this something that webpack does by default ? 这是webpack默认执行的操作吗?

How do I make it to css ? 我如何使其成为CSS?

code: 码:

module.exports = {
     entry: glob.sync('./src/components/**/*.scss') ,
     output: {
         path: path.resolve(__dirname, 'public', 'css'),
         filename: 'styles.css'
     },
     module: {
       loaders: [
           {
            test: /\.scss$/,
            loaders: ['style-loader', 'css-loader', 'sass-loader']

          }
        ]
    }
 };

You should use extract-text-webpack-plugin . 您应该使用extract-text-webpack-plugin After installing plugin you need to add some line to your config 安装插件后,您需要在配置中添加一些行

At top 在顶部

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

Rewrite loader 重写加载器

    {
        test: /\.scss$/,
        loader: ExtractTextPlugin.extract("style-loader", "css-loader", "sass-loader")
    }

Add plugins field to export 添加插件字段以导出

module.exports = {
    .....
    plugins: [
        new ExtractTextPlugin({filename: 'styles.css'})
    ]
    ...
}

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

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