-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
98 lines (88 loc) · 3.43 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
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
react(),
{
name: 'srcbook-error-reporter',
// ref: https://vite.dev/guide/api-plugin.html#transformindexhtml
transformIndexHtml(html) {
if (process.env.NODE_ENV !== 'development') {
return html;
}
return [{
tag: 'script',
attrs: { type: 'module' },
injectTo: 'head',
children: `
// Report any logs, errors, etc to the parent srcbook app context to include in
// the bottom panel.
for (const method of ['log', 'debug', 'info', 'error', 'warn']) {
const originalFn = console[method];
console[method] = function(...args) {
window.parent.postMessage({ type: 'console', method, args: args.map(a => \`\${a}\`) }, '*');
return originalFn(...args);
};
}
// Report any thrown errors / promise rejections so they show up in the logs
window.addEventListener('error', (e) => {
if (window.parent) {
window.parent.postMessage({ type: 'error', stack: e.error.stack }, '*');
}
});
window.addEventListener('unhandledrejection', (e) => {
if (window.parent) {
window.parent.postMessage({ type: 'unhandledrejection', reason: e.reason }, '*');
}
});
// Report URL change event from iframe
const originalPushState = history.pushState;
const originalReplaceState = history.replaceState;
const notifyParent = () => {
window.parent.postMessage({ type: 'iframe_url_changed', url: window.location.href }, '*');
};
history.pushState = function (...args) {
originalPushState.apply(this, args);
notifyParent();
};
history.replaceState = function (...args) {
originalReplaceState.apply(this, args);
notifyParent();
};
window.addEventListener('popstate', notifyParent);
window.addEventListener('hashchange', notifyParent);
`,
}];
},
transform(src: string, id: string) {
if (id === '/app/src/main.tsx') {
return `
${src}
if (process.env.NODE_ENV === 'development') {
// Report any vite-hmr errors up to the parent srcbook app context
// Full event list: https://vite.dev/guide/api-hmr.html
if (import.meta.hot) {
import.meta.hot.on('vite:error', (data) => {
if (window.parent) {
window.parent.postMessage({ type: 'vite:hmr:error', data }, '*');
}
});
import.meta.hot.on('vite:beforeUpdate', (data) => {
if (window.parent) {
window.parent.postMessage({ type: 'vite:hmr:beforeUpdate', data }, '*');
}
});
import.meta.hot.on('vite:afterUpdate', (data) => {
if (window.parent) {
window.parent.postMessage({ type: 'vite:hmr:afterUpdate', data }, '*');
}
});
}
}
`;
}
},
},
],
});