HtmlWebpackPlugin(html-webpack-plugin)传入自定义参数到模板

张溪叠
2023-12-01

文档:https://github.com/jantimon/html-webpack-plugin

示例:将版本号传入到html模板文件中

配置文件 webpack.config.js

const HtmlWebpackPlugin = require('html-webpack-plugin')
const version = require('./package.json').version;

module.exports = {
  plugins: [
    new HtmlWebpackPlugin({
      template: 'index.ejs'

      templateParameters: {
        'version': version
      },
    })
  ]
};

package.json

{
  "version": "2.0.1"
}

模板文件

<title><%= version %></title>

打包结果

<title>2.0.1</title>
 类似资料: