forked from 3meraldK/earthmc-dynmap
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild-userscript.ts
More file actions
55 lines (48 loc) · 2.06 KB
/
build-userscript.ts
File metadata and controls
55 lines (48 loc) · 2.06 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
import { readFileSync, writeFileSync } from 'fs'
import { build, type BuildOptions } from 'esbuild'
import * as path from 'path'
import './src/types.d.ts' // they are global anyway but vscode shits itself sometimes
const STYLE_CSS = readFileSync('resources/style.css', 'utf8')
const BORDERS: Borders = JSON.parse(readFileSync('resources/borders.json', 'utf8'))
const MANIFEST: Manifest = JSON.parse(readFileSync('manifest.json', 'utf8'))
// TODO: Dynamically insert @include tags depending on matches arr count
const contentScripts = MANIFEST.content_scripts[0]
const HEADER = `// ==UserScript==
// @name ${MANIFEST.name}
// @version ${MANIFEST.version}
// @description ${MANIFEST.description}
// @author ${MANIFEST.author}
// @include ${contentScripts.matches[0]}
// @include ${contentScripts.matches[1]}
// @icon https://raw.githubusercontent.com/EarthMC-Toolkit/earthmc-dynmap/main/resources/icon48.png
// @downloadURL https://raw.githubusercontent.com/EarthMC-Toolkit/earthmc-dynmap/main/dist/emc-dynmapplus.user.js
// @grant GM_addStyle
// ==/UserScript==
`
const outdir = 'dist'
const outfile = path.join(outdir, 'emc-dynmapplus.user.js')
const buildOpts: BuildOptions = {
entryPoints: ['resources/interceptor.js', ...contentScripts.js],
outdir: outdir,
bundle: true,
write: false,
format: 'cjs',
target: ['es2020'],
treeShaking: false,
define: {
IS_USERSCRIPT: 'true',
STYLE_CSS: JSON.stringify(STYLE_CSS),
BORDERS: JSON.stringify(BORDERS),
MANIFEST: JSON.stringify(MANIFEST),
window: 'unsafeWindow',
'chrome.runtime.getURL': 'GM_getResourceURL',
},
}
const start = performance.now()
build(buildOpts).then(res => {
const contentCode = res.outputFiles.map(f => f.text).join('\n')
writeFileSync(outfile, `${HEADER}\n${contentCode}`)
const elapsed = (performance.now() - start).toFixed(2)
const relPath = '.' + path.sep + path.relative(process.cwd(), outfile)
console.log(`Successfully generated userscript.\n Output: ${relPath}\n Took: ${elapsed}ms\n`)
})