-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
160 lines (150 loc) · 5.7 KB
/
vite.config.ts
File metadata and controls
160 lines (150 loc) · 5.7 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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" && process.env.SHOW_WATERMARK !== "false") {
return [
{
tag: "style",
attrs: { type: "text/css" },
injectTo: "head",
children: `
.srcbook-watermark {
position: fixed;
bottom: 16px;
right: 16px;
background: white;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
display: flex;
align-items: center;
padding: 8px 12px;
z-index: 9999;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
font-size: 14px;
font-weight: bold;
color: #000;
gap: 8px;
border: 1px solid #e6e6e6;
background: linear-gradient(to bottom, #FFFFFF, #F9F9F9);
cursor: pointer;
transition: all 0.2s ease-in-out;
}
.srcbook-watermark:hover {
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.srcbook-watermark:active {
transform: translateY(0);
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.srcbook-watermark img {
width: 16px;
height: 16px;
}
`,
},
{
tag: "script",
attrs: { type: "module" },
injectTo: "body",
children: `
const watermark = document.createElement('a');
watermark.href = 'https://www.srcbook.com?a_id=' + encodeURIComponent('01953363-a7c3-751c-9ec3-a2db8dfc9b6e');
watermark.target = '_blank';
watermark.className = 'srcbook-watermark';
watermark.innerHTML = \`
<img src="https://assets.srcbook.com/favicon.svg" alt="Srcbook Logo" />
Made in Srcbook
\`;
document.body.appendChild(watermark);
`,
},
];
}
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 }, '*');
}
});
}
}
`;
}
},
},
],
server: {
allowedHosts: true,
},
});