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

一、文件夹结构图:

node.js - Webpack-simple + vue-cli with SASS - 无法编译?-LMLPHP


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


<style lang="scss" scoped>

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

</style>


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


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
    })
  ])
}


"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"

}

我已经按照文档做了一些搜索,但我仍然不确定如何解决。感谢您的任何线索!

最佳答案

我在从 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:[
...,
...,
{
   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 导入 o​​jit_code
import Vue from 'vue';
import App from './App.vue';

import './style.scss';

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

这是我的文件夹结构

node.js - Webpack-simple &#43; vue-cli with SASS - 无法编译?-LMLPHP

您现在可以将 style.scss 中的面向对象的 css 代码编译为 CSS

要在组件级范围内使用 scss main.js 必须在 lang="scss" 标签中指定
App.vue
<style lang="scss">
    $override-color : green;

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

有关此主题的更多见解,请参阅 this manual。期待您的反馈。

关于node.js - Webpack-simple + vue-cli with SASS - 无法编译?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48490969/

10-14 16:00