【问题标题】:mini-css-extract-plugin throws Module parse failed: Unexpected character '@' error on @font-face in css filemini-css-extract-plugin 抛出模块解析失败:css 文件中 @font-face 出现意外字符“@”错误
【发布时间】:2020-08-18 02:58:16
【问题描述】:

我正在使用mini-css-extract-plugin 和这样的 Webpack 4:

module: {
        rules: [
{
  test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
  use: {
    loader: 'url-loader',
    options: {
      limit: 10000,
      name: utils.assetsPath('webfonts/[name].[hash:7].[ext]')
    }
  }
},
            {
                test: /\.css?$/,
                use: [
                    'css-loader',
                  {
                    loader: MiniCssExtractPlugin.loader,
                    options: {
                        sourceMap: !isProduction,
                        // only enable hot in development
                        hmr: !isProduction,
                        // if hmr does not work, this is a forceful method.
                        reloadAll: true,
                    },
                  },

                ]
              },

由于某种原因,由于在我的 css 文件中导入了 @font-face,我收到了一个错误。错误是这样的:

ERROR in ./src/assets/css/site.css
Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js):
ModuleParseError: Module parse failed: Unexpected character '@' (77: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
| @font-face { font-family: "MySans"; font-style: normal; font-weight: 800; src: local('☺'),

为什么 mini-css-extract-plugin 在@font-face 上跳闸,有解决办法吗?

【问题讨论】:

    标签: webpack css-loader mini-css-extract-plugin


    【解决方案1】:

    请切换您的加载程序顺序,底部应为“css-loader”

    {
        test: /\.css$/,
        loaders: ['ThirdLoader', 'secondLoader', 'firstLoader']
    }
    

    【讨论】:

    • 谢谢!但这现在会导致 ERROR in ./src/assets/css/site.css Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js): TypeError: Cannot convert undefined or null to object at hasOwnProperty (<anonymous>) at module.exports.module.exports (C:\Site\node_modules\css-loader\dist\cjs.js!S:\Site\src\assets\css\site.css:115:25) 的另一个错误当我转到 site.css 中的 115 行时,它仍然是导致错误的 @font-face 行——有什么想法吗?
    • 您确定您的@font-face 规则有效吗?你能把所有的规则都放在这里吗?
    • 它甚至不是@font-face 的问题。只是为了测试,我从我的 css 文件中删除了所有内容并运行了构建。但我得到的错误与ERROR in ./src/assets/css/ashappyas.css Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js): TypeError: Cannot convert undefined or null to object at hasOwnProperty (<anonymous>) at module.exports.module.exports (C:\Site\node_modules\css-loader\dist\cjs.js!C:\Site\src\assets\css\site.css:115:25) 相同。 css 文件中没有任何内容了。 115 什么都没有了。
    • 你运行的是什么版本的 minicasextractplugin?
    • 找出问题所在,但不知道为什么会导致问题。这是一个 Babel Core-JS 问题。如果我注释掉 Webpack 中 useBuiltIns : "usage", corejs : { version : "3", }babel-loader 配置,那么它构建得很好。但显然 Babel 现在没有做好它的工作。我会提出一个不同的问题!
    猜你喜欢
    • 2020-06-17
    • 2019-01-29
    • 2021-11-11
    • 2019-12-04
    • 2019-03-05
    • 2019-04-23
    • 2023-01-05
    • 2021-04-12
    • 2019-05-24
    相关资源
    最近更新 更多