Skip to content

Commit

Permalink
chore: merge
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Feb 9, 2024
1 parent 0315c04 commit e2b317e
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 44 deletions.
54 changes: 20 additions & 34 deletions library-external.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
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 @@ -16,46 +15,38 @@ export const setupLibraryExternal = (
isProduction: boolean,
baseUrl: string,
) => {
const staticSuffix = randomstring.generate({
length: 8,
charset: 'hex',
})

const cdnUrl = 'https://unpkg.com/'

const staticTargets: Target[] = [
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 +56,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,15 +73,12 @@ export const setupLibraryExternal = (

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

createHtmlPlugin({
minify: false,
inject: {
tags: injectTags,
},
}),
]
}
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@
"unplugin-auto-import": "0.17.3",
"vite": "5.0.10",
"vite-plugin-checker": "0.6.2",
"vite-plugin-externals": "0.6.2",
"vite-plugin-html": "3.2.2",
"vite-plugin-mkcert": "1.17.3",
"vite-plugin-vue-devtools": "0.5.1",
"vite-plugin-wasm": "3.3.0",
Expand Down
8 changes: 4 additions & 4 deletions plugins/hello/vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@ 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
Loading

0 comments on commit e2b317e

Please sign in to comment.