-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsup.config.ts
79 lines (65 loc) · 2.27 KB
/
tsup.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
import { defineConfig } from 'tsup'
import { WebSocketServer } from 'ws'
import { existsSync } from 'fs'
import { mkdir, readdir, unlink, writeFile, cp, rename } from 'fs/promises'
import { join, resolve } from 'path'
import pkg from './package.json'
const PLUGIN_NAME = pkg.name
const WSS_PORT = 3000
const WSS_URL = `ws://localhost:${WSS_PORT}`
let wss: WebSocketServer | undefined
export default defineConfig((config) => ({
clean: true,
dts: false,
entry: ["src/index.ts"],
outDir: 'dist',
format: 'esm',
bundle: true,
minify: config.watch ? false : true,
shims: false,
splitting: false,
env: {
ENV: config.watch ? 'development' : 'production',
PROD: config.watch ? '' : 'true',
DEV: config.watch ? 'true' : '',
},
async onSuccess() {
if (!config.watch) {
return
}
// Lazily initialize websocket server
if (wss === undefined) {
wss = new WebSocketServer({ port: WSS_PORT })
}
// Get plugin's path
const path = join(resolve(pkg.config.penguPath), 'plugins', PLUGIN_NAME)
// Create a folder if doesn't exist, else empty it
if (existsSync(path)) {
for (const file of await readdir(path)) {
await unlink(join(path, file));
}
} else {
await mkdir(path, { recursive: true });
}
// Copy dist files to plugin directory
await cp('dist', path, { recursive: true });
// Rename original index.js to _index.js
const indexJsPath = join(path, 'index.js')
await rename(indexJsPath, join(path, '_index.js'))
// Prepare our proxy index.js
let indexJS =
`new WebSocket('${WSS_URL}').addEventListener('message', () => location.reload());
export * from './_index.js';`
if (existsSync(join('dist', 'index.css'))) {
indexJS += `import './index.css';`
}
// Write our proxy index.js file
await writeFile(indexJsPath, indexJS)
// Notify all WSS clients that we need to refresh LCU!
wss!.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send('change');
}
});
},
}))