-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
115 lines (113 loc) · 3.27 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
104
105
106
107
108
109
110
111
112
113
114
115
import { defineConfig, loadEnv } from 'vite';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from 'vite-plugin-commonjs';
import svgr from 'vite-plugin-svgr';
import path from 'path';
import react from '@vitejs/plugin-react';
import generatePackageJson from 'rollup-plugin-generate-package-json';
import dts from 'vite-plugin-dts';
import version from 'vite-plugin-package-version';
import pkg from './package.json';
import copy from 'rollup-plugin-copy';
import fs from 'fs';
function shiftStaticFiles(directories: string[]) {
return {
name: 'shift-static-file',
writeBundle() {
directories.forEach((dir) => {
const targetDir = path.resolve(__dirname, 'dist', dir);
if (fs.existsSync(targetDir)) {
fs.rmSync(targetDir, { recursive: true, force: true });
console.log(`Deleted directory: ${targetDir}`);
} else {
console.warn(`Directory not found: ${targetDir}`);
}
});
},
};
}
export default ({ mode }: { mode: string }) => {
const env = loadEnv(mode, process.cwd());
console.log(env);
return defineConfig({
base: './',
plugins: [
resolve(),
commonjs(),
svgr(),
react(),
version(),
dts({
tsconfigPath: './tsconfig.app.json',
insertTypesEntry: true,
include: ['src/application/**/*.ts'], // 仅包含 application 目录
}),
generatePackageJson({
outputFolder: 'dist',
baseContents: {
name: env.VITE_PUBLISH_NAME || pkg.name,
main: 'index.js',
license: 'MIT',
// @ts-expect-error 这里是因为样式文件可能没有类型定义
style: 'assets/style.css',
types: 'index.d.ts',
private: false,
version: pkg.version,
author: pkg.author,
type: 'module',
scripts: {
test: 'yarn link',
disconnect: 'yarn unlink',
},
exports: {
'.': './index.js',
'./style.css': './assets/style.css',
},
},
}),
copy({
targets: [{ src: 'NPMREADME.md', dest: 'dist', rename: 'README.md' }],
hook: 'writeBundle',
}),
shiftStaticFiles(['files']),
],
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
esbuild: {
charset: 'ascii',
},
// assetsInclude: ['**/*.mjs'], // 确保 Vite 能识别 .mjs 文件为资源
build: {
outDir: 'dist',
lib: {
entry: path.resolve(__dirname, 'src/application/lib_enter.ts'),
formats: ['es'],
fileName: (format) => `index.${format === 'es' ? 'js' : 'umd.js'}`,
},
rollupOptions: {
external: ['react', 'react-dom'],
input: {
main: path.resolve(__dirname, 'src/application/lib_enter.ts'),
},
output: {
dir: 'dist', // 确保输出在 dist 根目录
entryFileNames: `index.js`,
assetFileNames: `assets/[name].[ext]`,
globals: {
react: 'React',
// 'react-dom': 'ReactDOM',
},
},
// treeshake: true, // 启用 tree-shaking,减少无用代码
},
},
server: {
cors: true,
host: '0.0.0.0',
port: 8888,
},
});
};