在我的文件中找到了使用HtmlWebpackPlugin
和WebpackAutoInject
插件的解决方案;vue.config.js
放弃我的VUE_APP_*
变量使用,index.html
因为它导致我构建错误。
npm install html-webpack-plugin --save-dev
npm install webpack-auto-inject-version --save-dev
我的新vue.config.js
:
const HtmlWebpackPlugin = require('html-webpack-plugin')
const WebpackAutoInject = require('webpack-auto-inject-version')
module.exports = {
publicPath: process.env.NODE_ENV === 'production'
? process.env.VUE_APP_PUBLIC_PATH_EN
: '/',
configureWebpack: {
plugins: [
// index.html customization
new HtmlWebpackPlugin({
template: 'public/index.html',
filename: 'index.html',
inject: true,
deploy: process.env.VUE_APP_DEPLOY,
webtrends: '/webtrends/scripts/webtrends.load.js', // include webtrends script for OPS only
minify: {
removeComments: false
}
}),
// Auto inject version
new WebpackAutoInject({
SILENT: true,
// options
components: {
AutoIncreaseVersion: false,
InjectAsComment: false
},
componentsOptions: {
InjectByTag: {
// https://www.npmjs.com/package/dateformat
dateFormat: 'isoUtcDateTime'
}
}
})
]
}
}
然后在我的index.html
(使用自定义脚本包含在构建中):
<!--
My application
Version: [AIV]{version}[/AIV]
Build date: [AIV]{date}[/AIV]
-->
<% if (htmlWebpackPlugin.options.deploy === 'ops') { %>
<script src="<%= htmlWebpackPlugin.options.webtrends %>"></script>
<% } %>