-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
52 lines (43 loc) · 1.69 KB
/
gulpfile.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
const g = require('gulp')
const through2 = require('through2')
const concat = require('gulp-concat')
const merge = require('merge-stream')
const path = require('path')
const HTML_START_DELIM = '<!doctype html>'
const HTML_END_DELIM = '</html>'
const SHADERS_TEMPLATE = '{{ SHADERS }}'
function embed_shaders_in_html() {
const a = g.src('src/**/*.vert').pipe(g.src('src/**/*.frag'))
.pipe(through2.obj(function(file, _, cb) {
const src = file.contents.toString()
const new_src = `<script id="${path.basename(file.path).replace('.', '_')}" type="notjs">\n${src}\n</script>`
file.contents = Buffer.from(new_src)
cb(null, file)}))
.pipe(concat('shaders.html.template'))
.pipe(through2.obj(function(file, _, cb) {
const src = file.contents.toString()
cb(null, file)
}))
return merge(a, g.src('src/index.html'))
.pipe(concat('index.html'))
.pipe(through2.obj(function(file, _, cb) {
// ewwww :)
const src = file.contents.toString()
if (src.startsWith(HTML_START_DELIM)) {
const shaders = src.split(HTML_END_DELIM)[1]
const html_template = src.split(HTML_END_DELIM)[0] + HTML_END_DELIM
file.contents = Buffer.from(html_template.replace(SHADERS_TEMPLATE, shaders))
} else {
const shaders = src.split(HTML_START_DELIM)[0]
const html_template = HTML_START_DELIM + src.split(HTML_START_DELIM)[1]
file.contents = Buffer.from(html_template.replace(SHADERS_TEMPLATE, shaders))
}
cb(null, file)
}))
.pipe(g.dest('dist/'))
}
function copy_styles(cb) {
g.src('src/*.css').pipe(g.dest('dist/'))
cb()
}
exports.default = g.parallel(embed_shaders_in_html, copy_styles)