This repository was archived by the owner on Apr 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgulpfile.js
More file actions
239 lines (226 loc) · 5.63 KB
/
gulpfile.js
File metadata and controls
239 lines (226 loc) · 5.63 KB
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
232
233
234
235
236
237
238
239
"use strict";
var browserSync = require("browser-sync").create();
var changed = require("gulp-changed");
var sass = require("gulp-sass");
var ejs = require("gulp-ejs");
var fs = require("fs");
var gulp = require("gulp");
var KarmaServer = require("karma").Server;
var notify = require("gulp-notify");
var path = require("path");
var plumber = require("gulp-plumber");
var rename = require("gulp-rename");
sass.compiler = require("node-sass");
// Parameters
var SRC_DIR = "src";
var DEST_DIR = "build";
var DIRS = {
fonts: { src: "fonts" },
html: { src: "html", dest: "", srcPattern: "*.ejs" },
css: { src: "scss", dest: "css", srcPattern: "*.scss" },
resources: {
src: "../node_modules/vivliostyle/resources",
dest: "resources"
},
mathjax: { src: "../node_modules/mathjax", dest: "mathjax" },
plugin_resources: {
src: "../node_modules/vivliostyle/plugins/*/resources",
dest: "plugins"
}
};
var VIVLIOSTYLE_JS_SRC_DIR = "node_modules/vivliostyle/src";
var HTML_FILENAMES = {
production: "vivliostyle-viewer.html",
development: "vivliostyle-viewer-dev.html"
};
function getVersion(basePath) {
var version = JSON.parse(fs.readFileSync(basePath + "package.json", "utf8"))
.version;
return version.replace(/\.0$/, "");
}
var versions = {
core: getVersion("node_modules/vivliostyle/"),
ui: getVersion("")
};
// Utility functions
function destDir(type) {
var dirs = DIRS[type];
return (
DEST_DIR + "/" + (typeof dirs.dest === "string" ? dirs.dest : dirs.src)
);
}
function srcPattern(type) {
return (
SRC_DIR + "/" + DIRS[type].src + "/" + (DIRS[type].srcPattern || "**/*")
);
}
// create a task simply copying files
function copyTask(type) {
var dest = destDir(type);
return gulp.task("build:" + type, function() {
return gulp
.src(srcPattern(type))
.pipe(changed(dest))
.pipe(gulp.dest(dest));
});
}
copyTask("fonts");
copyTask("resources");
copyTask("mathjax");
copyTask("plugin_resources");
// HTML build
function buildHtml(development) {
return gulp
.src(srcPattern("html"))
.pipe(ejs({ development: development, versions: versions }))
.pipe(
plumber({
errorHandler: notify.onError("Error: <%= error.message %>")
})
)
.pipe(
rename(
development ? HTML_FILENAMES.development : HTML_FILENAMES.production
)
)
.pipe(gulp.dest(destDir("html")));
}
gulp.task("build:html", function() {
return buildHtml(false);
});
gulp.task("build:html-dev", function() {
return buildHtml(true);
});
// CSS build
function buildCss(development) {
return gulp
.src(srcPattern("css"))
.pipe(
plumber({
errorHandler: notify.onError("Error: <%= error.message %>")
})
)
.pipe(
sass({
outputStyle: development ? "expanded" : "compressed"
}).on("error", sass.logError)
)
.pipe(gulp.dest(path.resolve(destDir("css"))));
}
gulp.task("build:css", function() {
return buildCss(false);
});
gulp.task("build:css-dev", function() {
return buildCss(true);
});
// build all
gulp.task(
"build",
gulp.parallel(
"build:html",
"build:fonts",
"build:resources",
"build:plugin_resources",
"build:css"
)
);
gulp.task(
"build-dev",
gulp.parallel(
"build:html-dev",
"build:fonts",
"build:resources",
"build:plugin_resources",
"build:css-dev"
)
);
// watch
gulp.task("start-watching", function(done) {
done();
});
gulp.task(
"watch",
gulp.series(gulp.parallel("start-watching", "build"), function(done) {
gulp.watch(srcPattern("html"), gulp.task("build:html"));
gulp.watch(srcPattern("fonts"), gulp.task("build:fonts"));
gulp.watch(srcPattern("resources"), gulp.task("build:resources"));
gulp.watch(
srcPattern("plugin_resources"),
gulp.task("build:plugin_resources")
);
gulp.watch(srcPattern("css"), gulp.task("build:css"));
done();
})
);
gulp.task(
"watch-dev",
gulp.series(gulp.parallel("start-watching", "build-dev"), function(done) {
gulp.watch(srcPattern("html"), gulp.task("build:html-dev"));
gulp.watch(srcPattern("fonts"), gulp.task("build:fonts"));
gulp.watch(srcPattern("resources"), gulp.task("build:resources"));
gulp.watch(
srcPattern("plugin_resources"),
gulp.task("build:plugin_resources")
);
gulp.watch(srcPattern("css"), gulp.task("build:css-dev"));
done();
})
);
// serve
function serve(development) {
browserSync.init({
browser:
process.platform === "darwin"
? "google chrome"
: process.platform === "win32"
? "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
: "chromium-browser",
server: {
baseDir: "../"
},
startPath: "/vivliostyle.js/test/files/"
});
var target = [DEST_DIR + "/**/*"];
if (development) {
target.push(VIVLIOSTYLE_JS_SRC_DIR + "/**/*");
}
gulp.watch(target).on("change", browserSync.reload);
}
gulp.task(
"serve",
gulp.series("watch", function(done) {
serve(false);
done();
})
);
gulp.task(
"serve-dev",
gulp.series("watch-dev", function(done) {
serve(true);
done();
})
);
gulp.task("default", gulp.task("serve-dev"));
// test
gulp.task("test-local", function(done) {
var server = new KarmaServer(
{
configFile: process.cwd() + "/test/conf/karma-local.conf"
},
function(exitStatus) {
done(exitStatus ? "Some tests failed" : undefined);
}
);
server.start();
});
gulp.task("test-sauce", function(done) {
var server = new KarmaServer(
{
configFile: process.cwd() + "/test/conf/karma-sauce.conf"
},
function(exitStatus) {
done(exitStatus ? "Some tests failed" : undefined);
}
);
server.start();
});