简体   繁体   English

使用webpack为Angular App编译scss

[英]Compile scss with webpack for an Angular App

I'm using webpack 3 for my angular app. 我将Webpack 3用于我的角度应用程序。 I have some issues with compiling my scss files. 我在编译scss文件时遇到一些问题。 Here is full webpack config file: 这是完整的webpack配置文件:

const path = require('path')
const autoprefixer = require('autoprefixer')
const CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin')
const UglifyJSPlugin = require('uglifyjs-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')

const config = {
  context: __dirname + '/src',
  entry: {
    app: './index.js',
    vendor: [
      'angular',
      'angular-animate',
      'angular-bootstrap',
      'angular-route',
      'animate',
      'bootstrap',
      'bootstrap-filestyle',
      'jquery',
      'ng-file-upload',
      'ng-parallax'
    ]
  },
  output: {
    filename: 'bundle.js',
    path: path.join(__dirname, 'app'),
    publicPath: path.join(__dirname, 'app')
  },
  resolve: {
    extensions: ['.js', '.jsx', '.scss']
  },
  module: {
    loaders: [
      {
        test: /\.scss$/,
        loader: ExtractTextPlugin.extract('style-loader',
          'css?minimize!postcss!sass')
      },
      {
        test: /\.(eot|woff|woff2|ttf|svg)(\?\S*)?$/,
        loader: 'file?name=fonts/[name].[ext]'
      },
      {
        test: /\.(jpg|jpeg|gif|png|svg)$/,
        loader: 'file?name=images/[name].[ext]'
      }
    ],
    rules: [
      {
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['env']
          }
        }
      },
      {
        test: /\.svg$/,
        loader: 'url-loader'
      },
      {
        test: /\.php$/,
        loader: 'file-loader?name=[name].[ext]'
      },
      {
        test: /\.zip$/,
        loader: 'file-loader?name=[name].[ext]'
      },
      {
        test: /(\.png|\.jpg|\.gif)$/,
        loader: 'file-loader?name=[path][name].[ext]'
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin('./bundle.css'),
    new CommonsChunkPlugin({
      name: 'vendor',
      filename: 'vendor.bundle.js'
    }),
    new UglifyJSPlugin({})
    // new ExtractTextPlugin({
    //   filename: '[name].min.css'
    // })
  ]
}

module.exports = config

After running webpack i've got this error: 运行webpack后,出现此错误:

ERROR in ./assets/sass/main.scss
Module parse failed: /home/git/project/src/public/src/assets/sass/main.scss Unexpected token (1:13)
You may need an appropriate loader to handle this file type.
| $header-color: #ffffff;
| $admin-panel-height: 40px;
| 
 @ ./index.js 3:0-34

Also i tried to use this loader: https://github.com/webpack-contrib/sass-loader 我也尝试使用此加载器: https : //github.com/webpack-contrib/sass-loader

After webpack build there no errors appeared, but css file also was not created in /app folder. 在webpack构建之后,没有出现错误,但是/ app文件夹中也没有创建css文件。

file main.scss imports in index.js: index.js中的文件main.scss导入:

import './assets/sass/main.scss'

Can anyone give me an advice how can i build and watch scss files with webpack 3 ? 谁能给我建议我如何使用webpack 3构建和观看scss文件?

You have used some of the loader configs that suppose to be for webpack 1 . 您已经使用了一些应该用于webpack 1的加载器配置。

That section of the config: 配置的那一部分:

loaders: [
      {
        test: /\.scss$/,
        loader: ExtractTextPlugin.extract('style-loader',
          'css?minimize!postcss!sass')
      },
      {
        test: /\.(eot|woff|woff2|ttf|svg)(\?\S*)?$/,
        loader: 'file?name=fonts/[name].[ext]'
      },
      {
        test: /\.(jpg|jpeg|gif|png|svg)$/,
        loader: 'file?name=images/[name].[ext]'
      }
    ],

There are breaking changes when you move to Webpack 2 (or 3). 移至Webpack 2 (或3)时,会有一些重大更改。 One of them was module.loaders => module.rules . 其中之一是module.loaders => module.rules You will need to convert that section to the new structure. 您将需要将该部分转换为新结构。

In addition, ExtractTextPlugin changes it config style as well, please read it README . 此外, ExtractTextPlugin也会更改其配置样式, 请阅读README

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

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