【问题标题】:webpack not loading SCSS in Vue.js projectwebpack 没有在 Vue.js 项目中加载 SCSS
【发布时间】:2018-02-15 03:36:38
【问题描述】:

我正在使用 Vue.js 和 Webpack 作为我的构建工具构建一个前端项目。我将使用 SCSS 编写我的样式,因此我通常会使用 sass-loader。然而,虽然我可以requirematerialize-css 的 SCSS 版本,但当我尝试要求我的自定义 SCSS 文件时,我收到以下错误,有人能对此有所了解吗?

ERROR in ./node_modules/css-loader!./node_modules/sass-loader/lib/loader.js!./src/main.scss
Module build failed:
  display: flex;
                ^
      Invalid CSS after "  display: flex;": expected "}", was ".header--utils: {"
      in /media/transcend/projects/wp-vue-1/src/main.scss (line 2, column 18)
 @ ./src/main.scss 4:14-116 18:2-22:4 19:20-122
 @ ./src/main.js
 @ multi (webpack)-dev-server/client?http://192.168.1.20:8080 webpack/hot/dev-server ./main

这里是main.js

import Vue from 'vue';
import VueResource from 'vue-resource';
import AppComponent from './components/app-component/app-component';

require('materialize-css/sass/materialize.scss');
require('./main.scss');

Vue.use(VueResource);

// eslint-disable-next-line no-new
new Vue({
  el: '#app',
  components: {
    'app-component': AppComponent,
  },
});

main.scss.

.header {
  display: flex;

  .header--utils: {
    justify-content: flex-end;
  }

  .header--utils--login {
    align-self: flex-end;
  }
}

和我的 webpack 配置:

const path = require('path');
const webpack = require('webpack');

const APP = path.resolve(__dirname, './src');
const sassOpt = {
  sourceMap: true,
  includePaths: [
    path.resolve(APP, '**/*.scss'),
  ],
};

module.exports = {
  context: APP,
  entry: './main',
  output: {
    path: APP,
    filename: './bundle.js',
  },
  devtool: 'inline-source-map',
  devServer: {
    contentBase: './src',
    host: '0.0.0.0',
    public: '192.168.1.20:8080',
    hot: true,
  },
  module: {
    rules: [
      {
        test: /\.html$/,
        loader: 'raw-loader',
      },
      {
        enforce: 'pre',
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'eslint-loader',
        options: {
          quiet: true,
        },
      },
      {
        test: /\.js$/,
        exclude: '/node_modules',
        loader: 'babel-loader',
        query: {
          presets: ['es2015'],
        },
      },
      {
        test: /\.scss$/,
        use: [
          {
            loader: 'style-loader',
          },
          {
            loader: 'css-loader',
            options: {
              sourceMap: true,
            },
          },
          {
            loader: 'sass-loader',
            options: sassOpt,
          },
        ],
      },
      {
        test: /\.(eot|woff|woff2|ttf|svg|png|jpg)$/,
        loader: 'url-loader?limit=30000&name=[name]-[hash].[ext]',
      },
    ],
  },
  resolve: {
    alias: {
      vue: 'vue/dist/vue.js',
    },
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
  ],
};

还有package.json

{
  "name": "waypoints-vue",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "./node_modules/.bin/webpack-dev-server --open"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.25.0",
    "babel-eslint": "^7.2.3",
    "babel-loader": "^7.1.0",
    "babel-preset-es2015": "^6.24.1",
    "css-loader": "^0.28.7",
    "eslint": "^4.6.1",
    "eslint-config-airbnb-base": "^12.0.0",
    "eslint-friendly-formatter": "^3.0.0",
    "eslint-loader": "^1.9.0",
    "eslint-plugin-html": "^3.2.1",
    "eslint-plugin-import": "^2.7.0",
    "file-loader": "^0.11.2",
    "node-sass": "^4.5.3",
    "raw-loader": "^0.5.1",
    "sass-loader": "^6.0.6",
    "style-loader": "^0.18.2",
    "url-loader": "^0.5.9",
    "vue": "^2.3.4",
    "webpack": "^3.0.0",
    "webpack-dev-server": "^2.5.1"
  },
  "dependencies": {
    "html-loader": "^0.5.1",
    "materialize-css": "^0.100.2",
    "vue-material": "^0.7.4",
    "vue-resource": "^1.3.4"
  }
}

【问题讨论】:

  • 你确定你已经 npm 安装了所有依赖项吗?
  • 当然。我已经添加了我的 package.json

标签: webpack sass vuejs2


【解决方案1】:

发现问题是自己造成的。我在 .scss 文件中的元素选择器之后添加了一个冒号:

  .header--utils: {

我删除了冒号,它工作正常:-/

【讨论】:

    【解决方案2】:

    我猜你必须使用新的 webpack 语法。使用规则参数为here

    【讨论】:

    • 我现在尝试了,还将loaders 属性更改为rules。结果是一样的
    猜你喜欢
    • 1970-01-01
    • 2016-01-23
    • 2021-08-08
    • 1970-01-01
    • 2022-08-05
    • 2019-01-02
    • 2017-01-16
    • 2019-09-21
    • 2023-03-05
    相关资源
    最近更新 更多