forked from jdf2e/nutui-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.build.ts
100 lines (96 loc) · 2.84 KB
/
vite.config.build.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
import { defineConfig } from 'vite'
import dts from 'vite-plugin-dts'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import fse from 'fs-extra'
const path = require('path')
const config = require('./package.json')
const componentsConfig = require('./src/config.json')
const banner = `/*!
* ${config.name} v${config.version} ${new Date()}
* (c) 2023 @jdf2e.
* Released under the MIT License.
*/`
const { resolve } = path
let fileStr = `@import "@/styles/variables.scss";`
const projectID = process.env.VITE_APP_PROJECT_ID
if (projectID) {
fileStr = `@import '@/styles/variables-${projectID}.scss';`
}
// https://vitejs.dev/config/
export default defineConfig({
resolve: {
alias: [{ find: '@', replacement: resolve(__dirname, './src') }],
},
plugins: [
dts({
outputDir: 'dist/types',
clearPureImport: false,
exclude: [
'node_modules/**',
'src/sites/**',
'src/**/demo.tsx',
'src/**/demo.taro.tsx',
'src/**/*.spec.tsx',
],
afterBuild: () => {
fse
.readFile('./dist/types/packages/nutui.react.build.d.ts', 'utf-8')
.then((data: string) => {
fse.remove('./dist/types/packages/nutui.react.build.d.ts')
const types: string[] = []
componentsConfig.nav.forEach((item: any) => {
item.packages.forEach((element: any) => {
const { name, show, exportEmpty } = element
if (show || exportEmpty) {
const lowerName = name.toLowerCase()
if (lowerName === 'icon') return
types.push(
`export { ${name}Props } from './packages/${lowerName}/${lowerName}'`
)
}
})
})
fse.outputFile(
'./dist/types/index.d.ts',
`${types.join('\n')}\n${data.replace(/\.\.\//g, './')}`
)
})
},
}),
],
build: {
minify: false,
emptyOutDir: true,
rollupOptions: {
// 请确保外部化那些你的库中不需要的依赖
external: ['react', 'react-dom'],
output: [
{
banner,
// 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
globals: {
react: 'React',
'react-dom': 'ReactDOM',
},
format: 'es',
entryFileNames: 'nutui.react.es.js',
},
{
banner,
// 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
globals: {
react: 'React',
'react-dom': 'ReactDOM',
},
name: 'nutui.react',
format: 'umd',
entryFileNames: 'nutui.react.umd.js',
},
],
},
lib: {
entry: 'src/packages/nutui.react.build.ts',
},
},
})