-
Notifications
You must be signed in to change notification settings - Fork 9
/
gulpfile.ts
116 lines (101 loc) · 2.56 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
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"
const sass = require("gulp-sass")(require("sass"))
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/ui/index.scss")
.pipe(gulpIf(debug, sourcemaps.init()))
.pipe(
sass
.sync({ outputStyle: debug ? "expanded" : "compressed" })
.on("error", sass.logError)
)
.pipe(concat("webserial.css"))
.pipe(gulpIf(debug, sourcemaps.write(".")))
.pipe(gulp.dest("dist/"))
}
function resCopy() {
return gulp.src("src/ui/*.html").pipe(gulp.dest("dist/"))
}
function js(debug?: boolean, src?: string) {
const sources = {
"background": "webserial.background",
"content": "webserial.content",
"polyfill": "webserial.polyfill",
"ui": "webserial.ui",
}
return gulp
.src(src || Object.keys(sources).map((s) => `src/${s}.ts`))
.pipe(
bro({
debug: debug,
cacheFile: "browserify-cache.json",
plugin: [[tsifyBabelify, { debug }]],
})
)
.pipe(gulpIf(debug, sourcemaps.init({ loadMaps: true })))
.pipe(gulpIf(!debug, uglify()))
.pipe(
rename((opt) => {
opt.basename = sources[opt.basename]
opt.extname = ".js"
})
)
.pipe(gulpIf(debug, sourcemaps.write(".")))
.pipe(gulp.dest("dist/"))
}
gulp.task("css", () => {
return css(false)
})
gulp.task("css:dev", () => {
return css(true)
})
gulp.task("css:watch", () => {
return gulp.watch(
["src/**/*.scss"],
{ ignoreInitial: false },
gulp.task("css:dev")
)
})
gulp.task("html", () => {
return resCopy()
})
gulp.task("html:dev", () => {
return resCopy()
})
gulp.task("html:watch", () => {
return gulp.watch(
["src/ui/*.html"],
{ ignoreInitial: false },
gulp.task("html: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) => {
gulp.series(gulp.task("js:dev"))(null)
})
})
gulp.task("build", gulp.parallel("css", "js", "html"))
gulp.task("build:dev", gulp.parallel("css:dev", "js:dev", "html:dev"))
gulp.task("watch", gulp.parallel("css:watch", "js:watch", "html:watch"))