-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.ts
133 lines (119 loc) · 3.31 KB
/
gulpfile.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
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
import gulp from "gulp"
import sourcemaps from "gulp-sourcemaps"
import concat from "gulp-concat"
import { BrowserifyObject } from "browserify"
import uglify from "gulp-uglify"
import rename from "gulp-rename"
import gulpIf from "gulp-if"
import bro from "gulp-bro"
import transform from "gulp-transform"
import { File } from "gulp-util"
const sass = require("gulp-sass")(require("sass"))
const globals = {
"react": "Spicetify.React",
"react-dom": "Spicetify.ReactDOM",
}
function tsifyBabelify(b: BrowserifyObject, opts: { debug: boolean }) {
b.plugin("tsify")
b.transform("babelify", {
presets: ["@babel/preset-typescript", "@babel/preset-react"],
extensions: [".ts", ".tsx"],
sourceMaps: opts.debug,
})
}
function css(debug?: boolean) {
return gulp
.src("src/scss/style.scss")
.pipe(gulpIf(debug, sourcemaps.init()))
.pipe(
sass
.sync({ outputStyle: debug ? "expanded" : "compressed" })
.on("error", sass.logError)
)
.pipe(concat("beatsaber.css"))
.pipe(gulpIf(debug, sourcemaps.write(".")))
.pipe(gulp.dest("apps/beatsaber/css/"))
}
function resCopy() {
return gulp.src("res/*.css").pipe(gulp.dest("apps/beatsaber/css/"))
}
function js(debug?: boolean, src?: string) {
function rewriteRequire(contents: string, file: File) {
for (const global in globals) {
// @ts-ignore
contents = contents.replaceAll(`require("${global}")`, globals[global])
// @ts-ignore
contents = contents.replaceAll(`require('${global}')`, globals[global])
}
// make shim-xpui export the render() function
if (file.basename == "shim-xpui.ts") {
contents = "const renderRequire=" + contents
contents += "const render = renderRequire(1).default;"
}
return contents
}
const sources = {
"main": "beatsaber.bundle",
"loader": "beatsaber.loader",
"shim-zlink": "beatsaber.shim",
"shim-xpui": "index",
}
return gulp
.src(src || Object.keys(sources).map((s) => `src/${s}.ts`))
.pipe(
bro({
debug: debug,
cacheFile: "browserify-cache.json",
plugin: [[tsifyBabelify, { debug }]],
external: Object.keys(globals),
})
)
.pipe(gulpIf(debug, sourcemaps.init({ loadMaps: true })))
.pipe(transform("utf8", rewriteRequire))
.pipe(gulpIf(!debug, uglify()))
.pipe(
rename((opt) => {
opt.basename = sources[opt.basename]
opt.extname = ".js"
})
)
.pipe(gulpIf(debug, sourcemaps.write(".")))
.pipe(gulp.dest("apps/beatsaber/"))
}
gulp.task("css", () => {
resCopy()
return css(false)
})
gulp.task("css:dev", () => {
resCopy()
return css(true)
})
gulp.task("css:watch", () => {
return gulp.watch(
["src/scss/**/*.scss", "res/*.css"],
{ ignoreInitial: false },
gulp.task("css:dev")
)
})
gulp.task("js", () => {
return js(false)
})
gulp.task("js:dev", () => {
return js(true)
})
gulp.task("js:watch", () => {
gulp.series(gulp.task("js:dev"))(null)
return gulp.watch(["src/**/*.ts", "src/**/*.tsx"]).on("change", (file) => {
if (!file.endsWith("loader.ts") && !file.includes("shim-")) {
file = "src/main.ts"
}
// apparently it does not understand backslashes properly
file = file.replace("\\", "/")
const task = js.bind(this, true, file)
task.displayName = file
gulp.series(task)(null)
})
})
gulp.task("build", gulp.parallel("css", "js"))
gulp.task("build:dev", gulp.parallel("css:dev", "js:dev"))
gulp.task("watch", gulp.parallel("css:watch", "js:watch"))