-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathvite.config.ts
More file actions
120 lines (112 loc) · 3.3 KB
/
Copy pathvite.config.ts
File metadata and controls
120 lines (112 loc) · 3.3 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import dts from 'vite-plugin-dts'
import { resolve } from 'path'
import { readFileSync } from 'fs'
const PDF_WORKER_IMPORT = 'pdfjs-dist/legacy/build/pdf.worker.min.mjs?url'
const externalDependencies = [
'@floating-ui/dom',
'@radix-ui/themes',
'dayjs',
'exceljs',
'file-saver',
'i18next',
'konva',
'nanoid',
'pdf-lib',
'pdfjs-dist',
'react',
'react-colorful',
'react-dom',
'react-i18next',
'react-icons',
'web-highlighter',
'zustand'
]
function isExternalDependency(id: string) {
if (id === PDF_WORKER_IMPORT) return false
if (id === '@radix-ui/themes/styles.css') return false
return externalDependencies.some(
(dependency) => id === dependency || id.startsWith(`${dependency}/`)
)
}
function pdfWorkerAssetPlugin() {
const virtualModuleId = '\0inklayer-pdf-worker-url'
const workerPath = resolve(
__dirname,
'node_modules/pdfjs-dist/legacy/build/pdf.worker.min.mjs'
)
return {
name: 'inklayer-pdf-worker-asset',
enforce: 'pre' as const,
resolveId(id: string) {
if (id === PDF_WORKER_IMPORT) return virtualModuleId
},
load(id: string) {
if (id !== virtualModuleId) return
const referenceId = this.emitFile({
type: 'asset',
fileName: 'pdf.worker.min.mjs',
source: readFileSync(workerPath)
})
return `export default import.meta.ROLLUP_FILE_URL_${referenceId}`
}
}
}
export default defineConfig(({ mode }) => {
const isPlayground = mode === 'playground' || mode === 'development'
if (isPlayground) {
// Playground 模式:本地开发 / 构建演示站点
return {
plugins: [react()],
esbuild: {
jsx: 'automatic'
},
resolve: {
alias: { '@': resolve(__dirname, 'src') }
}
}
}
// 库模式:构建 npm 发布产物
return {
plugins: [
pdfWorkerAssetPlugin(),
dts({
include: ['src/**/*'],
exclude: ['**/*.stories.tsx', 'playground/**', 'public/**'],
insertTypesEntry: true,
rollupTypes: true
})
],
esbuild: {
jsx: 'automatic'
},
resolve: {
alias: { '@': resolve(__dirname, 'src') }
},
build: {
lib: {
entry: resolve(__dirname, 'src/index.ts'),
name: 'InkLayerReact',
cssFileName: 'inklayer-react',
formats: ['es', 'cjs'],
fileName: (format) =>
`index.${format === 'es' ? 'es' : 'cjs'}.js`
},
rollupOptions: {
external: isExternalDependency,
output: {
globals: {
react: 'React',
'react-dom': 'ReactDOM',
'pdfjs-dist': 'pdfjsLib',
konva: 'Konva',
}
}
},
sourcemap: false,
emptyOutDir: true,
copyPublicDir: false
}
}
})