1- import randomstring from 'randomstring'
21import { viteExternalsPlugin as ViteExternals } from 'vite-plugin-externals'
32import { createHtmlPlugin } from 'vite-plugin-html'
4- import { viteStaticCopy } from 'vite-plugin-static-copy'
53import type { HtmlTagDescriptor } from 'vite'
6- import type { Target } from 'vite-plugin-static-copy'
4+
5+ import { dependencies } from './package.json'
76
87/**
98 * It copies the external libraries to the `assets` folder, and injects the script tags into the HTML
@@ -16,46 +15,38 @@ export const setupLibraryExternal = (
1615 isProduction : boolean ,
1716 baseUrl : string ,
1817) => {
19- const staticSuffix = randomstring . generate ( {
20- length : 8 ,
21- charset : 'hex' ,
22- } )
18+
19+ const cdnUrl = 'https://unpkg.com/'
2320
24- const staticTargets : Target [ ] = [
21+ const staticTargets : { dev : string ; prod : string } [ ] = [
2522 {
26- src : `./node_modules/vue/dist/vue.global${
23+ dev : `./node_modules/vue/dist/vue.global${
2724 isProduction ? '.prod' : ''
2825 } .js`,
29- dest : './assets/vue' ,
30- rename : `vue.global.${ staticSuffix } .js` ,
26+ prod : `${ cdnUrl } vue@${ dependencies . vue } /dist/vue.global.prod.js` ,
3127 } ,
3228 {
33- src : `./node_modules/vue-router/dist/vue-router.global${
29+ dev : `./node_modules/vue-router/dist/vue-router.global${
3430 isProduction ? '.prod' : ''
3531 } .js`,
36- dest : './assets/vue-router' ,
37- rename : `vue-router.global.${ staticSuffix } .js` ,
32+ prod : `${ cdnUrl } vue-router@${ dependencies [ 'vue-router' ] } /dist/vue-router.global.prod.js` ,
3833 } ,
3934
4035 {
41- src : `./node_modules/vue-demi/lib/index.iife.js` ,
42- dest : './assets/vue-demi' ,
43- rename : `vue-demi.${ staticSuffix } .js` ,
36+ dev : `./node_modules/vue-demi/lib/index.iife.js` ,
37+ prod : `${ cdnUrl } vue-demi@0.14.6/lib/index.iife.js` ,
4438 } ,
4539 {
46- src : './node_modules/@vueuse/shared/index.iife.min.js' ,
47- dest : './assets/vueuse' ,
48- rename : `vueuse.shared.iife.${ staticSuffix } .js` ,
40+ dev : './node_modules/@vueuse/shared/index.iife.min.js' ,
41+ prod : `${ cdnUrl } @vueuse/shared@${ dependencies [ '@vueuse/shared' ] } /index.iife.min.js` ,
4942 } ,
5043 {
51- src : './node_modules/@vueuse/core/index.iife.min.js' ,
52- dest : './assets/vueuse' ,
53- rename : `vueuse.core.iife.${ staticSuffix } .js` ,
44+ dev : './node_modules/@vueuse/core/index.iife.min.js' ,
45+ prod : `${ cdnUrl } @vueuse/core@${ dependencies [ '@vueuse/core' ] } /index.iife.min.js` ,
5446 } ,
5547 {
56- src : './node_modules/@vueuse/components/index.iife.min.js' ,
57- dest : './assets/vueuse' ,
58- rename : `vueuse.components.iife.${ staticSuffix } .js` ,
48+ dev : './node_modules/@vueuse/components/index.iife.min.js' ,
49+ prod : `${ cdnUrl } @vueuse/components@${ dependencies [ '@vueuse/components' ] } /index.iife.min.js` ,
5950 } ,
6051 ]
6152
@@ -65,9 +56,7 @@ export const setupLibraryExternal = (
6556 injectTo : 'head' ,
6657 tag : 'script' ,
6758 attrs : {
68- src : isProduction
69- ? `${ baseUrl } ${ target . dest } /${ target . rename } `
70- : target . src ,
59+ src : isProduction ? target . prod : target . dev ,
7160 type : 'text/javascript' ,
7261 } ,
7362 }
@@ -84,15 +73,12 @@ export const setupLibraryExternal = (
8473
8574 'vue-demi' : 'VueDemi' ,
8675 } ) ,
87- isProduction &&
88- viteStaticCopy ( {
89- targets : staticTargets ,
90- } ) ,
76+
9177 createHtmlPlugin ( {
9278 minify : false ,
9379 inject : {
9480 tags : injectTags ,
9581 } ,
9682 } ) ,
9783 ]
98- }
84+ }
0 commit comments