-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgulpfile.js
171 lines (167 loc) · 6.07 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
var browserSync = require("browser-sync").create();
var chokidar = require("chokidar");
var gulp = require("gulp");
var $ = require("gulp-load-plugins")();
var proxy = require("proxy-middleware");
var yargs = require("yargs").argv;
var find = require("find");
var crypto = require("crypto");
var path = require("path");
var fs = require("fs");
var url = require("url");
var folders = (folders => {
var names = fs.readdirSync(".");
for (var i = 0; i < names.length; i++) {
var name = names[i];
var state = fs.statSync("./" + name);
if (state.isDirectory() && name != "node_modules" && !name.startsWith(".")) {
folders.push(name);
}
}
return folders;
})([]);
var isDebug = yargs.isDebug == "true";
$.sync(gulp);
function babel(pathname) {
return gulp.src(pathname)
.pipe($.plumber()) // onerror do not stop
.pipe($.if(!isDebug, $.sourcemaps.init())) // sourcemap init
.pipe($.babel()) // ES6 to ES5
.pipe($.if((() => {
if (!/,|\*/g.test(pathname)) {
var data = fs.readFileSync(path.join(__dirname, pathname)).toString();
data = data.replace(/([\/]{2}.*)|(\/\*[\s\*]*.*[\s\*]*\*\/)/gm, ""); //remove comments
return /require\(.{3,}\)/g.test(data); //find require
}
return false;
})(), $.browserify({
insertGlobals: true,
debug: false /*!gulp.env.production*/
})))
.pipe($.if(!isDebug, $.uglify())) // minify js
.pipe($.bom()) // utf-8
.pipe($.rename(pathname => {
pathname.basename = pathname.basename.replace(".es6", "");
})) // rename .es6.js to .js
.pipe($.if(!isDebug, $.sourcemaps.write("."))) // sourcemap write
.pipe($.debug({
title: "es6:"
})) // show filename
.pipe(gulp.dest(file => {
return file.base;
})); //output file
}
function scss(pathname) {
return gulp.src(pathname)
.pipe($.if(!isDebug, $.sourcemaps.init())) // sourcemap init
.pipe($.sass.sync({
includePaths: ["./"], // @import modules
outputStyle: !isDebug ? "compressed" : "expanded", // minify css
errLogToConsole: true
}).on("error", $.sass.logError))
.pipe($.if(!isDebug, $.sourcemaps.write("./"))) // sourcemap write
.pipe($.rename(pathname => {
pathname.basename = pathname.basename.replace(".cscc", ".css");
})) // rename .cscc to .css
.pipe($.debug({
title: "scss:"
})) // show filename
.pipe(gulp.dest(file => {
return file.base;
})); //output file
}
function image(pathname) {
return gulp.src(pathname)
.pipe($.cache($.imagemin({
optimizationLevel: 5, //類型:Number 預設:3 取值範圍:0-7(優化等級)
progressive: true, //類型:Boolean 預設:false 無損壓縮jpg圖片
interlaced: true, //類型:Boolean 預設:false 隔行掃描gif進行渲染
multipass: true //類型:Boolean 預設:false 多次優化svg直到完全優化
})))
.pipe(gulp.dest(file => {
return file.base;
})); //output file
}
gulp.task("image", () => {
return image(folders.map(a => a + "/**/*.{png,jpg,gif,ico}"));
});
function defaultPage(defaultPage) {
defaultPage = defaultPage || "index.html";
return function(req, res, next) {
if (req.url.lastIndexOf("/") !== req.url.length - 1 && !(req.url.split("/").pop().indexOf(".") > -1)) {
res.writeHead(301, {
location: req.url + "/"
});
res.end();
} else if (req.url.lastIndexOf("/") === req.url.length - 1) {
fs.exists("." + req.url + defaultPage, function(exists) {
if (exists) {
var data = fs.readFileSync("." + req.url + defaultPage);
var hash = crypto.createHash('sha1').update(data).digest('base64');
if (req.headers['if-none-match'] == hash) {
res.writeHead(304);
res.end();
return;
}
res.writeHead(200, {
"content-type": "text/html",
"Etag": hash
});
res.write(data);
res.end();
} else {
next();
}
});
} else {
next();
}
};
}
gulp.task("browserSync", () => {
var proxyOptions = url.parse("http://localhost:3000/api");
proxyOptions.route = "/api";
browserSync.init({
server: {
baseDir: "./",
directory: true,
middleware: [proxy(proxyOptions), defaultPage("index.html")]
},
port: 8080
});
});
gulp.task("watching", () => {
folders.forEach(a => {
chokidar.watch(a + "/**/*.es6.js").on("error", () => {}).on("all", (type, file) => {
babel(file);
});
chokidar.watch(a + "/**/*.scss").on("error", () => {}).on("all", (type, file) => {
scss(file);
if (path.basename(file).startsWith("_")) {
find.file(/^[^\_]*\.scss$/, path.join(__dirname, a), function(files) {
files.forEach(function(file) {
setTimeout(function() {
scss("./" + path.relative(path.join(__dirname), file));
}, 50);
});
});
}
});
var id;
gulp.watch(a + "/**/*.*").on("error", () => {}).on("change", function() {
clearTimeout(id);
id = setTimeout(function() {
browserSync.reload();
}, 1000);
});
});
});
gulp.task("api", function() {
if (fs.existsSync("./api/app.js")) {
$.express.run(["app.js"], {
cwd: "./api/"
}, false);
}
});
gulp.task("default", ["api", "browserSync", "watching"]);
gulp.task("noserver", ["watching"]);