-
Notifications
You must be signed in to change notification settings - Fork 11
/
gulpfile.js
115 lines (89 loc) · 2.88 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
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
var gulp = require('gulp');
var browserify = require('browserify');
var transform = require('vinyl-transform');
var source = require('vinyl-source-stream');
var watchify = require( 'watchify' )
var babel = require( 'babelify' )
var gutil = require( 'gulp-util' )
const through = require('through2')
const sourcemaps = require( 'gulp-sourcemaps' )
gulp.task('build', function () {
var b = browserify({
entries: './js/index.js'
}).transform( babel.configure({ sourceMaps:false, presets:['es2015']}) )
b.bundle()
.pipe( source('gibberwocky.js') )
.pipe( gulp.dest( './dist/' ) )
//.pipe( through.obj((chunk, enc, cb) => {
// generateHTML( cb )
// //cb(null, chunk)
//}))
//b.pipe( source('index.js') ).pipe( gulp.dest( './' ) )
});
watchify.args.entries = './js/index.js'
watchify.args.debug = true
//watchify.args.debug = true
var b = watchify(
browserify( watchify.args )
//.transform( babel.configure({ sourceMaps:true, presets:['es2015']} ) )
)
b.on( 'update', bundle )
b.on( 'log', gutil.log )
gulp.task('default', bundle)
function bundle() {
const stream = b.bundle()
.on("error", function(err) {
gutil.log("Browserify error:", err);
})
.pipe( source('index.js') )
.pipe( gulp.dest( './dist/' ) )
.pipe( through.obj((chunk, enc, cb) => {
generateHTML( cb )
//cb(null, chunk)
}))
return stream
}
const generateHTML = function( cb ) {
const fs = require("fs")
const readfilename = 'index_template.html'
const writefilename = 'index.html'
const css_sources = [
"./css/codemirror.css",
"./css/show-hint.css",
"./css/vanillatree.css",
"./css/tabs.microlib.css",
"./css/gibber.css"
]
const js_sources = [
"./dist/index.js",
]
const css_start_str = "<!-- css_insert_start -->"
const css_end_str = "<!-- css_insert_end -->"
const js_start_str = "<!-- js_insert_start -->"
const js_end_str = "<!-- js_insert_end -->"
let result = fs.readFileSync(readfilename, "utf-8")
let css = css_start_str + "\n<style>\n";
for (let f of css_sources) {
css += fs.readFileSync(f, "utf-8")
}
css += "\n</style>\n" + css_end_str
let js = js_start_str + "\n<script>\n"
for (let f of js_sources) {
js += fs.readFileSync(f, "utf-8")
}
js += "\n</script>\n" + js_end_str
// find our insertion points:
const css_start = result.indexOf(css_start_str)
const css_end = result.indexOf(css_end_str) + css_end_str.length
const js_start = result.indexOf( js_start_str)
const js_end = result.indexOf( js_end_str) + js_end_str.length
if (css_start >= 0 && css_end >= 0 && js_start >= 0 && js_end >= 0) {
const a = result.slice(0, css_start)
const b = result.slice(css_end, js_start)
const c = result.slice(js_end)
const output = a + css + b + js + c
//console.log(result);
fs.writeFileSync( writefilename, output )
}
cb( null, null )
}