-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvite.config.ts
77 lines (69 loc) · 1.84 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
import {defineConfig} from 'vite';
import vue from '@vitejs/plugin-vue';
import {glob} from 'glob';
import path from 'path';
import {readFileSync, writeFileSync} from "node:fs";
import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js';
// 读取 package.json
const packageJson = JSON.parse(readFileSync('package.json', 'utf-8'));
const getEntryPoints = () => {
const entries: { [key: string]: string } = {};
const files = glob.sync('src/*.ts');
files.forEach((file) => {
const name = path.parse(file).name;
// 检查该文件是否在 commands 中定义
const command = packageJson.commands.find((cmd: any) => cmd.name === name);
if (command) {
entries[name] = file;
}
});
return entries;
};
const initSofastContext = () => {
return {
name: 'inject-global-object',
transform(code: string, id: string) {
const fileName = path.basename(id, '.ts');
const command = packageJson.commands.find((cmd: any) => cmd.name === fileName);
if (command) {
const type = command.type || 'page'; // 默认为 page 类型
const injectedCode = `
globalThis.__SOFAST__ = {
type: '${type}'
};
${code}
`;
return {
code: injectedCode,
map: null
};
}
return null;
}
};
};
export default defineConfig({
plugins: [
vue(),
cssInjectedByJsPlugin(),
initSofastContext(),
{
name: 'copy-package-json',
writeBundle() {
const distPath = path.resolve(__dirname, 'dist');
writeFileSync(path.join(distPath, 'package.json'), JSON.stringify(packageJson, null, 2));
},
},
],
build: {
lib: {
entry: getEntryPoints(),
formats: ['es'],
},
rollupOptions: {
output: {
entryFileNames: '[name].js',
},
},
},
});