-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathgulpfile.js
233 lines (200 loc) · 5.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
232
233
const gulp = require("gulp");
const gutil = require("gulp-util");
const through = require("through2");
const strip = require("gulp-strip-code");
const rename = require("gulp-rename");
const gzip = require("gulp-gzip");
const brotli = require("gulp-brotli");
const babel = require("gulp-babel");
const uglify = require("gulp-uglify-es").default;
const clone = require("gulp-clone");
const gulpMerge = require("gulp-merge");
const path = require("path");
const useBrotli = process.env.ENABLE_BROTLI || false;
const uglifyOptions = {
ecma: 6,
mangle: {
reserved: [],
toplevel: true
}
};
const plugins = [
"keyed"
];
/**
* a gulp transformer that keeps the smallest file and discard the rest
* @returns {*}
*/
const keepSmallest = () => {
const files = [];
function transform(file, encoding, callback) {
files.push(file);
callback();
}
function flush() {
files.sort(function(a, b) {
const sizeA = a.contents.byteLength;
const sizeB = b.contents.byteLength;
return sizeA < sizeB ? -1 : sizeA > sizeB ? 1 : 0;
});
files.forEach((file, index) => {
const size = index
? gutil.colors.red(file.contents.byteLength)
: gutil.colors.green(file.contents.byteLength);
gutil.log(gutil.colors.magenta(path.basename(file.path)), size);
});
this.push(files[0]);
}
return through.obj(transform, flush);
};
/**
* Trims the tailing ';' to save an extra byte
*/
const trimSemicolon = () => {
function filter(file, enc, cb) {
file.contents = file.contents.slice(0, -1);
cb(null, file);
}
return through.obj(filter);
};
/**
* Strips out the NPM-GLUE armor from the given source file
*/
const getBaseStream = (src) => {
return gulp.src(src).pipe(
strip({
start_comment: "BEGIN NPM-GLUE",
end_comment: "END NPM-GLUE"
})
);
};
/**
* Wraps the input stream with uglify
*/
const getUglifyStream = baseStream =>
baseStream
.pipe(clone())
.pipe(uglify(uglifyOptions))
.on("error", function(err) {
gutil.log(gutil.colors.red("[Error]"), err.toString());
})
.pipe(trimSemicolon())
.pipe(
rename({
suffix: "-uglify"
})
);
/**
* Wraps the input stream with minify
*/
const getMinifyStream = baseStream =>
baseStream
.pipe(clone())
.pipe(
babel({
presets: ["minify"],
comments: false
})
)
.on("error", function(err) {
gutil.log(gutil.colors.red("[Error]"), err.toString());
})
.pipe(trimSemicolon())
.pipe(
rename({
suffix: "-minify"
})
);
/**
* Builds the given source file to the specified destination as a source stream
*/
const getBuildSource = (srcName, dstName) => {
const baseStream = getBaseStream(srcName);
const uglifyStream = getUglifyStream(baseStream);
const minifyStream = getMinifyStream(baseStream);
return gulpMerge(
uglifyStream,
minifyStream,
null
)
.pipe(keepSmallest())
.pipe(clone())
.pipe(
rename({
basename: dstName
})
)
.pipe(gulp.dest("./dist"));
}
/**
* Builds the given source file to the specified destination as a compressed stream,
* picking the best out of the compression matrix available.
*/
const getBuildCompressed = (srcName, dstName, gzipOptions={level: 9}) => {
const baseStream = getBaseStream(srcName);
const uglifyStream = getUglifyStream(baseStream);
const minifyStream = getMinifyStream(baseStream);
const streams = [
uglifyStream.pipe(clone()).pipe(gzip({ gzipOptions })),
minifyStream.pipe(clone()).pipe(gzip({ gzipOptions })),
];
if (useBrotli) {
streams.push(
uglifyStream.pipe(clone()).pipe(brotli.compress()),
minifyStream.pipe(clone()).pipe(brotli.compress()),
);
}
streams.push(null);
return gulpMerge.apply(null, streams)
.pipe(keepSmallest())
.pipe(clone())
.pipe(
rename({
basename: dstName
})
)
.pipe(gulp.dest("./dist"));
}
gulp.task("build:plugins:js", () => {
const args = plugins.map(name => getBuildSource(
"src/plugins/" + name + ".js",
"plugin-" + name + ".min"
));
args.push(null);
return gulpMerge.apply(null, args);
});
gulp.task("build:plugins:gz", () => {
const args = plugins.map(name => getBuildCompressed(
"src/plugins/" + name + ".js",
"plugin-" + name + ".min"
));
args.push(null);
return gulpMerge.apply(null, args);
});
gulp.task("build:js", () => {
return getBuildSource(
"src/dotdom.js",
"dotdom.min"
);
});
gulp.task("build:gz", () => {
return getBuildCompressed(
"src/dotdom.js",
"dotdom.min"
);
});
gulp.task("build:plugins", ["build:plugins:js", "build:plugins:gz"]);
gulp.task("build", ["build:js", "build:gz", "build:plugins"]);
gulp.task("watch", () => {
const watcher = gulp.watch("./src/**/*.js", ["build:js"]);
watcher.on("change", function(event) {
gutil.log(
"File",
gutil.colors.magenta(event.path),
"was",
gutil.colors.cyan(event.type)
);
});
return watcher;
});
gulp.task("default", ["build", "watch"]);