【问题标题】:Issue with CSS Loader in Vue app using webpack使用 webpack 的 Vue 应用程序中的 CSS 加载器问题
【发布时间】:2020-02-12 13:20:51
【问题描述】:

正在开发我的第一个 Vue 应用程序,现在我在运行构建脚本时遇到了问题。它声称我在选项对象中有一个未知的“最小化”属性,但如果我能找到在哪里该死的。我假设某种依赖冲突?关于如何解开这个问题的任何想法?

以下错误:

ERROR in ./node_modules/css-loader/dist/cjs.js?minimize!./node_modules/vue-loader/lib/style-compiler?{"vue":true,"id":"data-v-329f0f9e","scoped":false,"hasInlineConfig":false}!./node_modules/vue-loader/lib/selector.js?type=styles&index=0!./src/App.vue
Module build failed: ValidationError: Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
 - options has an unknown property 'minimize'. These properties are valid:
   object { url?, import?, modules?, sourceMap?, importLoaders?, localsConvention?, onlyLocals?, esModule? }
    at validate (C:\code\my-vue-app\node_modules\css-loader\node_modules\schema-utils\dist\validate.js:85:11)
    at Object.loader (C:\code\my-vue-app\node_modules\css-loader\dist\index.js:34:28)
 @ ./node_modules/vue-style-loader!./node_modules/css-loader/dist/cjs.js?minimize!./node_modules/vue-loader/lib/style-compiler?{"vue":true,"id":"data-v-329f0f9e","scoped":false,"hasInlineConfig":false}!./node_modules/vue-loader/lib/selector.js?type=styles&index=0!./src/App.vue 4:14-287
 @ ./src/App.vue
 @ ./src/main.js

这是我的 package.json 文件:

{
  "name": "my-vue-app",
  "version": "1.0.0",
  "scripts": {
    "dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
    "build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
  },
  "dependencies": {
    "axios": "^0.19.2",
    "vue": "^2.5.11"
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ],
  "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": "^3.4.2",
    "file-loader": "^1.1.4",
    "node-sass": "^4.5.3",
    "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.config.js 文件。 (注意:我注释掉了webpack.LoaderOptionsPlugin 位;它没有效果。)

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

【问题讨论】:

  • 显示你的 webpack 配置。可能您在配置中遇到了一些问题。
  • 谢谢,@Guarana。我已将其添加到问题中。
  • 尝试评论此代码new webpack.LoaderOptionsPlugin({ minimize: true })
  • 是的,这是我尝试的第一件事。没有变化。
  • 您有生产构建或开发的问题?

标签: vue.js webpack css-loader


【解决方案1】:

如果您正在开发您的第一个 Vue 应用程序,您应该使用 Vue CLI。它为您管理基本的 Webpack 配置。

它带有 SCSS、SASS 和较少的开箱即用支持......以及热重载、babel、jest、linting 等。

npx vue create my-app 会给你一个工作的应用程序。

大多数生产应用程序都使用 Vue CLI,因为像这样管理您自己的加载器依赖项是花费大量时间配置 webpack 而不是编程的可靠方法。

【讨论】:

  • 感谢您的提示;现在将对其进行研究。
  • ...我认为您的错误中发生的情况是 Vue 的样式加载器不支持该版本的 css 样式加载器。
  • 这种版本不匹配正是Vue CLI旨在解决的问题
  • 我认为在切换到使用 Vue CLI 作为拐杖之前了解如何使用 Webpack 是很好的。
  • 我同意,但他使用的是 Webpack 3,并且存在与各种加载器的版本不匹配问题,这些加载器没有详细记录如何处理向后兼容性。
【解决方案2】:

尝试删除

new webpack.LoaderOptionsPlugin({
  minimize: true
})

这是一个旧插件,我相信它会将选项minimize: true 传递给css-loadercss-loader 不支持这个,所以会报错。

【讨论】:

  • 是的,我试过了,结果没有明显变化。
猜你喜欢
  • 2016-06-10
  • 1970-01-01
  • 1970-01-01
  • 2020-05-15
  • 2018-07-15
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 2021-06-29
相关资源
最近更新 更多