一、vue.config.js中配置
const { defineConfig } = require('@vue/cli-service')
let cdn = { css: [], js: [] }
const isProd = process.env.NODE_ENV === 'development'
let externals = {}
if (isProd) {
externals = {
echarts: 'echarts',
}
cdn = {
css: [],
js: [
'https://cdn.bootcdn.net/ajax/libs/echarts/5.4.3/echarts.common.js',
],
}
}
module.exports = defineConfig({
lintOnSave: false,
transpileDependencies: true,
devServer: {
client: {
overlay: false,
},
},
configureWebpack: {
externals: externals,
},
chainWebpack(config) {
config.plugin('html').tap((args) => {
args[0].cdn = cdn
return args
})
},
})
二、public下index.html中
<body>
<div id="app"></div>
<% for(var js of htmlWebpackPlugin.options.cdn.js) { %>
<script src="<%=js%>"></script>
<% } %>
</body>