简体   繁体   English

什么webpack4加载程序用于加载* .svg文件,*。gif,*。eot?

[英]What webpack4 loader is used to load *.svg files, *.gif, *.eot?

I'm developing a react UI component and it depends on another UI component (react-widgets/lib/DropDownlist). 我正在开发一个React UI组件,它依赖于另一个UI组件(react-widgets / lib / DropDownlist)。 This javascript module has resources that end with these file extensions: *.gif, *.eot, *.svg, *.woff, *.ttf. 此javascript模块的资源以以下文件扩展名结尾:*。gif,*。eot,*。svg,*。woff和* .ttf。

Webpack4 is complaining that it doesn't know how to process these file types and that I might need a loader to handle these file type. Webpack4抱怨它不知道如何处理这些文件类型,并且我可能需要一个加载器来处理这些文件类型。 One error is: 一个错误是:

Error in .../react-widgets/dist/fonts/rw-widgets.svg?v=4.1.0
Module parse failed: ...
**You may need an appropriate loader to handle this file type.**

So I need to update my webpack.config.js file with the appropriate loaders for those file types. 因此,我需要使用适用于这些文件类型的相应加载程序来更新webpack.config.js文件。 My config is based off of this . 我的配置基于此 Side Note: A shout out goes to Mark England who wrote this article which does a fantastic job for how to create a reusable component . 旁注: 马克·英格兰(Mark England)对此大喊大叫, 他写了这篇文章,对于如何创建可重复使用的组件表现出色

The relevant snippet is: 相关代码段是:

// Snippet from Mark's code webpack.config.js
 module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        use: "babel-loader",
        exclude: /node_modules/
      },
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"]
      }
    ]
},

I know what the syntax for webpack is to define the loaders but I don't know what loaders to use. 我知道webpack定义加载程序的语法是什么,但是我不知道要使用什么加载程序。 But this sample webpack config file didn't include support for these other file types. 但是此示例Webpack配置文件不包含对其他文件类型的支持。

What have I done to try and solve the problem 我做了什么来尝试解决问题

  • I generally use create-react-app so I avoid this problem altogether. 我通常使用create-react-app,因此完全避免了此问题。 :-) It, however, doesn't allow me to create react libraries for distribution (AFAIK). :-)但是,它不允许我创建用于分发的反应库(AFAIK)。

  • First I searched on the net webpack *.gif loader . 首先,我在网络webpack * .gif加载程序上进行了搜索。 Nothing useful. 没什么用。

  • Next I searched for webpack loaders based on file type . 接下来,我根据文件类型搜索了webpack加载程序 This gave some good results that describe the loader syntax , pointed me to some loaders file-loader and how to use them, and this question on SO that helps me realize the *.svg loader might be what I need to load svg files. 这给出了描述加载程序语法的良好结果,为我指出了一些加载程序文件加载程序以及如何使用它们, SO上的这个问题可以帮助我认识到* .svg加载程序可能是我加载svg文件所需要的。

{test: /\\.svg$/, use: "svg-inline-loader"},

So I might be able to use svg-inline-loader for the *.svg files. 因此,我可以对* .svg文件使用svg-inline-loader。 I can repeat this approach for all of the file types. 我可以对所有文件类型重复这种方法。

The next approach is to examine Create React App (CRA) 下一种方法是检查Create React App(CRA)

I primarily develop in react, and look at the CRA webpack config files (because the create-react-app appears to stay leading edge on these topic). 我主要在react中进行开发,并查看CRA webpack配置文件(因为create-react-app似乎在这些主题上保持领先地位)。 So I can see the url-loader is used for images (based on what the node_modules/react-scripts/config/webpack.config.dev.js file is using). 因此,我可以看到url-loader用于图像(基于node_modules/react-scripts/config/webpack.config.dev.js文件的使用情况)。

Another one down... 再下来...

My question 我的问题

Does webpack (or another website) have a table that lists the loaders available for given file types? webpack(或其他网站)是否具有列出可用于给定文件类型的加载程序的表?

For example, know good image loaders for the following file types are: 例如,对于以下文件类型,已知好的图像加载器是:

Webpack 4
*.gif, *.jpg => url-loader
*.svg => svg-inline-loader
*.eot => ???

I realize that because webpack is more of a plugin/loader architecture that it might not be webpacks place to have this list so another website might need to have it. 我意识到,由于webpack更像是插件/加载器体系结构,因此可能不是webpacks拥有此列表的地方,因此另一个网站可能需要它。

When you need a loader what do you do? 当您需要装载机时,您会做什么? If there is no central place to look for this answer, then please share how you find loaders that are needed to solve your webpack file loading problem. 如果没有中心位置可以找到此答案,请与您分享如何找到解决Webpack文件加载问题所需的加载器。

It all depends on your workflow, how u want to load assets at run-time. 这完全取决于您的工作流程,以及您要如何在运行时加载资产。

For eg, if u have lot of images, it might be a good idea to use a file-loader and place them directly inside the build directory. 例如,如果您有很多图像,则最好使用文件加载器并将其直接放置在构建目录中。

The above approach will increase the GET calls and the bundled js file size will not be affeted 上面的方法将增加GET调用,并且绑定的js文件大小不会太麻烦

If u have less images/small size images then you can use url-loader which converts them into data-URL and put them inside your bundled js files. 如果您的图像较少/图像较小,则可以使用url-loader将其转换为data-URL并将其放入捆绑的js文件中。

The above approach will reduce the GET calls and will slightly increase the bundled js size. 上面的方法将减少GET调用,并会稍微增加捆绑的js大小。

If u want combination of both, then u can set a size limit and fallback loader(file-loader) on url-loader. 如果您想同时使用两者,则可以在url-loader上设置大小限制和后备加载器(文件加载器)。 What this will do is, the size of the dataURL will be calculated.If the size is grater than the limit, the file-loader will be used, which will place it in the build directory. 这样做的目的是计算dataURL的大小,如果大小超出限制,将使用文件加载器,并将其放置在构建目录中。

How I use them 我如何使用它们

  {
    test: /\.(png|jpg|gif)$/,
    use: [
      {
        loader: 'file-loader',
        options: {
          outputPath: 'images/',
          name: '[name][hash].[ext]',
        },
      },
    ],
  },
  {
    test: /\.(svg)$/,
    exclude: /fonts/, /* dont want svg fonts from fonts folder to be included */
    use: [
      {
        loader: 'svg-url-loader',
        options: {
          noquotes: true,
        },
      },
    ],
  },
  {
    test: /.(ttf|otf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
    exclude: /images/,  /* dont want svg images from image folder to be included */
    use: [
      {
        loader: 'file-loader',
        options: {
          outputPath: 'fonts/',
          name: '[name][hash].[ext]',
        },
      },
    ],
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM