-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
156 lines (142 loc) · 3.85 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
// For NPM build
var gulp = require("gulp");
var eslint = require("gulp-eslint");
var sourcemaps = require("gulp-sourcemaps");
var babel = require("gulp-babel");
var replace = require("gulp-replace");
// For browser build
var browserify = require("browserify");
var envify = require("envify/custom");
var source = require("vinyl-source-stream");
var buffer = require("vinyl-buffer");
var uglify = require("gulp-uglify");
var rename = require("gulp-rename");
// For bragging
var ignore = require("gulp-ignore");
var size = require("gulp-size");
var gzip = require("gzip-size");
// To make middleware easier to import
var fs = require("fs");
function generateIndex(directory) {
var cwd = process.cwd();
process.chdir(directory);
var files = recurseDirectories();
// Our index file has a generic structure:
var indexFile = "";
for( var i = 0; i < files.length; i++ ) {
if( files[i] !== "./index.js" && /\.js$/.test(files[i]) ) {
indexFile += "module.exports." + files[i].slice(2, -3) + "Middleware = require(\"" + files[i].slice(0, -3) + "\");\n";
}
}
fs.writeFileSync("index.js", indexFile);
process.chdir(cwd);
}
function recurseDirectories() {
var allFiles = [];
function doRecurse(dir) {
var newFiles = fs.readdirSync(dir);
for( var i = 0; i < newFiles.length; i++ ) {
var fullPath = dir + '/' + newFiles[i];
if( fs.lstatSync(fullPath).isDirectory() ) {
doRecurse(fullPath);
} else {
allFiles.push(fullPath);
}
}
}
doRecurse(".");
return allFiles;
}
var packageObj = JSON.parse(fs.readFileSync("package.json", "utf8"));
gulp.task("lint", function() {
return gulp.src("src/**/*.js")
.pipe(eslint({
rulePaths: ["eslint-rules"]
}))
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
gulp.task("generate-npm-module", ["lint"], function() {
return gulp.src("src/index.js")
.pipe(sourcemaps.init())
.pipe(babel({
presets: ["es2015"]
}))
.pipe(replace("%VERSION%", "v"+packageObj.version))
.pipe(sourcemaps.write("./"))
.pipe(gulp.dest("dist"))
// Compute the minified size (for bragging rights)
.pipe(ignore(/\.map$/))
.pipe(size({
showFiles: true,
showTotal: false
}))
// Good bragging rights should assume production-level compression
.pipe(replace("process.env.NODE_ENV", "\"production\""))
.pipe(uglify())
.pipe(rename({
suffix: ".min"
}))
.pipe(gulp.dest("tmp"))
.pipe(size({
showFiles: true,
showTotal: false,
gzip: true
}));
});
gulp.task("compile-middleware", ["generate-npm-module"], function() {
return gulp.src("src/middleware/*.js")
.pipe(sourcemaps.init())
.pipe(babel({
presets: ["es2015"]
}))
.pipe(replace("%VERSION%", "v"+packageObj.version))
.pipe(sourcemaps.write("./"))
.pipe(gulp.dest("middleware"))
.pipe(ignore(/\.map$/))
.pipe(uglify())
.pipe(size({
showFiles: true,
showTotal: false,
gzip: true
}));
});
gulp.task("generate-browser-module", ["compile-middleware"], function() {
// Now that middleware is compiled we can generate the index
generateIndex("middleware");
return browserify("dist/index.js")
.transform(envify({
NODE_ENV: "development"
}))
.bundle()
.pipe(source("minimux.js"))
.pipe(buffer())
.pipe(sourcemaps.init())
.pipe(gulp.dest("./"))
.pipe(size({
showFiles: true,
showTotal: false
}))
.pipe(uglify())
.pipe(rename({
suffix: ".min"
}))
.pipe(sourcemaps.write("./"))
.pipe(gulp.dest("./"))
.pipe(ignore(/\.map$/))
.pipe(size({
showFiles: true,
showTotal: false,
gzip: true
}));
});
gulp.task("update-readme", ["generate-browser-module"], function() {
var minifiedSize = gzip.sync(fs.readFileSync("tmp/index.min.js", "utf8"));
fs.unlinkSync("tmp/index.min.js");
fs.rmdirSync("tmp");
gulp.src("src/README.md")
.pipe(replace("%VERSION%", "v"+packageObj.version))
.pipe(replace("%SIZE%", minifiedSize))
.pipe(gulp.dest("./"));
});
gulp.task("default", ["update-readme"]);