-
Notifications
You must be signed in to change notification settings - Fork 46
/
gulpfile.js
193 lines (167 loc) · 5.47 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
var gulp = require("gulp"),
uglify = require("uglify-js"),
CleanCss = require("clean-css"),
del = require("del"),
through = require("through2"),
vinylMap = require("vinyl-map"), // through2 abstraction for accessing file contents only
path = require("path"),
babel = require("gulp-babel"),
cssnext = require("gulp-cssnext")
paths = {
base: {
root: "",
src: "src/",
dest: "dist/"
},
js: {
srcGlob: [
"node_modules/gulp-babel/node_modules/babel-core/browser-polyfill.min.js",
"src/app-shared/js/libs/**",
"src/app-shared/js/markdown-it-plugins/markdown-it-map-lines.js",
"src/app-shared/js/utilities.js",
"src/app-shared/js/preview.js",
"src/app-shared/js/main.js",
"src/js/**",
],
relDest: "js/all.js",
relRevDest: null
},
css: {
srcGlob: "src/app-shared/css/**",
srcDir: "src/app-shared/css/",
relDest: {
light: "css/bundle-light-theme.css",
dark: "css/bundle-dark-theme.css"
},
relRevDest: {
light: null,
dark: null
}
},
html: {
src: "src/index.html"
},
// Negate a glob string or array
negateGlob: function(glob) {
var negate = function(str) {
// If globstar alone in last path portion, also exclude the empty directory itself
if (str.slice(-3) == "/**") str = str.slice(0, -3) +"{,/**}";
return "!"+ str;
};
if (glob instanceof Array) return glob.map(negate);
else return negate(glob);
}
},
gulpPlugins = {
concat: require("gulp-concat"),
size: require("gulp-size"),
revision: require("gulp-rev"),
htmlReplace: require("gulp-html-replace"),
saveRevFileName: function(assetType, theme) {
var getRevFileName = function(file) {
var sep = "/",
fileBase = file.base.split(path.sep).join(sep), // Replace all without having to escape special regex chars
filePath = file.path.split(path.sep).join(sep);
filePath = filePath.replace(fileBase, "");
if (filePath[0] == sep) filePath = filePath.slice(1); // Make sure this is a relative path
return filePath;
},
write = function(file, enc, cb) {
var revFileName = getRevFileName(file);
if (assetType == "js") paths[assetType].relRevDest = revFileName;
else paths[assetType].relRevDest[theme] = revFileName;
cb(null, file);
};
return through({ objectMode: true }, write);
}
};
// Empty paths.base.dest
gulp.task("clean", function(c) {
del(paths.base.dest +"**", c);
});
// JS files: concat + minify + cache-bust
gulp.task("build-js", ["clean"], function() {
var displayOriginalSize = gulpPlugins.size({
title: "Original JS size"
}),
displayFinalSize = gulpPlugins.size({
title: "Minified + gzipped JS size",
gzip: true
}),
minifyJs = vinylMap(function(contents) {
return uglify.minify(contents.toString(), {
fromString: true
}).code;
});
return gulp.src(paths.js.srcGlob, { base: paths.base.src })
.pipe(gulpPlugins.concat(paths.js.relDest))
.pipe(displayOriginalSize)
.pipe(babel())
.pipe(minifyJs)
.pipe(displayFinalSize)
.pipe(gulpPlugins.revision())
.pipe(gulpPlugins.saveRevFileName("js"))
.pipe(gulp.dest(paths.base.dest));
});
// CSS files: concat + minify + cache-bust, and generate one bundle per theme
var buildThemedCss = function(theme) {
var displayOriginalSize = gulpPlugins.size({
title: "Original CSS size"
});
var displayFinalSize = gulpPlugins.size({
title: "Minified + gzipped CSS size",
gzip: true
});
var minifyCss = vinylMap(function (contents) {
return new CleanCss({
relativeTo: "src/css/", // Path to resolve relative URLs
}).minify(contents.toString()).styles;
});
return gulp.src([
paths.css.srcGlob,
"!src/app-shared/css/themes/**/!(" + theme + "-theme-vars.css)"
], { base: paths.base.src })
.pipe(displayOriginalSize)
.pipe(minifyCss)
.pipe(gulpPlugins.concat(paths.css.relDest[theme]))
.pipe(cssnext())
.pipe(displayFinalSize)
.pipe(gulpPlugins.revision())
.pipe(gulpPlugins.saveRevFileName("css", theme))
.pipe(gulp.dest(paths.base.dest));
};
gulp.task("build-css-theme-light", ["clean"], buildThemedCss.bind(null, "light"));
gulp.task("build-css-theme-dark", ["clean"], buildThemedCss.bind(null, "dark"));
// Revise assets' references. Currently only bothers to do that in index.html, since JS and CSS are the only
// revised assets; will have to extend to CSS files once fonts and images are also revised.
gulp.task("revise-assets-refs", ["build-js", "build-css-theme-light", "build-css-theme-dark"], function() {
return gulp.src(paths.html.src, { base: paths.base.src })
.pipe(gulpPlugins.htmlReplace({
js: paths.js.relRevDest,
cssDefaultTheme: {
src: paths.css.relRevDest.light,
tpl: '<link id="theme-default" href="%s" rel="stylesheet" type="text/css"/>'
},
cssLightThemeRef: {
src: paths.css.relRevDest.light,
tpl: '<link id="theme-light-ref" data-href="%s" rel="stylesheet" type="text/css"/>'
},
cssDarkThemeRef: {
src: paths.css.relRevDest.dark,
tpl: '<link id="theme-dark-ref" data-href="%s" rel="stylesheet" type="text/css"/>'
}
}))
.pipe(gulp.dest(paths.base.dest));
});
// Simply copy the rest of the files from src/ to dist/
gulp.task("copy", ["clean"], function() {
var src = [].concat(
"src/**",
paths.negateGlob(paths.js.srcGlob),
paths.negateGlob(paths.css.srcGlob),
paths.negateGlob(paths.html.src)
);
return gulp.src(src, { base: paths.base.src })
.pipe(gulp.dest(paths.base.dest));
});
gulp.task("default", ["build-js", "build-css-theme-light", "build-css-theme-dark", "revise-assets-refs", "copy"]);