Repository navigation
Expand file tree
/
Copy pathvite-spa-dev.ts
More file actions
326 lines (282 loc) · 13.1 KB
/
Copy pathvite-spa-dev.ts
File metadata and controls
326 lines (282 loc) · 13.1 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import chokidar, { FSWatcher } from 'chokidar';
import { ResolvedConfig, Plugin } from 'vite';
import { WebSocketServer } from 'ws';
import { createServer, Server } from "https";
import { existsSync, statSync, createReadStream, readFileSync } from 'fs';
import mime from 'mime';
import path from 'path';
import { EventName } from 'chokidar/handler.js';
import { createHash } from 'crypto';
export const chunkFromId = (id: string) => {
return `${id.split('/')
.slice(
id.split('/').indexOf('src'),
id.split('/').length + 1
)
.join('_')
.replaceAll('.tsx', '')
.replaceAll('.ts', '')}`;
}
export type HMRPluginOptions = {
targetFilePath: string;
server: {
https?: boolean;
host: string;
port: number;
hmrTopic: string;
}
}
type WatcherPayload = {
topic: string;
changeType: EventName;
path: string;
rootComponentPath: string;
}
const enum ChangeTypes {
ADD = 'add',
UNLINK = 'unlink',
CHANGE = 'change',
}
let fileHashes: { [key: string]: string } = {};
function getFileHash(filePath: string): string {
const fileBuffer = readFileSync(filePath);
const hashSum = createHash('sha256');
hashSum.update(fileBuffer);
return hashSum.digest('hex');
}
export const ViteSpaDev = (opt: HMRPluginOptions): Plugin => {
const options = opt;
let config: ResolvedConfig;
let wsServer: WebSocketServer;
let fileWatcher: FSWatcher;
let server: Server;
return {
name: 'hmr-dev',
apply: 'build',
configResolved(resolved: ResolvedConfig) {
config = resolved;
},
async transform(code, id) {
const packageJSON = await import('./package.json');
/**
* Patch the styling by adding the extension's name
* This is required to avoid overwriting the global styles
*/
if (id.endsWith('.css')) {
const transformed: string[] = [];
for (const line of code.split('\n')) {
let newLine: string = line;
if (line === ':root {') {
newLine = `#${packageJSON.name} {`;
}
if (line === '.dark {') {
newLine = `.dark #${packageJSON.name} {`;
}
transformed.push(newLine);
}
return `${transformed.join('\n')}`;
}
if (id.endsWith(options.targetFilePath)) {
return `
${code}
if (__DEV__) {
const safeSystemDelete = (path) => {
if (System.has(path)) {
System.delete(path);
}
}
let debounceTimeout = null;
const debounce = (fn, delay) => {
return (...args) => {
clearTimeout(debounceTimeout);
debounceTimeout = setTimeout(() => fn(...args), delay);
}
}
const projectName = '${packageJSON.name}';
const wsNamespace = '${packageJSON.name}_ws';
const listenHMR = async () => {
global[wsNamespace] = new WebSocket('${options.server.https ? 'wss' : 'ws'}://localhost:${options.server.port}');
global[wsNamespace].addEventListener('open', async () => {
console.info('HMR - enabled. Make sure you create an app with Extension ID: ${packageJSON.name}');
console.info('Please consult the docs for more info: https://docs.akasha.world/devkit/');
});
global[wsNamespace].addEventListener('error', (event) => {
console.warn('HMR - WebSocket failed to connect');
});
if (!System) return;
const sspa = await System.import(System.resolve('single-spa'));
if (!sspa) {
console.warn('Cannot update modules. Required package "single-spa" not found');
return;
}
const reloadApp = debounce(async () => {
await sspa.unloadApplication(projectName);
}, 250);
global[wsNamespace].addEventListener('message', async (event) => {
try {
const { topic, changeType, path, rootComponentPath } = JSON.parse(event.data);
if (topic === '${options.server.hmrTopic}') {
safeSystemDelete(path);
if (changeType === '${ChangeTypes.ADD}' || changeType === '${ChangeTypes.CHANGE}') {
await System.import(path);
}
if (changeType !== '${ChangeTypes.UNLINK}') {
safeSystemDelete(rootComponentPath);
reloadApp();
}
}
} catch(err) {
console.warn('HMR - failed to reload.', err);
}
});
global[wsNamespace].addEventListener('close', () => {
console.log('HMR - connection closed. will retry in 3s');
setTimeout(() => {
listenHMR();
}, 3000);
});
}
if (typeof WebSocket !== 'undefined' && typeof global[wsNamespace] === 'undefined') {
listenHMR();
}
}
`;
}
return code;
},
async closeBundle() {
const isHTTPSServer = Boolean(options.server.https);
const srvURI = 'localhost';
const srvUrl = `${isHTTPSServer ? 'https://' : 'http://'}${srvURI}:${options.server.port}`;
const packageJSON = await import('./package.json');
let { output } = config.build.rollupOptions;
if (output && Array.isArray(output)) {
output = output[0];
}
const entryFile = output?.entryFileNames;
const outDir = config.build.outDir;
const mainFile = `${srvUrl}/${outDir}/${entryFile}`;
const rootComponentPath = `${srvUrl}/${outDir}/${chunkFromId(`src/${options.targetFilePath}`)}.js`;
if (!wsServer) {
server = createServer({
cert: config.server.https?.cert,
key: config.server.https?.key
}, (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
if (!req.url) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
return res.end(`File not found: ${req.url}`);
}
let filePath = path.join(process.cwd(), req.url);
if (existsSync(filePath) && statSync(filePath).isFile()) {
// wrong or no mimetype messes up with system (or single-spa) :|
res.writeHead(200, { 'Content-Type': mime.getType(filePath) || 'application/octet-stream' });
createReadStream(filePath)
.pipe(res);
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end(`File not found: ${req.url}`);
}
});
wsServer = new WebSocketServer({ server });
const rollupOutput = config.build.rollupOptions.output;
if (typeof rollupOutput === 'object' && !Array.isArray(rollupOutput)) {
fileWatcher = chokidar.watch(`${config.build.outDir}`, {
usePolling: true,
alwaysStat: true,
awaitWriteFinish: true,
interval: 50,
});
fileWatcher.on('add', (path, stats) => {
const fileHash = getFileHash(path);
fileHashes[path] = fileHash;
wsServer.clients.forEach(wsClient => {
if (wsClient.readyState === 1) {
const payload: WatcherPayload = {
topic: options.server.hmrTopic,
changeType: ChangeTypes.ADD,
path: `${srvUrl}/${path}`,
rootComponentPath,
}
wsClient.send(JSON.stringify(payload));
}
})
});
fileWatcher.on('change', (path) => {
const currentFileHash = getFileHash(path);
if (fileHashes[path] && currentFileHash === fileHashes[path]) return;
fileHashes[path] = currentFileHash;
wsServer.clients.forEach(wsClient => {
if (wsClient.readyState === 1) {
const payload: WatcherPayload = {
topic: options.server.hmrTopic,
changeType: ChangeTypes.CHANGE,
path: `${srvUrl}/${path}`,
rootComponentPath
}
wsClient.send(JSON.stringify(payload));
}
})
});
fileWatcher.on('unlink', (path) => {
delete fileHashes[path];
wsServer.clients.forEach(wsClient => {
if (wsClient.readyState === 1) {
const payload: WatcherPayload = {
topic: options.server.hmrTopic,
changeType: ChangeTypes.UNLINK,
path: `${srvUrl}/${path}`,
rootComponentPath,
}
wsClient.send(JSON.stringify(payload));
}
});
});
}
process.on('SIGINT', () => {
console.info('\nHMR - Stopping processes');
fileWatcher.close();
wsServer.close();
process.exit();
});
server.listen(options.server.port, options.server.host, () => {
console.info(
`************************* EXTENSION DEVKIT *************************`,
);
console.log('');
console.log(
'\x1b[36m%s\x1b[0m',
'Documentation: \x1b[0m',
'https://docs.akasha.world/devkit',
);
console.log('');
console.log(
'\x1b[36m%s\x1b[0m',
'Server: \x1b[0m',
`${options.server.https ? 'https' : 'http'}://${options.server.host}:${
options.server.port
}`,
);
console.log(
'\x1b[36m%s\x1b[0m',
'Extension ID: \x1b[0m',
`${packageJSON.name}`,
);
console.log('\x1b[36m%s\x1b[0m', 'MainFile: \x1b[0m', `${mainFile}`);
console.log('');
console.log(
'\x1b[33m%s\x1b[0m',
'Please open MainFile url (from above) in your browser to make sure that the index file loads. \x1b[0m',
);
console.log('');
console.log(
'*********************************************************************',
);
});
}
}
}
}