Skip to content

Commit

Permalink
fix: vite config
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Jan 2, 2024
1 parent 292bf2e commit 6672e46
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 59 deletions.
47 changes: 19 additions & 28 deletions library-external.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import randomstring from 'randomstring'
import { viteExternalsPlugin as ViteExternals } from 'vite-plugin-externals'
import { createHtmlPlugin } from 'vite-plugin-html'
import { viteStaticCopy } from 'vite-plugin-static-copy'
import type { HtmlTagDescriptor } from 'vite'
import type { Target } from 'vite-plugin-static-copy'

import { dependencies } from './package.json'

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

const staticTargets: Target[] = [
const cdnUrl = 'https://unpkg.com/'

const staticTargets: { dev: string; prod: string }[] = [
{
src: `./node_modules/vue/dist/vue.global${
dev: `./node_modules/vue/dist/vue.global${
isProduction ? '.prod' : ''
}.js`,
dest: './assets/vue',
rename: `vue.global.${staticSuffix}.js`,
prod: `${cdnUrl}vue@${dependencies.vue}/dist/vue.global.prod.js`,
},
{
src: `./node_modules/vue-router/dist/vue-router.global${
dev: `./node_modules/vue-router/dist/vue-router.global${
isProduction ? '.prod' : ''
}.js`,
dest: './assets/vue-router',
rename: `vue-router.global.${staticSuffix}.js`,
prod: `${cdnUrl}vue-router@${dependencies['vue-router']}/dist/vue-router.global.prod.js`,
},

{
src: `./node_modules/vue-demi/lib/index.iife.js`,
dest: './assets/vue-demi',
rename: `vue-demi.${staticSuffix}.js`,
dev: `./node_modules/vue-demi/lib/index.iife.js`,
prod: `${cdnUrl}vue-demi@0.14.6/lib/index.iife.js`,
},
{
src: './node_modules/@vueuse/shared/index.iife.min.js',
dest: './assets/vueuse',
rename: `vueuse.shared.iife.${staticSuffix}.js`,
dev: './node_modules/@vueuse/shared/index.iife.min.js',
prod: `${cdnUrl}@vueuse/shared@${dependencies['@vueuse/shared']}/index.iife.min.js`,
},
{
src: './node_modules/@vueuse/core/index.iife.min.js',
dest: './assets/vueuse',
rename: `vueuse.core.iife.${staticSuffix}.js`,
dev: './node_modules/@vueuse/core/index.iife.min.js',
prod: `${cdnUrl}@vueuse/core@${dependencies['@vueuse/core']}/index.iife.min.js`,
},
{
src: './node_modules/@vueuse/components/index.iife.min.js',
dest: './assets/vueuse',
rename: `vueuse.components.iife.${staticSuffix}.js`,
dev: './node_modules/@vueuse/components/index.iife.min.js',
prod: `${cdnUrl}@vueuse/components@${dependencies['@vueuse/components']}/index.iife.min.js`,
},
]

Expand All @@ -65,9 +61,7 @@ export const setupLibraryExternal = (
injectTo: 'head',
tag: 'script',
attrs: {
src: isProduction
? `${baseUrl}${target.dest}/${target.rename}`
: target.src,
src: isProduction ? target.prod : target.dev,
type: 'text/javascript',
},
}
Expand All @@ -84,10 +78,7 @@ export const setupLibraryExternal = (

'vue-demi': 'VueDemi',
}),
isProduction &&
viteStaticCopy({
targets: staticTargets,
}),

createHtmlPlugin({
minify: false,
inject: {
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@
"xss": "1.0.14",
"xterm": "5.3.0",
"xterm-addon-fit": "0.8.0",
"xterm-theme": "1.1.0"
"xterm-theme": "1.1.0",
"@vueuse/shared": "10.7.1",
"@vueuse/components": "10.7.1"
},
"devDependencies": {
"@innei/eslint-config-vue-ts": "0.12.0",
Expand All @@ -97,8 +99,6 @@
"@vitejs/plugin-vue": "5.0.2",
"@vitejs/plugin-vue-jsx": "3.1.0",
"@vue/compiler-sfc": "3.4.3",
"@vueuse/components": "10.7.1",
"@vueuse/shared": "10.7.1",
"cors": "2.8.5",
"eslint": "^8.56.0",
"postcss": "8.4.32",
Expand All @@ -115,7 +115,6 @@
"vite-plugin-checker": "0.6.2",
"vite-plugin-externals": "0.6.2",
"vite-plugin-html": "3.2.1",
"vite-plugin-static-copy": "1.0.0",
"vite-plugin-vue-devtools": "0.5.1",
"vite-plugin-wasm": "3.3.0",
"vite-tsconfig-paths": "4.2.3",
Expand Down
7 changes: 3 additions & 4 deletions plugins/hello/vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,15 @@ export default defineConfig({
name: 'hello',
},
}),
// replaceImportsToGlobal({
// include: 'src/**/*.tsx', // 根据需要调整
// // ... 其他选项
// }),
],
build: {
rollupOptions: {
plugins: [
externalGlobals({
vue: 'Vue',
'vue-router': 'VueRouter',
'@vueuse/core': 'VueUse',
'@vueuse/components': 'VueUse',
}),
],
},
Expand Down
30 changes: 7 additions & 23 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vite.config.mts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable import/no-default-export */
import { visualizer } from 'rollup-plugin-visualizer'
import AutoImport from 'unplugin-auto-import/vite'
import { defineConfig, loadEnv } from 'vite'
Expand Down

0 comments on commit 6672e46

Please sign in to comment.