当前位置: 首页 > 知识库问答 >
问题:

React+Webpack不解析文件加载器

公孙令秋
2023-03-14

我试图将一个典型的HTML站点迁移到一个“轻”的React应用程序。因此,我安装了React而没有create-react-app

项目的目录结构如下所示:

  • webpack.config.js
  • SRC
    • index.js
    • CSS
      • style.css
      • pGrooveRunners.ttf
      const path = require('path');
      const HtmlWebpackPlugin = require('html-webpack-plugin');
      
      module.exports = {
        entry: path.resolve(__dirname, 'src/index'),
        output: {
          path: path.resolve(__dirname, 'dist'),
          filename: 'bundle.js'
        },
        module: {
          rules: [
              {
                  test: /\.js$/,
                  include: path.resolve(__dirname, 'src'),
                  use: ['babel-loader']
              },
              {
                  test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
                  use: [
                    {
                      loader: 'file-loader',
                      options: {
                        name: '[name].[ext]',
                        outputPath: 'fonts/'
                      }
                    }
                  ]
                }
          ]
        },
        devServer: {
          contentBase:  path.resolve(__dirname, 'dist'),
          port: 9000
        },
        plugins: [
          new HtmlWebpackPlugin({
            template: "src/index.html"
          })
        ]
      };
      
      C:\Web\pwa-static2>yarn serve
      yarn run v1.17.3
      $ webpack-dev-server --mode development
      i 「wds」: Project is running at http://localhost:9000/
      i 「wds」: webpack output is served from /
      i 「wds」: Content not from webpack is served from C:\Web\app1\dist
      × 「wdm」: Hash: 77bf7ce09014ddcd0764
      Version: webpack 4.41.5
      Time: 3901ms
      Built at: 2020-01-27 13:35:42
           Asset      Size  Chunks             Chunk Names
       bundle.js  1.43 MiB    main  [emitted]  main       
      index.html  2.75 KiB          [emitted]  
      Entrypoint main = bundle.js
      [0] multi (webpack)-dev-server/client?http://localhost:9000 ./src/index 40 bytes {main} [built]
      [./node_modules/ansi-html/index.js] 4.16 KiB {main} [built]
      [./node_modules/react-dom/index.js] 1.33 KiB {main} [built]
      [./node_modules/react/index.js] 190 bytes {main} [built]
      [./node_modules/strip-ansi/index.js] 161 bytes {main} [built]
      [./node_modules/webpack-dev-server/client/index.js?http://localhost:9000] (webpack)-dev-server/client?http://localhost:9000 4.29 KiB {main} [built]  
      [./node_modules/webpack-dev-server/client/overlay.js] (webpack)-dev-server/client/overlay.js 3.51 KiB {main} [built]
      [./node_modules/webpack-dev-server/client/socket.js] (webpack)-dev-server/client/socket.js 1.53 KiB {main} [built]
      [./node_modules/webpack-dev-server/client/utils/createSocketUrl.js] (webpack)-dev-server/client/utils/createSocketUrl.js 2.91 KiB {main} [built]     
      [./node_modules/webpack-dev-server/client/utils/log.js] (webpack)-dev-server/client/utils/log.js 964 bytes {main} [built]
      [./node_modules/webpack-dev-server/client/utils/reloadApp.js] (webpack)-dev-server/client/utils/reloadApp.js 1.59 KiB {main} [built]
      [./node_modules/webpack-dev-server/client/utils/sendMessage.js] (webpack)-dev-server/client/utils/sendMessage.js 402 bytes {main} [built]
      [./node_modules/webpack/hot sync ^\.\/log$] (webpack)/hot sync nonrecursive ^\.\/log$ 170 bytes {main} [built]
      [./src/css/style.css] 335 bytes {main} [built] [failed] [1 error]
      [./src/index.js] 207 bytes {main} [built]
          + 30 hidden modules
      
      ERROR in ./src/css/style.css 1:0
      Module parse failed: Unexpected character '@' (1: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: pgRoofRunners;
      |     src: url("../fonts/pgroofrunners.ttf");
       @ ./src/index.js 3:0-25
      Child html-webpack-plugin for "index.html":
           1 asset
          Entrypoint undefined = index.html
          [./node_modules/html-webpack-plugin/lib/loader.js!./src/index.html] 2.99 KiB {0} [built]
          [./node_modules/lodash/lodash.js] 528 KiB {0} [built]
          [./node_modules/webpack/buildin/global.js] (webpack)/buildin/global.js 472 bytes {0} [built]
          [./node_modules/webpack/buildin/module.js] (webpack)/buildin/module.js 497 bytes {0} [built]
      i 「wdm」: Failed to compile.
      
      @font-face {
          font-family: pgRoofRunners;
          src: url("../fonts/pgroofrunners.ttf");
      }
      

      我该怎么解决这个?

共有1个答案

潘翊歌
2023-03-14

看起来您正在用WebPack导入CSS。为了理解CSS,webpack也需要一个加载器。通常你的模块里有这样的东西。规则:

{
  test: /\.css$/,
  use: [MiniCssExtractPlugin.loader, 'css-loader']
}

或者,如果不使用minicsSextractorPlugin:

{
  test: /\.css$/,
  use: ['style-loader', 'css-loader']
}
 类似资料:
  • 我正在使用webpack管理reactjs项目。我想通过webpack

  • 本文向大家介绍Vue+webpack实现懒加载过程解析,包括了Vue+webpack实现懒加载过程解析的使用技巧和注意事项,需要的朋友参考一下 这篇文章主要介绍了Vue+webpack实现懒加载过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 实现方式: 1、webpack method   require.ensure([''], cal

  • 当我启动应用程序时,我没有看到任何错误,但是是一个空数组。组件本身位于。我试着直接加载图像: 但无论我指定的路径是什么,如果出现错误,它就不能正常工作了。但是,我需要从文件夹加载所有图像,而不是静态加载,因为我事先不知道图像的名称。如有任何建议,将不胜感激。

  • 继幸存的JS电子书我试图设置我的ReactJS项目webpack配置。 在运行我的开始脚本时,我得到以下错误: ./app/index.js模块解析失败时出错:/Users/shooste/PersonalProjects/surviveJS/node_modules/eslint loader/index.js/Users/shooshte/PersonalProjects/surviveJS/

  • > 在Eclipse中新建EMF项目 导入xmi架构:xmi.xsd,并获取xmi.genmodel文件 使用xmi.genmodel生成eclipse中的所有代码,它将生成模型代码 新建一个Junit4测试用例,并尝试用Junit插件测试加载xmi-model.xmi文件,但失败了,下面是我的代码。 例外情况是: 好的,然后我试着去读xsd文件和xmi文件,我发现在xmi-model.xmi里面

  • 我提供了一些静态文件,像图像和字体,没有任何问题。当我尝试对PDF文件做同样的操作时,我得到一个错误。 ./src/views/default/components/footer.js中出错 C:\resurs\repos\frontend\src\views\default\components\footer.js 5:17错误解析导入模块'src/includes/anvandaravtal_