-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
208 lines (185 loc) · 5.39 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
// - - - - - DEPENDENCIES - - - - - //
const gulp = require("gulp");
const { series, parallel } = require("gulp");
const gutil = require("gulp-util");
const notify = require("gulp-notify");
const del = require("del");
const sourcemaps = require("gulp-sourcemaps");
const source = require("vinyl-source-stream");
const buffer = require("vinyl-buffer");
const globby = require("globby");
const rename = require("gulp-rename");
const es = require("event-stream");
const jade = require("gulp-jade");
const sass = require("gulp-dart-sass");
const cleanCSS = require("gulp-clean-css");
const postcss = require('gulp-postcss');
const uncss = require('postcss-uncss');
const jshint = require("gulp-jshint");
const browserify = require("browserify");
const uglify = require("gulp-uglify");
const browserSync = require("browser-sync").create();
// - - - - - VARIABLES - - - - - //
const reload = browserSync.reload;
const SRC = "./src";
const DIST = "./dist";
const BUILD = "./build"; //maybe rename this as test?
// - - - - - HELPERS - - - - - //
//source: https://gist.github.com/mlouro/8886076
const handleError = function(task) {
return function(err) {
notify.onError({
message: task + " failed, check the logs.."
})(err);
gutil.log(gutil.colors.bgRed(task + " error:"), gutil.colors.red(err));
};
};
// - - - - - TASKS - - - - - //
function clean(callback) {
del([DIST + "/**", BUILD + "/**"]).then(function() {
callback();
});
}
function scripts(done) {
const entries = await getScriptEntries();
entries.forEach((entry) => {
return doBrowserify(entry);
})
// TODO: ensure all entries have been processed before continuing
done();
};
async function getScriptEntries() {
return await globby(["./src/js/main_bufi.js", "./tests/js/main_**.js"])
.then(function(entries) {
return entries.map(function(entry) {
var entryAsString = String(entry);
var filename = entryAsString.substring(
entryAsString.indexOf("main_") + 5,
entryAsString.length - 3
);
return { entry, filename };
});
})
.catch(function(err) {
handleError("getScriptEntries");
});
}
function doUglify() {
return gulp
.src(BUILD + "/js/bufi.js")
.pipe(uglify({ output: { comments: /^!|@preserve|@license|@cc_on/i } }))
.pipe(
rename(function(path) {
path.basename += ".min";
})
)
.pipe(gulp.dest(DIST));
}
function doBrowserify({entry, filename}) {
return (
browserify({
entries: entry,
debug: true,
// for standalone to work you need to feed it
// the desired (unique!) global identifier
standalone: filename
})
.bundle()
.on("error", handleError("browserify"))
.pipe(source(entry))
.pipe(
rename(function(path) {
// TODO: replacing pathname like this seems crude - particularly
// since it doesn't use the SRC variable
path.dirname = path.dirname.replace("src/", "");
path.dirname = path.dirname.replace("tests/", "");
path.basename = path.basename.replace("main_", "");
})
)
.pipe(buffer())
// Add gulp plugins to the pipeline here.
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write("./"))
.pipe(gulp.dest(BUILD))
.pipe(browserSync.stream())
);
}
function templates() {
//TODO: avoid processing pages that haven't changed
return (
gulp
.src(["./src/**/*.jade", "./tests/*.jade", "!./_templates/**"])
.pipe(
jade({
pretty: true,
basedir: "./tests/_templates"
})
)
//TODO: fix this: reports errors, but doesn't allow watch task to continue
.on("error", handleError("jade"))
.pipe(gulp.dest(BUILD))
);
}
function doSass() {
return gulp
.src([SRC + "/**/*.scss", "./tests/**/*.scss"])
.pipe(sourcemaps.init())
.pipe(
sass({
// outputStyle: 'compressed',
includePaths: ["node_modules"]
}).on("error", sass.logError)
)
.pipe(sourcemaps.write())
.pipe(gulp.dest(BUILD))
.pipe(browserSync.stream());
}
function minifyCSS() {
return gulp
.src(BUILD + "/bufi_m.css")
.pipe(cleanCSS({ compatibility: "ie11" }))
.pipe(
rename(function(path) {
path.basename += ".min";
})
)
.pipe(gulp.dest(DIST));
}
function purgeCSS() {
const plugins = [
uncss({
html: [BUILD + '/static.html']
}),
];
return gulp.src(BUILD + "/css/bufi_m.css")
.pipe(postcss(plugins))
.pipe(gulp.dest(BUILD));
}
function serve() {
browserSync.init({
server: {
baseDir: BUILD,
directory: true // displays navigable directory structure
},
ghostmode: {
clicks: true,
forms: true,
scroll: true
}
});
gulp.watch(SRC + "/**/*.scss", doSass);
gulp.watch(SRC + "/**/*.js", scripts);
gulp.watch("./tests/*.jade", series(templates, reload));
gulp.watch("./tests/**/*.scss", doSass);
gulp.watch("./tests/**/*.js", scripts);
}
// working :)
gulp.task('clean', clean);
gulp.task('css', doSass);
gulp.task('minCSS', minifyCSS);
gulp.task('purgeCSS', purgeCSS);
gulp.task('jade', templates);
gulp.task('scripts', scripts);
// NOTE: not quite there yet: need to run scripts manually before this.
gulp.task('build', series(scripts, templates, doSass, doUglify, purgeCSS, minifyCSS));
gulp.task("default", series(clean, series(parallel(templates, doSass, scripts), serve)));