-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
executable file
·336 lines (298 loc) · 8.78 KB
/
gulpfile.babel.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Import modules
import gulp from "gulp";
import gutil from "gulp-util";
import sass from "gulp-ruby-sass";
import notifier from "node-notifier";
import sourcemaps from "gulp-sourcemaps";
import browserify from "browserify";
import livereload from "gulp-livereload";
import babelify from "babelify";
import uglify from "gulp-uglify";
import yargs from "yargs";
import preprocess from "gulp-preprocess";
import runSequence from "run-sequence";
import sprity from "sprity";
import gulpif from "gulp-if";
import source from "vinyl-source-stream";
import watchify from "watchify";
import assign from "lodash.assign";
import esteWatch from "este-watch";
import rename from "gulp-rename";
import imagemin from "gulp-imagemin";
import pngquant from "imagemin-pngquant";
import cache from 'gulp-cache';
import browserSync from "browser-sync";
import streamify from "gulp-streamify";
import _ from "lodash";
import loose_envify from "loose-envify"
import config from "./config";
const reload = browserSync.reload;
// Define global
let basePath = __dirname;
let environment = null;
let args = yargs.argv;
let settings = {};
let prepareFiles = () => {
const min = environment === "build" ? ".min" : "";
const path = "www/build";
const sassFiles = [
{ path: `${basePath}/styles/app.scss`, name: "styles" }
];
let jsFiles = [
{ path: `${basePath}/scripts/app.js`, name: "scripts" }
];
if(args.react) {
jsFiles.push({ path: `react/src/index.js`, name: "react" });
}
const sassFilesSetting = _.map(sassFiles, (file) => {
return {
entries: file.path,
dest: path,
bundle: `${file.name}${min}.css`,
options: {
quiet: true,
sourcemap: !args.build,
style: args.build ? "compressed" : "nested",
loadPath: require("node-neat").includePaths,
require: "sass-globbing"
}
}
});
const jsFilesSetting = _.map(jsFiles, (file) => {
return {
entries: file.path,
dest: path,
bundle: `${file.name}${min}.js`
};
});
settings = {
sass: {
files: sassFilesSetting
},
javascript: {
files: jsFilesSetting
}
};
};
/**
* @name Sass task
*/
gulp.task("sass", () => {
let sassFile = (file) => {
return sass(file.entries, file.options)
.on("error", function (err) {
notifier.notify({ "title": "Sass", "message": err.message });
console.error("Error!", err.message);
})
.pipe(sourcemaps.write())
.pipe(rename(file.bundle))
.pipe(gulp.dest(file.dest))
.pipe(reload({ stream: true }))
.pipe(livereload());
};
settings.sass.files.forEach(sassFile);
});
gulp.task("browser-sync", () => {
browserSync.init({
proxy: config.proxy
});
});
/**
* @name watchify task
*/
gulp.task("watchify", () => {
let watchifyFile = (file) => {
let opts = assign({}, watchify.args, { debug: true });
let bundler = watchify(browserify(file.entries, opts));
let rebundle = () => {
return bundler
.bundle()
.on("error", function handleError(err) {
notifier.notify({ "title": "Javascript", "message": err.toString() });
gutil.log(gutil.colors.red(err.toString()));
this.emit("end");
})
.pipe(source(file.bundle))
.pipe(gulp.dest(file.dest))
.pipe(reload({ stream: true }))
.pipe(livereload());
};
let logger = (msg) => {
gutil.log("Watchify:", gutil.colors.green(msg));
};
bundler
.transform(babelify, { presets: ["es2015", "react", "stage-0"] })
.on("update", rebundle)
.on("log", logger);
return rebundle();
};
settings.javascript.files.forEach(watchifyFile);
});
/**
* @name Browserify task
*/
gulp.task("browserify", () => {
let ugl = environment === "build" ? true : false;
let browserifyFile = (file) => {
return browserify({ debug: false })
.transform(loose_envify,{
NODE_ENV: "production"
})
.transform(babelify, { presets: ["es2015", "react", "stage-0"] })
.require(file.entries, { entry: true })
.bundle()
.on("error", function handleError(err) {
notifier.notify({ "title": "Javascript", "message": err.toString() });
console.error(err.toString());
this.emit("end");
})
.pipe(source(file.bundle))
.pipe(gulpif(ugl, streamify(uglify())))
.pipe(gulp.dest(file.dest));
};
settings.javascript.files.forEach(browserifyFile);
});
/**
* @name Preprocess task
*/
gulp.task("preprocess", () => {
return gulp.src(`${basePath}/htmls/**/*.html`)
.pipe(preprocess({ context: { NODE_ENV: environment } }))
.pipe(gulp.dest(basePath))
.pipe(reload({ stream: true }))
.pipe(livereload());
});
/**
* @name Sprity task
*/
gulp.task("sprity", () => {
return sprity.src({
src: `${basePath}/assets/images/sprites/*.{png,jpg}`,
processor: "sass",
style: "sprites.scss",
template: `png.hbs`,
dimension: [{ ratio: 1, dpi: 192 }, { ratio: 2, dpi: 192 }],
"lwip-interpolation": "cubic"
})
.on("error", (err) => {
console.log("No sprites images in sprites folder!");
})
.pipe(gulpif("*.png", gulp.dest(`${basePath}/assets/images`), gulp.dest(`${basePath}/styles/base`)))
.pipe(livereload());
});
/**
* @name Watch task
*/
gulp.task("watch", () => {
let watchDirs = [`${basePath}/styles/`, `${basePath}/scripts/`];
if (args.html) {
watchDirs.push(`${basePath}/htmls/`);
}
let watch = esteWatch(watchDirs, (e) => {
switch (e.extension) {
case "scss":
gulp.start("sass");
break;
case "html":
gulp.start("preprocess");
break;
case "png":
gulp.start("sprity");
break;
}
});
watch.start();
});
/**
* @name Uglify Task
*/
gulp.task("uglify", function () {
let uglifyFile = (file) => {
console.log(file);
return gulp.src(file.entries)
.pipe(uglify({ compress: { drop_console: true } }))
.pipe(gulp.dest(file.dest));
}
settings.javascript.uglify.forEach(uglifyFile);
});
/**
* @name Imagemin Task
*/
gulp.task("imagemin", () => {
return gulp.src(`${basePath}/assets/images/*`)
.pipe(cache(imagemin({ progressive: true, use: [pngquant()] })))
.pipe(gulp.dest(`${basePath}/assets/images`));
});
/**
* @name Copy Task
*/
gulp.task("copy", function () {
gulp.src(`${basePath}/assets/**/*`).pipe(gulp.dest(`${basePath}/assets`));
gulp.src(`${basePath}/styles/styles.min.css`).pipe(gulp.dest(`${basePath}/styles`));
gulp.src(`${basePath}/scripts/scripts.min.js`).pipe(gulp.dest(`${basePath}/scripts`));
gulp.src(`${basePath}/scripts/vendors/*`).pipe(gulp.dest(`${basePath}/scripts/vendors`));
gulp.src(`${basePath}/*.html`).pipe(gulp.dest(`${basePath}/dist/`));
});
/**
* @name Server task
*/
gulp.task("server", ["sass", "watchify", "preprocess", "sprity", "browser-sync"], () => {
livereload.listen();
gulp.start("watch");
});
/**
* @name No server task
*/
gulp.task("static", ["sass", "watchify", "sprity", "browser-sync"], () => {
livereload.listen();
gulp.start("watch");
});
/**
* @name Progress task
*/
gulp.task("progress", () => {
if (args.html) {
gulp.start("server");
} else {
gulp.start("static");
}
});
/**
* @name Develop task
*/
gulp.task("develop", () => {
gulp.start("build");
});
/**
* @name Build task
*/
gulp.task("build", () => {
process.env.NODE_ENV = "production";
if (args.dist) {
runSequence("sass", "browserify", "preprocess", "sprity", "imagemin", "copy");
} else {
runSequence("sass", "browserify", "preprocess", "sprity", "imagemin");
}
});
/**
* @name Default task
*/
gulp.task("default", () => {
if (args.production) {
environment = "build";
}
else if (args.stage) {
environment = "develop";
}
else {
environment = "progress";
}
if (typeof args.path === "string") {
basePath = `${basePath}/${args.path}`;
}
else {
basePath = `${basePath}/www`;
}
prepareFiles();
gulp.start(environment);
});