【问题标题】:Vue 2.0 scss syntax with webpackVue 2.0 scss 语法与 webpack
【发布时间】:2017-05-25 23:32:15
【问题描述】:

我正在努力 <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,但我不确定。

谢谢

【问题讨论】:

    标签: webpack vue.js webpack-2


    【解决方案1】:

    这最终奏效了(从较新的 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
        })
      ])
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        我偶然发现这已经在较新版本的 vue 中解决了。所以我:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-14
          • 2017-12-11
          • 2018-11-06
          • 2020-12-15
          • 2017-04-30
          相关资源
          最近更新 更多