Webpack-simple + vue-cli 配置 SASS,为什么无法编译?

4

我使用默认CLI设置,使用vue-cli和webpack-simple设置了一个简单的项目,并确保在询问是否要使用SASS时选择了'Y'。

首先,文件夹结构如下图:

enter image description here

我在./src/scss目录中创建了一个main.scss文件,并向文件中添加了以下简单的scss语法:

$primary-color: rgb(173, 16, 16);

然后,在我的App.vue模板中,我像这样设置它:
<style lang="scss" scoped>

h1, h2 {
  font-weight: normal;
  color: $primary-color;
}

</style>

在我的 main.js 文件中,我确保像这样导入了 main.scss 文件:
import './scss/main.scss'

在运行 npm run dev 时,我遇到了以下编译错误:
编译失败。
./node_modules/css-loader!./node_modules/vue-loader/lib/style-compiler?{"vue":true,"id":"data-v-04c2046b","scoped":true,"hasInlineConfig":false}!./node_modules/sass-loader/lib/loader.js!./node_modules/vue-loader/lib/selector.js?type=styles&index=0!./src/App.vue
Module build failed: 
undefined
        ^
      Undefined variable: "$primary-color".



    in D:\Users\medranns\Desktop\sasswebpack\src\App.vue (line 46, column 10)
 @ ./node_modules/vue-style-loader!./node_modules/css-loader!./node_modules/vue-loader/lib/style-compiler?{"vue":true,"id":"data-v-04c2046b","scoped":true,"hasInlineConfig":false}!./node_modules/sass-loader/lib/loader.js!./node_modules/vue-loader/lib/selector.js?type=styles&index=0!./src/App.vue 4:14-316 13:3-17:5 14:22-324
 @ ./src/App.vue
 @ ./src/main.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./src/main.js

以下是我的webpack.config.js文件的内容:
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: /\.css$/,
        use: [
          'vue-style-loader',
          'css-loader'
        ],
      },
      {
        test: /\.scss$/,
        use: [
          'vue-style-loader',
          'css-loader',
          'sass-loader'
        ],
      },
      {
        test: /\.sass$/,
        use: [
          'vue-style-loader',
          'css-loader',
          'sass-loader?indentedSyntax'
        ],
      },
      {
        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 necessary.
            '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.esm.js'
    },
    extensions: ['*', '.js', '.vue', '.json']
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true,
    overlay: 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
    })
  ])
}

以下是package.json文件片段:

"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.0",
"babel-preset-stage-3": "^6.24.1",
"cross-env": "^5.0.5",
"css-loader": "^0.28.7",
"file-loader": "^1.1.4",
"node-sass": "^4.7.2",
"sass-loader": "^6.0.6",
"vue-loader": "^13.0.5",
"vue-template-compiler": "^2.4.4",
"webpack": "^3.6.0",
"webpack-dev-server": "^2.9.1"

}

我遵循了文档并进行了一些搜索,但仍不确定如何解决。感谢任何帮助!

(译:在it技术方面,我按照文档和其他资料的指示操作,但是仍有一些问题,不太清楚如何处理。非常感谢您能提供任何帮助!)

2个回答

5

我在一个由webpack-simple脚手架构建的Vue项目中使用了SCSS。

vue-init webpack-simple test-scss

尝试以下步骤在Vue项目中设置scss

使用上述命令安装基本设置后,安装以下npm包

npm install sass-loader node-sass style-loader --save-dev 

webpack.config.js文件中的rules数组中包含以下加载器。
rules:[ 
...,
...,
{
   test: /\.scss$/,
   use: [{
       loader: "style-loader" // creates style nodes from JS strings
   }, 
   {
       loader: "css-loader" // translates CSS into CommonJS
   }, 
   {
       loader: "sass-loader" // compiles Sass to CSS
   }]
}
]

在此之前做了什么?已经完成了使编译器能够将scss转换为css的配置。
现在将.scss导入到main.js中。
import Vue from 'vue';
import App from './App.vue';

import './style.scss';

new Vue({
  el: '#app',
  render: h => h(App)
});

这是我的文件夹结构

enter image description here

你现在已经准备好编译 style.scss 中的面向对象的 CSS 代码为 CSS。
要在组件级别范围内使用 scss,必须在 <style> 标签中指定 lang="scss"
App.vue
<style lang="scss">
    $override-color : green;

    div {
        color: $override-color;
    }
</style>

请参考本手册获取更深入的了解。期待您的反馈。


0

我曾经遇到过同样的问题。尝试在'vue-loader'中移除?indentedSyntax:

    test: /\.vue$/,
    loader: 'vue-loader',
    options: {
      loaders: {
        'scss': [
          'vue-style-loader',
          'css-loader',
          'sass-loader'
        ],
        'sass': [
          'vue-style-loader',
          'css-loader',
          'sass-loader?indentedSyntax'
        ]
      }
      // other vue-loader options go here
    }

结果应该是这样的:

 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 necessary.
        'scss': [
          'vue-style-loader',
          'css-loader',
          'sass-loader'
        ],
        'sass': [
          'vue-style-loader',
          'css-loader',
          'sass-loader'
        ]
      }
      // other vue-loader options go here

“the right config” 对于 scss 和 sass 看起来完全一样吗?或者你漏掉了什么?顺便问一下,这是哪个 Webpack 版本? - Frank N

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接