-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvite.config.dev.ts
More file actions
146 lines (136 loc) · 4.05 KB
/
Copy pathvite.config.dev.ts
File metadata and controls
146 lines (136 loc) · 4.05 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import * as vite from 'vite';
import { defineConfig, loadConfigFromFile } from "vite";
import type { Plugin, ConfigEnv } from "vite";
import tailwindcss from "tailwindcss";
import autoprefixer from "autoprefixer";
import fs from "fs/promises";
import path from "path";
import {
makeTagger,
injectedGuiListenerPlugin,
injectOnErrorPlugin,
monitorPlugin
} from "miaoda-sc-plugin";
export default defineConfig(async () => {
const env: ConfigEnv = { command: "serve", mode: "development" };
const configFile = path.resolve(__dirname, "vite.config.ts");
const result = await loadConfigFromFile(env, configFile);
const userConfig = result?.config;
const viteVersionInfo = {
version: vite.version,
rollupVersion: (vite as any).rollupVersion ?? null,
rolldownVersion: (vite as any).rolldownVersion ?? null,
isRolldownVite: 'rolldownVersion' in vite
};
return {
...userConfig,
define: {
__VITE_INFO__: JSON.stringify(viteVersionInfo),
...(userConfig?.define || {})
},
// 将 Vite 缓存目录设置为项目本地目录,避免在 /workspace/node_modules/ 下创建
cacheDir: path.resolve(__dirname, "node_modules/.vite"),
server: {
...(userConfig?.server || {}),
warmup: { clientFiles: ["./src/main.tsx"] }
},
plugins: [
makeTagger(),
injectedGuiListenerPlugin({
path: 'https://resource-static.cdn.bcebos.com/common/v2/injected.js'
}),
injectOnErrorPlugin(),
...(userConfig?.plugins || []),
{
name: 'hmr-toggle',
configureServer(server) {
let hmrEnabled = true;
// 包装原来的 send 方法
const _send = server.ws.send;
server.ws.send = (payload) => {
if (hmrEnabled) {
return _send.call(server.ws, payload);
} else {
console.log('[HMR disabled] skipped payload:', payload.type);
}
};
// 提供接口切换 HMR
server.middlewares.use('/innerapi/v1/sourcecode/__hmr_off', (req, res) => {
hmrEnabled = false;
let body = {
status: 0,
msg: 'HMR disabled'
};
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(body));
});
server.middlewares.use('/innerapi/v1/sourcecode/__hmr_on', (req, res) => {
hmrEnabled = true;
let body = {
status: 0,
msg: 'HMR enabled'
};
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(body));
});
// 注册一个 HTTP API,用来手动触发一次整体刷新
server.middlewares.use('/innerapi/v1/sourcecode/__hmr_reload', (req, res) => {
if (hmrEnabled) {
server.ws.send({
type: 'full-reload',
path: '*', // 整页刷新
});
}
res.statusCode = 200;
let body = {
status: 0,
msg: 'Manual full reload triggered'
};
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(body));
});
},
load(id) {
if (id === 'virtual:after-update') {
return `
if (import.meta.hot) {
import.meta.hot.on('vite:afterUpdate', () => {
window.postMessage(
{
type: 'editor-update'
},
'*'
);
});
}
`;
}
},
transformIndexHtml(html) {
return {
html,
tags: [
{
tag: 'script',
attrs: {
type: 'module',
src: '/@id/virtual:after-update'
},
injectTo: 'body'
}
]
};
}
},
,
monitorPlugin(
{
scriptSrc: 'https://resource-static.cdn.bcebos.com/sentry/browser.sentry.min.js',
sentryDsn: 'https://e3c07b90fcb5207f333d50ac24a99d3e@sentry.miaoda.cn/233',
environment: 'undefined',
appId: 'app-bxi5vk6ftmv5'
}
)
]
};
});