Vue 2.0 scss 语法与 webpack

Vue 2.0 scss syntax with webpack

我正在尝试获取 <style lang="scss">(不是 "sass")在 .vue 文件中工作,以便 Atom 可以正确突出显示。我发现的解决方案似乎适用于不同版本的 vue 或 webpack。我有 vue 2.1.8 和 webpack 2.1.0-beta.22

这行不通:

var path = require('path')
var webpack = require('webpack')
//test

module.exports = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, './../dist'),
    publicPath: '/dist/',
    filename: 'build.js'
  },
  resolveLoader: {
    root: path.join(__dirname, 'node_modules'),
  },
  module: {
    loaders: [
      {
        test: /\.vue$/,
        loader: 'vue',
        options: {
          loaders: {
            'scss': 'style-loader!css-loader!sass-loader'
            // 'sass': 'vue-style!css!sass?indentedSyntax'
          }
        }
      },
      {
        test: /\.js$/,
        loader: 'babel',
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file',
        query: {
          name: '[name].[ext]?[hash]'
        }
      }
    ]
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true,
    proxy: {
      '/site/api/**': {
        target: 'http://localhost:8888',
        secure: false,
        "changeOrigin": true
      },
      '/site/font/*': {
        target: 'http://localhost:8888',
        secure: false,
        "changeOrigin": true
      }
    }
  },
  devtool: '#eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      compress: {
        warnings: false
      }
    })
  ])
}

我也试过了 vue: {} 语法,我认为它适用于 vue 1,resolve: {} 而不是 module: {},我认为它适用于 webpack 1,但我不确定。

谢谢

无意中我发现这个问题已经在新版本的vue中解决了。所以我:

  • 安装了新的 vue
  • 检查了 webpack 和 webpack-dev-server 的版本
  • 安装了那些版本
  • 复制过来webpack.config.js

这最终奏效了(从较新的 vue 安装复制)

var path = require('path')
var webpack = require('webpack')

module.exports = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, './../dist'),
    publicPath: '/dist/',
    filename: 'build.js'
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
            // Since sass-loader (weirdly) has SCSS as its default parse mode, we map
            // the "scss" and "sass" values for the lang attribute to the right configs here.
            // other preprocessors should work out of the box, no loader config like this nessessary.
            'scss': 'vue-style-loader!css-loader!sass-loader',
            'sass': 'vue-style-loader!css-loader!sass-loader?indentedSyntax'
          }
          // other vue-loader options go here
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      }
    ]
  },
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.common.js'
    }
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true,
    proxy: {
      '/site/api/**': {
        target: 'http://localhost:8888',
        secure: false,
        "changeOrigin": true
      },
      '/site/font/*': {
        target: 'http://localhost:8888',
        secure: false,
        "changeOrigin": true
      }
    }
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}

对我来说,安装 node-sasssass-loader 足以让它与 vue 1 和 vue 2 的 vue-cli webpack 设置一起工作。必须安装其中一个如果我没记错的话,全球范围内。