Skip to content

Commit 1132723

Browse files
author
奇淼(piexlmax
authored
Merge pull request #336 from JunIce/develop
chore: 增加自动扫描package.json, 从cdn引入资源
2 parents cab8de7 + 843a585 commit 1132723

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

web/build.config.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict'
2+
3+
module.exports = {
4+
title: 'GIN-VUE-ADMIN1',
5+
baseCdnUrl: '//cdn.staticfile.org',
6+
cdns: [
7+
/**
8+
* 如果设置path属性, { name: 'vue', scope: 'Vue', path: '/vue/2.6.9/vue.min.js' } 即编译出来以[baseCdnUrl][path]
9+
* 否则自动拼写 [baseCdnUrl]/[name]/[version]/[name].min.js
10+
* */
11+
{ name: 'vue', scope: 'Vue' },
12+
{ name: 'vue-router', scope: 'VueRouter' },
13+
{ name: 'vuex', scope: 'Vuex' },
14+
{ name: 'axios', scope: 'axios' },
15+
{ name: 'echarts', scope: 'echarts' },
16+
{ name: 'element-ui', scope: 'ELEMENT', path: '/element-ui/2.12.0/index.js'},
17+
]
18+
};

web/public/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
<meta http-equiv="X-UA-Compatible" content="IE=edge">
88
<meta name="viewport" content="width=device-width,initial-scale=1.0">
99
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
10-
<title>GIN-VUE-ADMIN</title>
10+
<title><%=htmlWebpackPlugin.options.title%></title>
1111
<% if(process.env.NODE_ENV!=='development'){ %>
12-
<script src="//cdn.staticfile.org/vue/2.6.10/vue.min.js"></script>
13-
<script src="//cdn.staticfile.org/axios/0.19.0/axios.min.js"></script>
14-
<script src="//cdn.staticfile.org/echarts/4.7.0/echarts.min.js"></script>
12+
<% htmlWebpackPlugin.options.cdns.forEach(function(item){ if(item.js){ %>
13+
<script type="text/javascript" src="<%= item.js %>"></script>
14+
<% } }) %>
1515
<% } %>
1616
</head>
1717

web/vue.config.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict'
22

33
const path = require('path')
4+
const buildConf = require('./build.config')
5+
const packageConf = require('./package.json')
46

57
function resolve(dir) {
68
return path.join(__dirname, dir)
@@ -63,13 +65,31 @@ module.exports = {
6365
// 不打包 begin
6466
// 1.目前已经测试通过[vue,axios,echarts]可以cdn引用,其它组件测试通过后可继续添加
6567
// 2.此处添加不打包后,需在public/index.html head中添加相应cdn资源链接
66-
config.set('externals', {
67-
vue: 'Vue',
68-
axios: 'axios',
69-
echarts: 'echarts'
70-
})
68+
config.set('externals', buildConf.cdns.reduce((p, a) => {
69+
p[a.name] = a.scope
70+
return p
71+
},{}))
7172
// 不打包 end
7273

74+
config.plugin('html')
75+
.tap(args => {
76+
if(buildConf.title) {
77+
args[0].title = buildConf.title
78+
}
79+
if(buildConf.cdns.length > 0) {
80+
args[0].cdns = buildConf.cdns.map(conf => {
81+
if (conf.path) {
82+
conf.js = `${buildConf.baseCdnUrl}${conf.path}`
83+
} else {
84+
conf.js = `${buildConf.baseCdnUrl}/${conf.name}/${packageConf.dependencies[conf.name].replace('^', '')}/${conf.name}.min.js`
85+
}
86+
87+
return conf
88+
})
89+
}
90+
return args
91+
})
92+
7393
config
7494
.plugin('ScriptExtHtmlWebpackPlugin')
7595
.after('html')

0 commit comments

Comments
 (0)