-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.js
121 lines (110 loc) · 3.62 KB
/
build.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
import { build, context } from 'esbuild';
import sveltePlugin from 'esbuild-svelte';
import { sveltePreprocess } from 'svelte-preprocess';
import postcss from 'postcss';
import postcssLoadConfig from 'postcss-load-config';
import fs from 'fs';
const pkg = JSON.parse(fs.readFileSync("package.json"));
const version = pkg.version;
import { compileAsync } from "sass";
import path from "path";
console.time("Built");
let timerRunning = true;
// unholy amalgamation of esbuild-postcss-inline-styles and esbuild-style-plugin
function importStyles() {
return {
name: "import-styles",
setup(build) {
build.onResolve({ filter: /.\.scss$/ }, (args) => {
return {
path: path.join(args.resolveDir, args.path),
namespace: "import-styles"
}
});
build.onLoad({ filter: /.*/, namespace: "import-styles" }, async (args) => {
const sassed = (await compileAsync(args.path)).css;
const config = await postcssLoadConfig();
const postcssed = await postcss(config.plugins).process(sassed, {
from: args.path
});
const contents = `const styles = ${JSON.stringify(postcssed.css)};\nexport default styles;`;
return {
contents,
loader: "js"
}
})
}
}
}
const meta = `// ==UserScript==
// @name Gimloader
// @description A plugin loader for Gimkit
// @namespace https://github.com/TheLazySquid/Gimloader
// @match https://www.gimkit.com/*
// @match https://thelazysquid.github.io/gimloader*
// @run-at document-start
// @iconURL https://www.gimkit.com/favicon.png
// @author TheLazySquid
// @updateURL https://raw.githubusercontent.com/TheLazySquid/Gimloader/main/build/bundle.user.js
// @downloadURL https://raw.githubusercontent.com/TheLazySquid/Gimloader/main/build/bundle.user.js
// @version ${version}
// @grant unsafeWindow
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_deleteValue
// @grant GM_listValues
// @grant GM.registerMenuCommand
// @grant GM.xmlHttpRequest
// @grant GM_addValueChangeListener
// ==/UserScript==`
let config = {
entryPoints: ["src/index.ts"],
mainFields: ["svelte", "browser", "module", "main"],
conditions: ["svelte", "browser"],
bundle: true,
outfile: "build/bundle.user.js",
plugins: [
sveltePlugin({
preprocess: sveltePreprocess(),
compilerOptions: {
css: "injected"
}
}),
importStyles()
],
loader: {
".svg": "text",
".css": "empty"
},
banner: {
"js": meta
},
minify: true
}
if(process.argv.includes("-w") || process.argv.includes("--watch")) {
config.plugins.push({
name: "rebuild-notify",
setup(build) {
build.onStart(() => {
process.stdout.write("Building...")
if(timerRunning) {
timerRunning = false;
} else {
console.time("Built");
}
})
build.onEnd(result => {
process.stdout.write("\r")
console.timeEnd("Built");
if(result.errors.length > 0) {
console.log("Build finished with", result.errors, "errors");
}
})
}
})
const ctx = await context(config)
await ctx.watch()
} else {
await build(config);
console.timeEnd("Built")
}