-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
212 lines (194 loc) · 5.15 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
209
210
211
212
var $ = require("gulp-load-plugins")();
var argv = require("yargs").argv;
var gulp = require("gulp");
var newer = require("gulp-newer");
var rimraf = require("rimraf");
var panini = require("panini");
var sequence = require("run-sequence");
var sassLint = require("gulp-sass-lint");
// Check for --production flag
var isProduction = !!argv.production;
// File paths to various assets are defined here.
var paths = {
assets : ["src/assets/**/*", "!src/assets/{img,js,scss}/**/*"],
downloads : ["src/downloads/**/*.*"],
sass : ["node_modules/foundation-sites/scss"],
javascript : [
"node_modules/jquery/dist/jquery.js",
"node_modules/foundation-sites/dist/js/foundation.js",
"node_modules/what-input/what-input.js",
"node_modules/typed.js/lib/typed.js",
"node_modules/waypoints/lib/jquery.waypoints.js",
"node_modules/chart.js/dist/Chart.js",
"src/assets/js/**/*.js",
"node_modules/lodash/lodash.js",
"src/assets/js/app.js"
]
};
// Delete the "dist" folder
// This happens every time a build starts
gulp.task("clean", function(done) {
rimraf("./dist", done);
});
// Copy files out of the assets folder
// This task skips over the "img", "js", and "scss" folders, which are parsed separately
gulp.task("copy", function() {
return gulp.src(paths.assets).pipe(gulp.dest("./dist/assets"));
});
gulp.task("downloads", function() {
return gulp.src("src/downloads/**/*.*").pipe(gulp.dest("./dist/downloads"));
});
// Resets Panini so that we can assemble using different layouts for the iframes and building block pages
function getNewPanini(options) {
var p = new panini.Panini(options);
p.loadBuiltinHelpers();
p.refresh();
return p.render();
}
// Copy page templates into finished HTML files
gulp.task("pages", function() {
return gulp
.src("./src/pages/**/*.html")
.pipe(
newer({
dest: "./dist",
ext: ".html"
})
)
.pipe(
getNewPanini({
root: "./src/pages/",
layouts: "./src/layouts/",
partials: "./src/partials/",
data: "./src/data/"
})
)
.pipe($.cacheBust({ type: "MD5" }))
.pipe(gulp.dest("./dist"));
});
gulp.task("pages:reset", function(done) {
panini.refresh();
done();
});
// Lint the Sass
gulp.task("lint", function() {
return gulp
.src("src/assets/scss/**/*.s+(a|c)ss")
.pipe(sassLint())
.pipe(sassLint.format())
.pipe(sassLint.failOnError());
});
// Compile Sass into CSS
// In production, the CSS is compressed
gulp.task("sass", function() {
var uncss = $.if(
isProduction,
$.uncss({
html: ["src/**/*.html"],
ignore: [new RegExp("^meta..*"), new RegExp("^.is-.*")]
})
);
return (
gulp
.src("./src/assets/scss/app.scss")
.pipe(
$.sass({
includePaths: paths.sass,
outputStyle: isProduction ? "compressed" : "nested"
}).on("error", $.sass.logError)
)
.pipe(
$.autoprefixer({
browsers: ["last 2 versions", "ie >= 9"]
})
)
// .pipe(uncss)
.pipe(gulp.dest("./dist/assets/css"))
);
});
// Combine JavaScript into one file
// In production, the file is minified
gulp.task("javascript", function() {
var uglify = $.if(
isProduction,
$.uglify({
mangle: false
}).on("error", function(e) {
console.log(e);
})
);
return gulp
.src(paths.javascript)
.pipe($.concat("app.js"))
.pipe(uglify)
.pipe(gulp.dest("./dist/assets/js"));
});
// Compiles HTML templates into JST
gulp.task("jst", function() {
return gulp
.src("src/templates/*.html")
.pipe(
$.jstConcat("templates.js", {
renameKeys: ["^.*(src\/.*.html)$", "$1"]
})
)
.pipe(gulp.dest("dist/assets/js"));
});
// Copy images to the "dist" folder
// In production, the images are compressed
gulp.task("images", function() {
var imagemin = $.if(
isProduction,
$.imagemin({
progressive: true
})
);
return (
gulp
.src("./src/assets/img/**/*")
// .pipe(imagemin)
.pipe(gulp.dest("./dist/assets/img"))
);
});
// Build the "dist" folder by running all of the above tasks
gulp.task("build", function(done) {
sequence(
"clean",
[
"copy",
"pages",
"lint",
"sass",
"javascript",
"images",
"jst",
"downloads"
],
done
);
});
// Start a server with LiveReload to preview the site in
gulp.task("server", ["build"], function() {
return gulp.src("./dist").pipe(
$.webserver({
host: "localhost",
port: 8000,
livereload: true,
open: true
})
);
});
// Build the site, run the server, and watch for file changes
gulp.task("default", ["build", "server"], function() {
gulp.watch(paths.assets, ["copy"]);
gulp.watch(["./src/pages/**/*.html"], ["pages"]);
gulp.watch(["./src/{layouts,partials}/**/*.html"], ["pages:reset", "pages"]);
gulp.watch(["./src/assets/scss/**/*.scss"], ["sass"]);
gulp.watch(["./src/assets/js/**/*.js"], ["javascript"]);
gulp.watch(
["node_modules/foundation-sites/dist/foundation.js"],
["javascript"]
);
gulp.watch(["./src/assets/img/**/*"], ["images"]);
gulp.watch(["./src/templates/**/*"], ["jst"]);
});