forked from scrollback/scrollback
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
231 lines (207 loc) · 6.02 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Load plugins and declare variables
var gulp = require("gulp"),
del = require("del"),
bower = require("bower"),
browserify = require("browserify"),
source = require("vinyl-source-stream"),
buffer = require("vinyl-buffer"),
es = require("event-stream"),
lazypipe = require("lazypipe"),
plumber = require("gulp-plumber"),
gutil = require("gulp-util"),
jshint = require("gulp-jshint"),
concat = require("gulp-concat"),
striplogs = require("gulp-strip-debug"),
uglify = require("gulp-uglify"),
rename = require("gulp-rename"),
sass = require("gulp-ruby-sass"),
autoprefixer = require("gulp-autoprefixer"),
minify = require("gulp-minify-css"),
manifest = require("gulp-manifest"),
config = require("./config.js"),
debug = !(gutil.env.production || config.env === "production"),
dirs = {
bower: "bower_components",
lib: "public/s/scripts/lib",
lace: "public/s/styles/lace",
css: "public/s/styles/dist"
},
files = {
js: [
"*/*-client.js",
"lib/*.js", "ui/*.js",
"public/client.js", "public/libsb.js", "client-init/*.js",
"client-entityloader/*.js", "localStorage/*.js", "socket/*.js", "interface/*.js"
],
css: [ "public/s/styles/scss/*.scss" ]
};
// Make browserify bundle
function bundle(files, opts) {
var streams = [],
bundler = function(file) {
opts.entries = "./" + file;
return browserify(opts).bundle()
.pipe(source(file.split(/[\\/]/).pop()))
.on("error", gutil.log);
};
opts = opts || {};
if (files && files instanceof Array) {
for (var i = 0, l = files.length; i < l; i++) {
if (typeof files[i] === "string") {
streams.push(bundler(files[i]));
}
}
} else if (typeof files === "string") {
streams.push(bundler(files));
}
return es.merge.apply(null, streams).pipe(buffer());
}
// Add prefix in an array
function prefix(str, arr) {
var prefixed = [];
if (!(arr && arr instanceof Array)) {
return arr;
}
for (var i = 0, l = arr.length; i < l; i++) {
prefixed.push(str + arr[i]);
}
return prefixed;
}
// Lazy pipe for building scripts
var buildscripts = lazypipe()
.pipe(plumber)
.pipe(!debug ? uglify : gutil.noop)
.pipe(!debug ? striplogs : gutil.noop);
// Lint JavaScript files
gulp.task("lint", function() {
return gulp.src([
"*/*{.js,/*.js,/*/*.js}",
"!*/*{.min.js,/*.min.js,/*/*.min.js}",
"!node_modules{,/**}", "!bower_components{,/**}"
])
.pipe(plumber())
.pipe(jshint())
.pipe(jshint.reporter("jshint-stylish"))
.on("error", gutil.log);
});
// Install and copy third-party libraries
gulp.task("bower", function() {
return bower.commands.install([], { save: true }, {})
.on("error", gutil.log);
});
gulp.task("libs", [ "bower" ], function() {
return gulp.src(prefix(dirs.bower + "/", [
"jquery/dist/jquery.min.js",
"lace/src/js/*.js",
"sockjs/sockjs.min.js",
"svg4everybody/svg4everybody.min.js",
"velocity/jquery.velocity.min.js",
"velocity/velocity.ui.min.js"
]))
.pipe(plumber())
.pipe(gulp.dest(dirs.lib))
.on("error", gutil.log);
});
// Copy and minify polyfills
gulp.task("polyfills", [ "bower" ], function() {
return gulp.src(prefix(dirs.bower + "/", [
"flexie/dist/flexie.min.js",
"transformie/transformie.js"
]))
.pipe(buildscripts())
.pipe(concat("polyfills.js"))
.pipe(gulp.dest(dirs.lib))
.pipe(rename({ suffix: ".min" }))
.pipe(gulp.dest(dirs.lib))
.on("error", gutil.log);
});
// Build browserify bundles
gulp.task("bundle", [ "libs" ], function() {
return bundle([ "libsb.js", "client.js" ], { debug: debug })
.pipe(buildscripts())
.pipe(rename({ suffix: ".bundle.min" }))
.pipe(gulp.dest("public/s/scripts"))
.on("error", gutil.log);
});
// Generate embed widget script
gulp.task("embed", function() {
return bundle("embed/embed-parent.js", { debug: debug })
.pipe(buildscripts())
.pipe(rename("embed.min.js"))
.pipe(gulp.dest("public"))
.pipe(rename("client.min.js"))
.pipe(gulp.dest("public"))
.on("error", gutil.log);
});
// Generate scripts
gulp.task("scripts", [ "polyfills", "bundle", "embed" ]);
// Generate styles
gulp.task("lace", [ "bower" ], function() {
return gulp.src(dirs.bower + "/lace/src/scss/*.scss")
.pipe(plumber())
.pipe(gulp.dest(dirs.lace))
.on("error", gutil.log);
});
gulp.task("styles", [ "lace" ], function() {
return gulp.src(files.css)
.pipe(sass({
style: !debug ? "compressed" : "expanded",
sourcemapPath: "../scss"
}))
.on("error", function(e) { gutil.log(e.message); })
.pipe(plumber())
.pipe(!debug ? autoprefixer() : gutil.noop())
.pipe(!debug ? minify() : gutil.noop())
.pipe(gulp.dest(dirs.css))
.on("error", gutil.log);
});
// Generate appcache manifest file
gulp.task("manifest", function() {
var clientConfig = require("./client-config.js"),
protocol = clientConfig.server.protocol,
host = clientConfig.server.host,
domain = protocol + host;
return gulp.src(prefix("public/s/", [
"lib/jquery.min.js",
"scripts/client.bundle.min.js",
"styles/dist/client.css",
"img/client/**/*"
]))
.pipe(manifest({
basePath: "public",
prefix: domain,
cache: [
domain + "/client.html",
protocol + "//fonts.googleapis.com/css?family=Open+Sans:400,600",
protocol + "//fonts.gstatic.com/s/opensans/v10/cJZKeOuBrn4kERxqtaUH3T8E0i7KZn-EPnyo3HZu7kw.woff",
protocol + "//fonts.gstatic.com/s/opensans/v10/MTP_ySUJH_bn48VBG8sNSnhCUOGz7vYGh680lGh-uXM.woff"
],
network: [ "*" ],
fallback: [
protocol + "//gravatar.com/avatar/ " + domain + "/s/img/client/avatar-fallback.svg",
domain + "/socket " + domain + "/s/socket-fallback",
domain + "/ " + domain + "/client.html"
],
preferOnline: true,
timestamp: true,
filename: "manifest.appcache"
}))
.pipe(gulp.dest("public"))
.on("error", gutil.log);
});
// Clean up generated files
gulp.task("clean", function() {
return del([
"public/{*.map,**/*.map}",
"public/{*.min.js,**/*.min.js}",
"public/{*.bundle.js,**/*.bundle.js}",
"public/{*.appcache,**/*.appcache}",
dirs.lib, dirs.css, dirs.lace
]);
});
gulp.task("watch", function() {
gulp.watch(files.js, [ "scripts", "manifest" ]);
gulp.watch(files.css, [ "styles", "manifest" ]);
});
// Default Task
gulp.task("default", [ "scripts", "styles", "manifest" ]);