【问题标题】:React phone number input is causing a webpack error on cypressReact电话号码输入导致cypress上的webpack错误
【发布时间】:2021-01-21 17:02:18
【问题描述】:

当我尝试运行使用 react-phone-number-input 的 cypress 测试时,出现 webpack 错误,此错误仅出现在 cypress 中,我的 react 项目不显示此错误并且工作正常,是否有解决方案可以消除此错误的 webpack 或 cypress?

Error: Webpack Compilation Error
./node_modules/react-phone-number-input/style.css 2:0
Module parse failed: Unexpected token (2:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| /* CSS variables. */

> :root {
|   --PhoneInputCountrySelect-marginRight: 0.35em;
|   --PhoneInputCountrySelectArrow-width: 0.3em;
 @ ./src/components/InputPhoneNumber/index.js 15:0-44
 @ ./src/components/SignUpForm/index.js
 @ ./src/components/ProductForm/index.js
 @ ./src/tests/ProductForm.spec.js

我的 Webpack 配置是这样的

const CopyWebpackPlugin = require('copy-webpack-plugin')

module.exports = {
  plugins: [
    new CopyWebpackPlugin({
      patterns: [
        { from: 'public' }
      ]
    })
  ],
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader'
        }
      },
      {
        test: /\.css$/i,
        use: ['style-loader', 'css-loader']
      },
      {
        test: /\.s[ac]ss$/i,
        use: [
          {
            loader: 'style-loader' // inject CSS to page
          },
          {
            loader: 'css-loader' // translates CSS into CommonJS modules
          },
          {
            loader: 'postcss-loader', // Run postcss actions
            options: {
              plugins: function () { // postcss plugins, can be exported to postcss.config.js
                return [
                  require('autoprefixer')
                ]
              }
            }
          },
          {
            loader: 'sass-loader' // compiles Sass to CSS
          }
        ]
      }
    ]
  }
}

【问题讨论】:

  • 看起来你的 css 加载器正在阻塞 css 变量的使用。你用的是什么版本?
  • "css-loader": "^3.6.0", "cypress": "^6.2.1",
  • 您的 css-loader 是一个相当旧的版本。尝试将其更新到最新版本。
  • 我将 css-loader 更新到最新版本 v5.0.1 仍然无法正常工作
  • 样式加载器怎么样?也是旧版本吗?

标签: reactjs webpack cypress


【解决方案1】:

css-loader 不编译 css 根参数。

这个答案将帮助您解决问题 https://github.com/webpack-contrib/css-loader/issues/69#issuecomment-108597013

【讨论】:

    猜你喜欢
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    • 2022-11-30
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多