Webpack html-webpack-plugin 在模板中加载图标

2024-04-11

我正在使用 Webpackhtml-webpack-plugin以及他们提供的模板。 我想在标题中添加网站图标列表:

<link rel="apple-touch-icon" sizes="57x57" href="<%= htmlWebpackPlugin.extraFiles.apple-touch-icon-57x57 %>">
<link rel="apple-touch-icon" sizes="60x60" href="<%= htmlWebpackPlugin.extraFiles.favicons.fav60%>">
<link rel="apple-touch-icon" sizes="72x72" href="<%= htmlWebpackPlugin.extraFiles.favicons.fav72%>">
<link rel="apple-touch-icon" sizes="76x76" href="favicons/apple-touch-icon-76x76.png">
<link rel="apple-touch-icon" sizes="114x114" href="favicons/apple-touch-icon-114x114.png">
<link rel="apple-touch-icon" sizes="120x120" href="favicons/apple-touch-icon-120x120.png">
<link rel="apple-touch-icon" sizes="144x144" href="favicons/apple-touch-icon-144x144.png">
<link rel="apple-touch-icon" sizes="152x152" href="favicons/apple-touch-icon-152x152.png">
<link rel="apple-touch-icon" sizes="180x180" href="favicons/apple-touch-icon-180x180.png">
<link rel="icon" type="image/png" href="favicons/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="favicons/android-chrome-192x192.png" sizes="192x192">
<link rel="icon" type="image/png" href="favicons/favicon-96x96.png" sizes="96x96">
<link rel="icon" type="image/png" href="favicons/favicon-16x16.png" sizes="16x16">
<link rel="manifest" href="favicons/manifest.json">
<link rel="mask-icon" href="favicons/safari-pinned-tab.svg" color="#e53935">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="msapplication-TileImage" content="favicon/mstile-144x144.png">
<meta name="theme-color" content="#e53935">

我怎样才能在我的 webpack 构建中包含所有的图标,无论有没有html-webpack-plugin?

我尝试将它们添加为额外文件,如文档所述,但它们最终不会出现在我的构建文件夹中。

注意:前 3 个是我尝试做的事情,但没有成功。


您需要确保图像由 WebPack 处理,因此它们存在匹配的加载器(例如文件加载器 https://github.com/webpack/file-loader).

为此,您必须明确要求相应属性中的文件。为了能够显式地请求index.html中的文件,您必须依次为index.html使用加载器itself,允许内联处理 JS。

这实际上取决于您的设置(即您是否设置了 html-webpack-loader);看看FAQ https://github.com/ampedandwired/html-webpack-plugin/blob/master/docs/template-option.md,解释基础知识。

所以假设,你有一些这样的情况:

// webpack config.js 中的某个位置

plugins: [

  new HtmlWebpackPlugin({
    template: 'index.html',
    inject: 'head',
  }) //..
]

You can require in你的index.html图像是这样的:

<link rel="apple-touch-icon" sizes="120x120" href="${require('./favicons/apple-touch-icon-120x120.png')}">

这将尝试加载苹果触摸图标-120x120.png通过 WebPack,所以你必须确保有一个加载器andhtml-loader 还需要配置:

//somewhere in your webpack.config.js
module: {
  loaders: [
    {
      test: /\.png$/,
      loader: 'file?name=assets/icons/[name].[hash].[ext]'
    },

    {
      test: /\.html$/,
      loader: 'html',
      query: {
        interpolate: 'require'
      }
    } //..

   ] //..
}

您只需对以下图像使用 requirenot inside <img>- 标签,这些标签将被 html-webpack-loader 自动拾取。

html-loader 的未来版本可能会改变这种行为 ->https://github.com/webpack/html-loader/issues/17 https://github.com/webpack/html-loader/issues/17

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Webpack html-webpack-plugin 在模板中加载图标 的相关文章

随机推荐