簡體   English   中英

Webpack-simple + vue-cli with SASS - 無法編譯?

[英]Webpack-simple + vue-cli with SASS - Can't compile?

我使用默認的CLI設置使用vue-cli和webpack-simple設置了一個簡單的項目,並確保在詢問我是否要使用SASS時選擇了“Y”。

一,文件夾結構pic:

在此輸入圖像描述

我創建了一個main.scss在文件./src/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"

}

我已經按照文檔進行了一些搜索,但我仍然不確定如何修復。 感謝任何線索!

我在一個從webpack-simple樣板開發的vue項目中使用scss

vue-init webpack-simple test-scss

嘗試以下步驟在vue項目中設置scss

使用上面的命令安裝基本設置后,在npm packages下面安裝

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

webpack.config.js下的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的配置已完成。

現在進口.scssmain.js

import Vue from 'vue';
import App from './App.vue';

import './style.scss';

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

這是我的文件夾結構

在此輸入圖像描述

您現在可以將style.scss中面向對象的css代碼編譯為CSS

要在組件級別范圍內使用scss,必須在<style>標記中指定lang="scss"

App.vue
<style lang="scss">
    $override-color : green;

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

有關此主題的更多見解,請參閱本手冊 期待您的反饋。

我遇到了同樣的問題。 嘗試在'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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM