如果想让html-webpack-plugin生成的HTML文件包含CDN链接或其他特定的外部资源,该如何操作?并给出一个实际的示例。

想要让 html-webpack-plugin 生成的HTML文件包含CDN链接或其他特定的外部资源,可以通过配置 html-webpack-plugin 的 template 属性或者直接在 plugin 选项中添加所需的资源链接。

方法一:使用 template 模板

你可以创建一个HTML模板文件,然后在模板中直接引入所需的CDN资源。html-webpack-plugin 会基于这个模板生成最终的HTML文件。

例如,创建一个名为 index.html 的模板文件,内容如下:

<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml" xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-if="http://www.w3.org/1999/xhtml" xmlns:v-slot="http://www.w3.org/1999/xhtml" xmlns:v-model="http://www.w3.org/1999/xhtml" xmlns:v-show="http://www.w3.org/1999/xhtml" xmlns:v-once="http://www.w3.org/1999/xhtml" xmlns:v-pre="http://www.w3.org/1999/xhtml" xmlns:v-cloak="http://www.w3.org/1999/xhtml" xmlns:v-text="http://www.w3.org/1999/xhtml" xmlns:v-html="http://www.w3.org/1999/xhtml" xmlns:keep-alive="http://www.w3.org/1999/xhtml" xmlns:v-for="http://www.w3.org/1999/xhtml" xmlns:transition="http://www.w3.org/1999/xhtml" xmlns:transition-group="http://www.w3.org/1999/xhtml" xmlns:v-once="http://www.w3.org/1999/xhtml" xmlns:v-cloak="http://www.w3.org/1999/xhtml" xmlns:v-text="http://www.w3.org/1999/xhtml" xmlns:v-html="http://www.w3.org/1999/xhtml" xmlns:v-for="http://www.w3.org/1999/xhtml" xmlns:transition="http://www.w3.org/1999/xhtml" xmlns:transition-group="http://www.w3.org/1999/xhtml" xmlns:v-if="http://www.w3.org/1999/xhtml" xmlns:v-else="http://www.w3.org/1999/xhtml" xmlns:v-else-if="http://www.w3.org/1999/xhtml" xmlns:v-show="http://www.w3.org/1999/xhtml" xmlns:v-on="http://www.w3.org/1999/xhtml" xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-slot="http://www.w3.org/1999/xhtml" xmlns:v-model="http://www.w3.org/1999/xhtml" xmlns:v-pre="http://www.w3.org/1999/xhtml" xmlns:keep-alive="http://www.w3.org/1999/xhtml" xmlns:transition="http://www.w3.org/1999/xhtml" xmlns:transition-group="http://www.w3.org/1999/xhtml" xmlns:vue-lazyload="http://www.w3.org/1999/xhtml" xmlns:vxe-grid="http://www.w3.org/1999/xhtml" xmlns:vxe-table="http://www.w3.org/1999/xhtml" xmlns:vxe-toolbar="http://www.w3.org/1999/xhtml" xmlns:vxe-tooltip="http://www.w3.org/1999/xhtml" xmlns:vxe-form="http://www.w3.org/1999/xhtml" xmlns:vxe-input="http://www.w3.org/1999/xhtml" xmlns:vxe-button="http://www.w3.org/1999/xhtml" xmlns:vxe-pulldown="http://www.w3.org/1999/xhtml" xmlns:vxe-radio-group="http://www.w3.org/1999/xhtml" xmlns:vxe-checkbox-group="http://www.w3.org/1999/xhtml" xmlns:vxe-list="http://www.w3.org/1999/xhtml" xmlns:vxe-cascader="http://www.w3.org/1999/xhtml" xmlns:vxe-paginator="http://www.w3.org/1999/xhtml" xmlns:vxe-select="http://www.w3.org/1999/xhtml" xmlns:vxe-switch="http://www.w3.org/1999/xhtml" xmlns:vxe-color="http://www.w3.org/1999/xhtml" xmlns:vxe-rate="http://www.w3.org/1999/xhtml" xmlns:vxe-counter="http://www.w3.org/1999/xhtml" xmlns:vxe-time="http://www.w3.org/1999/xhtml" xmlns:vxe-date="http://www.w3.org/1999/xhtml" xmlns:vxe-uploader="http://www.w3.org/1999/xhtml" xmlns:vxe-captured="http://www.w3.org/1999/xhtml" xmlns:vxe-menus="http://www.w3.org/1999/xhtml" xmlns:vxe-icons="http://www.w3.org/1999/xhtml" xmlns:vxe-tabs="http://www.w3.org/1999/xhtml" xmlns:vxe-popup="http://www.w3.org/1999/xhtml" xmlns:vxe-form-gather="http://www.w3.org/1999/xhtml" xmlns:vxe-form-item="http://www.w3.org/1999/xhtml" xmlns:vxe-row="http://www.w3.org/1999/xhtml" xmlns:vxe-space="http://www.w3.org/1999/xhtml" xmlns:vxe-block-quote="http://www.w3.org/1999/xhtml" xmlns:vxe-highlight-code="http://www.w3.org/1999/xhtml" xmlns:vxe-markdown="http://www.w3.org/1999/xhtml" xmlns:vxe-divider="http://www.w3.org/1999/xhtml" xmlns:vxe-loading-mask="http://www.w3.org/1999/xhtml" xmlns:vxe-modal="http://www.w3.org/1999/xhtml" xmlns:vxe-based-fonticon="http://www.w3.org/1999/xhtml" xmlns:vxe-aside="http://www.w3.org/1999/xhtml" xmlns:vxe-header="http://www.w3.org/1999/xhtml" xmlns:vxe-container="http://www.w3.org/1999/xhtml" xmlns:vxe-footer="http://www.w3.org/1999/xhtml" xmlns:vxe-toast="http://www.w3.org/1999/xhtml" xmlns:vxe-message="http://www.w3.org/1999/xhtml" xmlns:vxe-loading-bar="http://www.w3.org/1999/xhtml" xmlns:vxe-pulldown-select="http://www.w3.org/1999/xhtml" xmlns:vxe-pulldown-option="http://www.w3.org/1999/xhtml" xmlns:vxe-pulldown-option-group="http://www.w3.org/1999/xhtml" xmlns:vxe-pulldown-separator="http://www.w3.org/1999/xhtml" xmlns:vxe-change="http://www.w3.org/1999/xhtml" xmlns:vxe-if="http://www.w3.org/1999/xhtml" xmlns:vxe-show="http://www.w3.org/1999/xhtml" xmlns:vxe-v-model="http://www.w3.org/1999/xhtml" xmlns:vxe-slots="http://www.w3.org/1999/xhtml" xmlns:vxe-model="http://www.w3.org/1999/xhtml" xmlns:vxe-slot="http://www.w3.org/1999/xhtml" xmlns:vxe-on="http://www.w3.org/1999/xhtml" xmlns:vxe-contextmenuItem="http://www.w3.org/1999/xhtml" xmlns:vxe-contextmenu="http://www.w3.org/1999/xhtml" xmlns:vxe-toggle="http://www.w3.org/1999/xhtml" xmlns:vxe-worksheet="http://www.w3.org/1999/xhtml" xmlns:vxe-textarea="http://www.w3.org/1999/xhtml">    <head>
        <meta charset="UTF-8" />
        <title>Document</title>
        <!-- 引入外部CDN资源 -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
        <link href="https://cdn.bootcss.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
    </head>
    <body>
        <div id="app"></div>
    </body>
</html>

然后,在 webpack.config.js 中配置 html-webpack-plugin 的 template 属性:

const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './index.html'
    })
  ]
};

方法二:直接在 html-webpack-plugin 配置中添加资源

你也可以直接在 html-webpack-plugin 的配置中添加所需的资源链接,通过 inject: false 和手动编写 script 或 link 标签实现。

const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './index.html',
      inject: false,
      minify: {
        removeAttributeQuotes: false,
        collapseWhitespace: true
      },
      title: 'Webpack App',
      scripts: [
        'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js',
        'https://cdn.bootcss.com/bootstrap/4.1.3/js/bootstrap.min.js'
      ],
      links: [
        'https://cdn.bootcss.com/bootstrap/4.1.3/css/bootstrap.min.css'
      ]
    })
  ]
};

这样,html-webpack-plugin 会生成包含指定CDN资源链接的HTML文件。