-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvite.config.ts
103 lines (98 loc) · 3.44 KB
/
vite.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import vue from '@vitejs/plugin-vue'
import ExecSh from 'exec-sh'
import glob from 'fast-glob'
import fs from 'fs'
import path, { dirname } from 'path'
import { fileURLToPath } from 'url'
import { Plugin, defineConfig } from 'vite'
import pkg from './package.json' assert { type: 'json' }
const { promise: ExecShPromise } = ExecSh
const DIR = typeof __dirname === 'undefined' ? dirname(fileURLToPath(import.meta.url)) : __dirname
function execSh(command: string) {
return ExecShPromise(command, { cwd: path.resolve('./') })
}
const nameCamel = pkg.name
const namePascal = nameCamel.replace(/(^\w|-\w)/g, (c) => c.replace('-', '').toUpperCase())
const dependencies = Object.keys(pkg.dependencies || [])
const PEPICONS_REGEX = /^pepicons[\\/]dist/
const VUE_COMPONENT_REGEX = /^vue[\\/]src[\\/]/
export function pluginDts(): Plugin {
return {
name: 'vue-tsc',
writeBundle: {
sequential: true,
order: 'post',
async handler({ format }) {
if (format === 'cjs') {
await execSh('vue-tsc --declarationDir ./dist/cjs --declaration --emitDeclarationOnly')
fs.rename(
path.resolve(DIR, 'dist/cjs/index.d.ts'),
path.resolve(DIR, 'dist/cjs/index.d.cts'),
console.error,
)
} else {
await execSh('vue-tsc --declarationDir ./dist --declaration --emitDeclarationOnly')
}
},
},
}
}
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue(), pluginDts()],
build: {
lib: {
entry: Object.fromEntries(
[
...glob.sync('./src/components/*.vue'),
...glob.sync('./src/{component,index,icons}.ts'),
...glob.sync('./src/icons/**/*.ts'),
].map((file) => [
// This remove `src/` as well as the file extension from each
// file, so e.g. src/nested/foo.js becomes nested/foo
path.relative('src', file.slice(0, file.length - path.extname(file).length)),
// This expands the relative paths to absolute paths, so e.g.
// src/nested/foo becomes /project/src/nested/foo.js
fileURLToPath(new URL(file, import.meta.url)),
]),
),
name: namePascal,
formats: ['cjs', 'es'],
fileName: (format, name) => {
if (format === 'cjs') {
if (name.match(PEPICONS_REGEX)) {
return `cjs/${name.replace(PEPICONS_REGEX, 'pepicons')}.js`
}
if (name.match(VUE_COMPONENT_REGEX)) {
return `cjs/${name.replace(VUE_COMPONENT_REGEX, '').replace(/\.vue$/, '')}.js`
}
return `cjs/${name.replace(/\.vue$/, '')}.js`
} else {
if (name.match(PEPICONS_REGEX)) {
return `es/${name.replace(PEPICONS_REGEX, 'pepicons')}.js`
}
if (name.match(VUE_COMPONENT_REGEX)) {
return `es/${name.replace(VUE_COMPONENT_REGEX, '').replace(/\.vue$/, '')}.js`
}
return `es/${name.replace(/\.vue$/, '')}.js`
}
},
},
rollupOptions: {
// make sure to externalize deps that shouldn't be bundled
// into your library
external: ['vue', ...dependencies],
preserveEntrySignatures: 'strict',
output: {
preserveModules: true,
chunkFileNames: '[format]/[name].js',
minifyInternalExports: false,
// Provide global variables to use in the UMD build
// for externalized deps
globals: {
vue: 'Vue',
},
},
},
},
})