Skip to content

Commit 6672e46

Browse files
committed
fix: vite config
Signed-off-by: Innei <[email protected]>
1 parent 292bf2e commit 6672e46

File tree

5 files changed

+33
-59
lines changed

5 files changed

+33
-59
lines changed

library-external.ts

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import randomstring from 'randomstring'
22
import { viteExternalsPlugin as ViteExternals } from 'vite-plugin-externals'
33
import { createHtmlPlugin } from 'vite-plugin-html'
4-
import { viteStaticCopy } from 'vite-plugin-static-copy'
54
import type { HtmlTagDescriptor } from 'vite'
6-
import type { Target } from 'vite-plugin-static-copy'
5+
6+
import { dependencies } from './package.json'
77

88
/**
99
* It copies the external libraries to the `assets` folder, and injects the script tags into the HTML
@@ -21,41 +21,37 @@ export const setupLibraryExternal = (
2121
charset: 'hex',
2222
})
2323

24-
const staticTargets: Target[] = [
24+
const cdnUrl = 'https://unpkg.com/'
25+
26+
const staticTargets: { dev: string; prod: string }[] = [
2527
{
26-
src: `./node_modules/vue/dist/vue.global${
28+
dev: `./node_modules/vue/dist/vue.global${
2729
isProduction ? '.prod' : ''
2830
}.js`,
29-
dest: './assets/vue',
30-
rename: `vue.global.${staticSuffix}.js`,
31+
prod: `${cdnUrl}vue@${dependencies.vue}/dist/vue.global.prod.js`,
3132
},
3233
{
33-
src: `./node_modules/vue-router/dist/vue-router.global${
34+
dev: `./node_modules/vue-router/dist/vue-router.global${
3435
isProduction ? '.prod' : ''
3536
}.js`,
36-
dest: './assets/vue-router',
37-
rename: `vue-router.global.${staticSuffix}.js`,
37+
prod: `${cdnUrl}vue-router@${dependencies['vue-router']}/dist/vue-router.global.prod.js`,
3838
},
3939

4040
{
41-
src: `./node_modules/vue-demi/lib/index.iife.js`,
42-
dest: './assets/vue-demi',
43-
rename: `vue-demi.${staticSuffix}.js`,
41+
dev: `./node_modules/vue-demi/lib/index.iife.js`,
42+
prod: `${cdnUrl}[email protected]/lib/index.iife.js`,
4443
},
4544
{
46-
src: './node_modules/@vueuse/shared/index.iife.min.js',
47-
dest: './assets/vueuse',
48-
rename: `vueuse.shared.iife.${staticSuffix}.js`,
45+
dev: './node_modules/@vueuse/shared/index.iife.min.js',
46+
prod: `${cdnUrl}@vueuse/shared@${dependencies['@vueuse/shared']}/index.iife.min.js`,
4947
},
5048
{
51-
src: './node_modules/@vueuse/core/index.iife.min.js',
52-
dest: './assets/vueuse',
53-
rename: `vueuse.core.iife.${staticSuffix}.js`,
49+
dev: './node_modules/@vueuse/core/index.iife.min.js',
50+
prod: `${cdnUrl}@vueuse/core@${dependencies['@vueuse/core']}/index.iife.min.js`,
5451
},
5552
{
56-
src: './node_modules/@vueuse/components/index.iife.min.js',
57-
dest: './assets/vueuse',
58-
rename: `vueuse.components.iife.${staticSuffix}.js`,
53+
dev: './node_modules/@vueuse/components/index.iife.min.js',
54+
prod: `${cdnUrl}@vueuse/components@${dependencies['@vueuse/components']}/index.iife.min.js`,
5955
},
6056
]
6157

@@ -65,9 +61,7 @@ export const setupLibraryExternal = (
6561
injectTo: 'head',
6662
tag: 'script',
6763
attrs: {
68-
src: isProduction
69-
? `${baseUrl}${target.dest}/${target.rename}`
70-
: target.src,
64+
src: isProduction ? target.prod : target.dev,
7165
type: 'text/javascript',
7266
},
7367
}
@@ -84,10 +78,7 @@ export const setupLibraryExternal = (
8478

8579
'vue-demi': 'VueDemi',
8680
}),
87-
isProduction &&
88-
viteStaticCopy({
89-
targets: staticTargets,
90-
}),
81+
9182
createHtmlPlugin({
9283
minify: false,
9384
inject: {

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@
8181
"xss": "1.0.14",
8282
"xterm": "5.3.0",
8383
"xterm-addon-fit": "0.8.0",
84-
"xterm-theme": "1.1.0"
84+
"xterm-theme": "1.1.0",
85+
"@vueuse/shared": "10.7.1",
86+
"@vueuse/components": "10.7.1"
8587
},
8688
"devDependencies": {
8789
"@innei/eslint-config-vue-ts": "0.12.0",
@@ -97,8 +99,6 @@
9799
"@vitejs/plugin-vue": "5.0.2",
98100
"@vitejs/plugin-vue-jsx": "3.1.0",
99101
"@vue/compiler-sfc": "3.4.3",
100-
"@vueuse/components": "10.7.1",
101-
"@vueuse/shared": "10.7.1",
102102
"cors": "2.8.5",
103103
"eslint": "^8.56.0",
104104
"postcss": "8.4.32",
@@ -115,7 +115,6 @@
115115
"vite-plugin-checker": "0.6.2",
116116
"vite-plugin-externals": "0.6.2",
117117
"vite-plugin-html": "3.2.1",
118-
"vite-plugin-static-copy": "1.0.0",
119118
"vite-plugin-vue-devtools": "0.5.1",
120119
"vite-plugin-wasm": "3.3.0",
121120
"vite-tsconfig-paths": "4.2.3",

plugins/hello/vite.config.mts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,15 @@ export default defineConfig({
5757
name: 'hello',
5858
},
5959
}),
60-
// replaceImportsToGlobal({
61-
// include: 'src/**/*.tsx', // 根据需要调整
62-
// // ... 其他选项
63-
// }),
6460
],
6561
build: {
6662
rollupOptions: {
6763
plugins: [
6864
externalGlobals({
6965
vue: 'Vue',
66+
'vue-router': 'VueRouter',
67+
'@vueuse/core': 'VueUse',
68+
'@vueuse/components': 'VueUse',
7069
}),
7170
],
7271
},

pnpm-lock.yaml

Lines changed: 7 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vite.config.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable import/no-default-export */
12
import { visualizer } from 'rollup-plugin-visualizer'
23
import AutoImport from 'unplugin-auto-import/vite'
34
import { defineConfig, loadEnv } from 'vite'

0 commit comments

Comments
 (0)