-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
172 lines (152 loc) · 4.8 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
require("dotenv").config();
const gulp = require("gulp");
const gulpPlumber = require("gulp-plumber");
const gulpAutoprefixer = require("gulp-autoprefixer");
const gulpSass = require("gulp-sass");
const gulpRename = require("gulp-rename");
const gulpUglify = require("gulp-uglify-es").default;
const gulpSourcemaps = require("gulp-sourcemaps");
const gulpNotify = require("gulp-notify");
const gulpIf = require("gulp-if");
const browsersync = require("browser-sync");
const browserSync = browsersync.create();
const eventStream = require("event-stream");
const gulpOptions = require("./gulpfile.options");
const gulpBabel = require("gulp-babel");
const gulpConcat = require("gulp-concat");
const path = require("path");
const yargs = require("yargs");
const cliArgs = yargs.array("not").boolean("production");
/**
* Will be inserted as options for node-sass
* @link https://github.com/sass/node-sass#options
*/
const sassOptions = {
errLogToConsole: true,
precision: 8,
noCache: true
};
/**
* SASS Compiler, without watcher
*/
gulp.task("compile-sass", function() {
let { sourcemap, minify, src } = gulpOptions.sass;
const onSuccess = gulpNotify({
title: "SASS",
message: "All Compiled!",
onLast: true
});
// Check --production option from the cli
if (cliArgs && cliArgs.argv.production) {
minify = true;
sourcemap = false;
}
const _sassOptions = {
...sassOptions,
outputStyle: minify ? "compressed" : "nested"
};
// Check --not option from the cli
if (cliArgs && cliArgs.argv.not) {
src = src.filter(item => {
let extension = path.extname(item);
let name = path.basename(item, extension);
return !cliArgs.argv.not.includes(name);
});
}
return gulp
.src(src)
.pipe(gulpIf(minify, gulpRename({ suffix: ".min" })))
.pipe(gulpSourcemaps.init())
.pipe(gulpPlumber())
.pipe(gulpSass(_sassOptions))
.pipe(gulpAutoprefixer())
.pipe(gulpIf(sourcemap, gulpSourcemaps.write(".")))
.pipe(gulp.dest(gulpOptions.sass.distFolder))
.pipe(onSuccess);
});
/**
* SASS Watcher
* it will run compiler first and then watch for file changes
*/
gulp.task("watch-sass", function() {
gulp.watch(gulpOptions.sass.watch, gulp.series("compile-sass"));
});
/**
* JS Watcher
* it will run compiler first and then watch for file changes
*/
gulp.task("watch-js", function() {
const { watch } = gulpOptions.javascript;
gulp.watch(watch, gulp.series("compile-js"));
});
/**
* JS Compile, without watcher
*/
gulp.task("compile-js", function() {
let { list, minify, distFolder, sourcemap } = gulpOptions.javascript;
const onSuccess = gulpNotify({
title: "Javascript",
message: "All Compiled!",
onLast: true
});
// Check --production option from the cli
if (cliArgs && cliArgs.argv.production) {
minify = true;
sourcemap = false;
}
let stream = list.map(item => {
let name, src;
let isPolyfill = true;
let polyfill = "./node_modules/@babel/polyfill/dist/polyfill.min.js";
const emptyStream = gulp.src(".", { allowEmpty: true });
// Check whether a single file or an object
if (typeof item === "string") {
let extension = path.extname(item);
name = path.basename(item, extension);
src = [item];
} else {
let issetPolyfill = typeof item.polyfill !== "undefined";
name = item.name;
src = item.src;
if (issetPolyfill) isPolyfill = item.polyfill;
}
// Check --not option from the cli
if (cliArgs && cliArgs.argv.not.includes(name)) return emptyStream;
// Check polyfill option
if (isPolyfill) src = [polyfill, ...src];
return gulp
.src(src, { since: gulp.lastRun("watch-js"), allowEmpty: true })
.pipe(gulpSourcemaps.init())
.pipe(gulpConcat(name + ".js"))
.pipe(gulpIf(minify, gulpRename({ suffix: ".min" })))
.pipe(gulpBabel())
.pipe(gulpIf(minify, gulpUglify()))
.pipe(gulpIf(sourcemap, gulpSourcemaps.write(".")))
.pipe(gulp.dest(distFolder));
});
return eventStream.merge(stream).pipe(onSuccess);
});
/**
* Browsersync
* Reload browser on file changes
*/
gulp.task("browser-sync", function() {
browserSync.init(
gulpOptions.browserSync.watch,
gulpOptions.browserSync.config
);
gulp.watch(gulpOptions.browserSync.watch).on("change", function() {
browserSync.reload();
});
});
/**
* Default gulp command
*/
gulp.task(
"default",
gulp.parallel([
gulp.series("watch-sass"),
gulp.series("watch-js"),
gulp.series("browser-sync")
])
);