-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
233 lines (215 loc) Β· 5.21 KB
/
plugin.js
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import {promises as fs} from 'fs';
/**
* @param {string} txt
* @returns {string}
*/
function gray(txt) {
return '\x1b[30m' + txt + '\x1b[0m';
}
/**
* @param {string} txt
* @returns {string}
*/
function blue(txt) {
return '\x1b[36m' + txt + '\x1b[0m';
}
/**
* @param {string} txt
* @returns {string}
*/
function green(txt) {
return '\x1b[32m' + txt + '\x1b[0m';
}
/**
* @typedef {{
* slots: boolean,
* cslots: boolean,
* events: boolean,
* cevents: boolean,
* bubbles: boolean,
* dispatcher: boolean,
* bc: boolean
* }} Stat
*
* @typedef {(stats:Map<string,Stat>)=>string} Generator
*/
/**
* @param {Map<string,Stat>} stats
* @param {string} name
*/
async function generate_report(stats, name) {
let content = `<!doctype html>
<html>
<head>
<title>Svelte4-BC Report</title>
<style>
table {
width: 100%;
border: 1px solid #000;
border-spacing: 0;
}
thead {
position: sticky;
top: 0;
background: #f1f1f1;
color: #000;
border: 1px solid red;
}
thead > tr > th {
border-bottom: 1px solid #000;
}
td, th {
padding: 4px;
text-align: center;
}
td:first-child, th:first-child {
text-align: left;
}
tbody > tr:nth-child(odd) {
background-color: #f9f9f9;
}
tbody > tr:hover {
background-color: #bae1ff;
}
</style>
</head>
<body>
<h1>Svelte4-BC Report</h1>
<table>
<thead>
<tr>
<th>File</th>
<th><slots/></th>
<th>slot="*"</th>
<th>bubbles</th>
<th>on:event</th>
<th>comp on:event</th>
<th>dispatch</th>
<th>BC</th>
</tr>
</thead>
<tbody>
`;
for (const [k, v] of stats.entries()) {
content += `
<tr>
<td>${k}</td>
<td>${v.slots?'π«':'β
'}</td>
<td>${v.cslots?'π«':'β
'}</td>
<td>${v.bubbles?'π«':'β
'}</td>
<td>${v.events?'π«':'β
'}</td>
<td>${v.cevents?'π«':'β
'}</td>
<td>${v.dispatcher?'π«':'β
'}</td>
<td>${v.bc?'π ':''}</td>
</tr>`
}
content += `
</tbody>
</table>
</body>
</html>`;
return await fs.writeFile(name, content);
}
/**
* Svelte4-BC vite plugin
* @param {import("./plugin.js").Svelte4BPluginConfig|boolean} [conf]
* @returns { {
* name: string,
* config:(conf:any,env:any)=>void,
* transform: (code: string, id: string, options?: {ssr?:boolean})=>string|undefined,
* buildStart: ()=>void,
* closeBundle: () => void,
* }}
*/
export function svelte4BCPlugin(conf={}) {
if (typeof conf === 'boolean') {
conf = {logs: conf};
}
const regex = /function (.+)\(\$\$((anchor)|(payload)), \$\$props\) {/;
const prefix = gray('[Svelte4-BC]');
const server = blue('server');
const client = blue('client');
const show_logs = !!conf.logs;
const $$slots_regexp = /\$\$slots: {[^}]*?{/gs
let build = 0;
const gen_report = !!conf.report;
const report_name = './svelte4-bc-report.html'
/** @type {Map<string,Stat>} */
const stats = new Map();
let is_dev = false;
let mode = 'PROD';
return {
name: 'svelte4-bc', // required, will show up in warnings and errors
config: (conf, env) => {
is_dev = env.mode === 'development';
mode = is_dev ? green('DEV') : (blue('PROD' + '/' + conf.build.ssr ? 'SSR' : 'client'));
if (show_logs) {
console.info(`${prefix} Build in ${mode} mode`);
}
},
buildStart() {
build++;
},
async closeBundle() {
build--;
if (build === 0 && gen_report) {
await generate_report(stats, report_name);
if (show_logs) {
console.info(`${prefix} Generate report into ${green(report_name)}`);
}
}
},
transform(code, id, options) {
if (!id.endsWith('.svelte') || id.includes("node_modules")) {
return;
}
const bc = code.includes('\nexport const svelte4_bc =');
if (gen_report) {
const ssr = options?.ssr === true;
let r = stats.get(id);
if (!r) {
r = {slots: false, cslots: false, bubbles: false, events: false, cevents: false, dispatcher: false, bc};
stats.set(id, r);
}
r.slots ||= code.includes('$.slot(');
r.cslots ||= code.match($$slots_regexp)!==null;
if (!ssr) {
r.events ||= code.includes('$.event(');
r.cevents ||= code.includes('$$events:');
r.bubbles ||= code.includes('$.bubble_event.call');
r.dispatcher ||= code.includes('createEventDispatcher');
}
}
if (bc) {
if (show_logs) {
const ssr = options?.ssr === true;
console.info(`${prefix} Transform ${ssr ? server : client}/${mode} for ${blue(id)}`);
}
if (code.includes('$.slot(')) {
throw new Error('Cannot use both `svelte4_bc` and `<slot/>` !');
}
if (code.includes('$.event(')) {
throw new Error('Cannot use both `svelte4_bc` and `on:event` directive !');
}
let import_name = 'svelte4_bc_convert';
let svelte4_bc_convert = import_name;
while (code.includes(svelte4_bc_convert)) {
svelte4_bc_convert += '1';
}
if (svelte4_bc_convert != import_name) {
import_name += ' as ' + svelte4_bc_convert;
}
const componentName = is_dev ? `, "${id.replaceAll('"', '_')}"` : '';
// In dev mode $$props is a proxy, so he must be reassigned
// This is useless in prod mode
const assign = is_dev ? '$$props = ' : '';
return (
`import { ${import_name} } from "svelte4-bc";\n` +
code.replace(regex, (match) => {
return match + `${assign}${svelte4_bc_convert}($$props, svelte4_bc${componentName});`;
})
);
}
}
};
}