Skip to content

Commit e2b317e

Browse files
committed
chore: merge
Signed-off-by: Innei <i@innei.in>
1 parent 0315c04 commit e2b317e

File tree

4 files changed

+269
-44
lines changed

4 files changed

+269
-44
lines changed

library-external.ts

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import randomstring from 'randomstring'
21
import { viteExternalsPlugin as ViteExternals } from 'vite-plugin-externals'
32
import { createHtmlPlugin } from 'vite-plugin-html'
4-
import { viteStaticCopy } from 'vite-plugin-static-copy'
53
import 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+
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@
111111
"unplugin-auto-import": "0.17.3",
112112
"vite": "5.0.10",
113113
"vite-plugin-checker": "0.6.2",
114+
"vite-plugin-externals": "0.6.2",
115+
"vite-plugin-html": "3.2.2",
114116
"vite-plugin-mkcert": "1.17.3",
115117
"vite-plugin-vue-devtools": "0.5.1",
116118
"vite-plugin-wasm": "3.3.0",

plugins/hello/vite.config.mts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,16 @@ export default defineConfig({
5757
name: 'hello',
5858
},
5959
}),
60-
// replaceImportsToGlobal({
61-
// include: 'src/**/*.tsx', // 根据需要调整
62-
// // ... 其他选项
63-
// }),
60+
6461
],
6562
build: {
6663
rollupOptions: {
6764
plugins: [
6865
externalGlobals({
6966
vue: 'Vue',
67+
'vue-router': 'VueRouter',
68+
'@vueuse/core': 'VueUse',
69+
'@vueuse/components': 'VueUse',
7070
}),
7171
],
7272
},

0 commit comments

Comments
 (0)