【问题标题】:VueJS (w/ Vuetify) may need additional loader for scoped cssVueJS (w/Vuetify) 可能需要额外的加载器用于作用域 css
【发布时间】:2020-04-20 20:33:28
【问题描述】:

当我尝试使用 webpack 编译我的 VueJS 代码时(特别是这个):

<template>
  <v-app-bar app color="blue" flat height="100px">
    <v-img
      class="mx-2"
      contain
      max-height="80"
      max-width="80"
      :src="require('@/assets/images/logos/marble-dark-logo.png').default"
      alt="logo"
    ></v-img>
    <v-toolbar-title class="ml-4"><h2>Marble</h2></v-toolbar-title>
    <v-spacer></v-spacer>
    <v-btn outlined x-large>
      Sign In
    </v-btn>
    <v-btn class="ml-5" dark color="#363f44" x-large depressed>
      Contact Us
    </v-btn>
  </v-app-bar>
</template>
<script>
export default {
  name: 'Navbar',
  data: () => ({}),
  methods: {}
}
</script>
<style scoped>
.logo {
  border-radius: 10px;
}
</style>

我收到此错误

Module parse failed: Unexpected token (29:0)
File was processed with these loaders:
 * ./node_modules/vuetify-loader/lib/loader.js
 * ./node_modules/vue-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
|
|
> .logo {
|   border-radius: 10px;
| }
 @ ./src/components/Navbar.vue?vue&type=style&index=0&id=41458b80&scoped=true&lang=css& 1:0-211 1:227-230 1:232-440 1:232-440
 @ ./src/components/Navbar.vue
 @ ./node_modules/babel-loader/lib!./node_modules/vuetify-loader/lib/loader.js??ref--8-0!./node_modules/vue-loader/lib??vue-loader-options!./src/App.vue?vue&type=script&lang=js&
 @ ./src/App.vue?vue&type=script&lang=js&
 @ ./src/App.vue
 @ ./src/main.js

这是我的webpack.config.jspackage.json

{
  "name": "landing",
  "version": "1.0.0",
  "scripts": {
    "dev": "webpack-dev-server --open --hot --config ./build/webpack.config.js",
    "build": "webpack --mode production --progress --hide-modules --config ./build/webpack.config.js"
  },
  "devDependencies": {
    "@babel/core": "^7.9.0",
    "@babel/plugin-transform-runtime": "^7.9.0",
    "@babel/preset-env": "^7.9.5",
    "babel-eslint": "^10.1.0",
    "babel-loader": "^8.1.0",
    "copy-webpack-plugin": "^5.1.1",
    "css-loader": "^3.5.2",
    "deepmerge": "^4.2.2",
    "eslint": "^6.8.0",
    "eslint-config-prettier": "^6.10.1",
    "eslint-config-standard": "^14.1.1",
    "eslint-friendly-formatter": "^4.0.1",
    "eslint-plugin-babel": "^5.3.0",
    "eslint-plugin-html": "^6.0.2",
    "eslint-plugin-prettier": "^3.1.3",
    "eslint-plugin-promise": "^4.2.1",
    "eslint-plugin-standard": "^4.0.1",
    "eslint-plugin-vue": "^6.2.2",
    "fibers": "^4.0.2",
    "file-loader": "^6.0.0",
    "sass": "^1.26.3",
    "sass-loader": "^8.0.2",
    "style-loader": "^1.1.4",
    "vue-loader": "^15.9.1",
    "vue-style-loader": "^4.1.2",
    "vue-template-compiler": "^2.6.11",
    "vuetify-loader": "^1.4.3",
    "webpack": "^4.42.1",
    "webpack-cli": "^3.3.11",
    "webpack-dev-server": "^3.10.3"
  },
  "dependencies": {
    "vue": "^2.6.11",
    "vue-router": "^3.1.6",
    "vuetify": "^2.2.22"
  }
}
const path = require('path')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const VuetifyLoaderPlugin = require('vuetify-loader/lib/plugin')
const CopyPlugin = require('copy-webpack-plugin');

module.exports = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, "./dist"),
    filename: 'bundle.js'
  },
  devServer: {
    contentBase: path.resolve(__dirname, 'public'),
    historyApiFallback: true
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: "vue-loader"
      },
      {
        test: /\.s(c|a)ss$/,
        use: [
          'vue-style-loader',
          'css-loader',
          {
            loader: 'sass-loader',
            // Requires sass-loader@^8.0.0
            options: {
              implementation: require('sass'),
              sassOptions: {
                fiber: require('fibers'),
                indentedSyntax: true // optional
              },
            },
          },
        ],
      },
      {
        test: /\.js$/,
        loader: "babel-loader",
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpe?g|gif)$/i,
        use: [
          {
            loader: 'file-loader',
          },
        ],
      },
    ]
   },
  resolve: {
    alias: {
      vue$: "vue/dist/vue.esm.js",
      '@': path.resolve('src')
    },
    extensions: ["*", ".js", ".vue", ".json"]
  },
  plugins: [
    new VueLoaderPlugin(),
    new VuetifyLoaderPlugin(),
    new CopyPlugin([{ from: './public' }])
  ]
}

我已经看了好几个小时,并试图看看我是否滥用了加载器或在 webpack 中错误地设置了它们?

【问题讨论】:

  • 要尝试的事情:删除 scoped。添加lang="css"。或lang="scss"。如果没有任何效果,请尝试运行npm rebuild node-sass。实际上,您可以从rebuild 开始,因为它不会造成伤害。如果有任何东西 node-sass 坏了,它会修复。
  • 似乎唯一的解决方案是使用sass 语法并添加lang="sass"。有没有解决的办法?我不喜欢 sass 而不是 scss 语法。
  • 您的项目中的scss loader 显然有问题。不幸的是,没有简单的方法可以覆盖 webpack 配置。您确实可以选择使用vue.config.js,它提供configureWebpackchainWebpack,但它们远非直接。我认为修复它的最简单方法是完全创建一个新项目,然后简单地复制/粘贴 src 文件夹的内容,以及您可能在旧文件夹中修改的任何内容(根配置文件,如果有的话)。如果我在你的位置,我不会因为配置错误而开始使用sass

标签: javascript vue.js webpack vuejs2 vuetify.js


【解决方案1】:

我遇到了同样的问题,删除默认 css 加载器并替换它为我解决了这个问题。我使用的是 Rails 环境,所以我的配置看起来有点不同,但这里是相关的 sn-p:

// config/webpack/environment.js
const { environment } = require('@rails/webpacker')
environment.loaders.delete('css')
environment.loaders.append('css', {
  test: /\.css$/,
  use: [
    'style-loader',
    'css-loader'
  ]
});

【讨论】:

    猜你喜欢
    • 2021-07-24
    • 2022-11-06
    • 1970-01-01
    • 2022-11-11
    • 2020-04-05
    • 2020-12-04
    • 1970-01-01
    • 2020-09-12
    • 2022-11-10
    相关资源
    最近更新 更多