forked from DataDog/documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
289 lines (266 loc) · 8.5 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
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
// ## Globals
var argv = require('minimist')(process.argv.slice(2));
var autoprefixer = require('gulp-autoprefixer');
var browserSync = require('browser-sync').create();
var changed = require('gulp-changed');
var concat = require('gulp-concat');
var gulp = require('gulp');
var gulpif = require('gulp-if');
var lazypipe = require('lazypipe');
var merge = require('merge-stream');
var cssNano = require('gulp-cssnano');
var plumber = require('gulp-plumber');
var runSequence = require('run-sequence');
var sass = require('gulp-sass');
var uglify = require('gulp-uglify');
var jshint = require('gulp-jshint');
var manifest = require('./src/manifest.json');
var hash = require('gulp-hash');
var del = require('del');
var fs = require('fs');
var pathlib = require('path');
// asset manifest is a json object containing paths to dependencies
var path = manifest.paths;
// `project` - paths to first-party assets.
// - `project.js` - Array of first-party JS assets.
// - `project.css` - Array of first-party CSS assets.
var project = {
"fonts": ["src/fonts/**/*"],
"images": ["src/images/**/*"],
"js": [],
"css": [],
"globs": []
};
// build dependencies and globs
for (var fileName in manifest.dependencies) {
if (fileName.indexOf('.js') > -1) {
project["js"] = project["js"].concat(manifest.dependencies[fileName]["files"]);
}
if (fileName.indexOf('.css') > -1) {
project["css"] = project["css"].concat(manifest.dependencies[fileName]["files"]);
}
var partial_dirs = fs.readdirSync("./layouts/partials/").filter(function(file) {
return fs.statSync(pathlib.join("./layouts/partials/", file)).isDirectory();
});
// add partials
var partials = {"main-dd.css":[], "main-dd.js":[]};
for(var i=0; i < partial_dirs.length; i++) {
if (fileName.indexOf('.css') > -1) {
var pathToScss = pathlib.join("./layouts/partials/", partial_dirs[i], partial_dirs[i] + ".scss");
if (fs.existsSync(pathlib.resolve(pathToScss))) {
if (project["css"].indexOf(pathToScss) === -1) {
project["css"].push(pathToScss);
partials[fileName].push(pathToScss);
}
}
}
if (fileName.indexOf('.js') > -1) {
var pathToJs = pathlib.join("./layouts/partials/", partial_dirs[i], partial_dirs[i] + ".js");
if (fs.existsSync(pathlib.resolve(pathToJs))) {
if (project["js"].indexOf(pathToJs) === -1) {
project["js"].push(pathToJs);
partials[fileName].push(pathToJs);
}
}
}
}
var fileNameArray = fileName.split(".");
project.globs.push(
{
"type": fileNameArray[fileNameArray.length - 1],
"name": fileName,
"globs": manifest.dependencies[fileName]["vendor"].concat(manifest.dependencies[fileName]["files"], partials[fileName])
}
);
}
// CLI options
var enabled = {
// Enable static asset revisioning when `--production`
rev: false,
// Disable source maps when `--production`
maps: false,
// Fail styles task on error when `--production`
failStyleTask: true,
// Fail due to JSHint warnings only when `--production`
failJSHint: true,
// Strip debug statments from javascript when `--production`
stripJSDebug: false,
// hash static?
hashStatic: true,
// nano
nano: true,
// uglify
uglify: true
};
// Path to the compiled assets manifest in the dist directory
var revManifest = path.dist + 'assets.json';
// ## Reusable Pipelines
// See https://github.com/OverZealous/lazypipe
// ### CSS processing pipeline
// Example
// ```
// gulp.src(cssFiles)
// .pipe(cssTasks('main.css')
// .pipe(gulp.dest(path.dist + 'styles'))
// ```
var cssTasks = function (filename) {
del(["static/css/**/*"]);
return lazypipe()
.pipe(function () {
return gulpif(!enabled.failStyleTask, plumber());
})
.pipe(function () {
return gulpif('*.scss', sass({
outputStyle: 'nested', // libsass doesn't support expanded yet
precision: 10,
includePaths: ['.'],
errLogToConsole: !enabled.failStyleTask
}));
})
.pipe(concat, filename)
.pipe(autoprefixer, {
browsers: [
'last 2 versions',
'last 2 Safari versions',
'android 4',
'opera 12'
]
})
.pipe(function () {
return gulpif(enabled.nano, cssNano({
safe: true
}));
})
.pipe(function () {
return gulpif(enabled.hashStatic, hash())
})();
};
// ### JS processing pipeline
// Example
// ```
// gulp.src(jsFiles)
// .pipe(jsTasks('main.js')
// .pipe(gulp.dest(path.dist + 'scripts'))
// ```
var jsTasks = function (filename) {
del(["static/js/**/*"]);
return lazypipe()
.pipe(concat, filename)
.pipe(function () {
return gulpif(enabled.uglify, uglify({compress: {'drop_debugger': true}}));
})
.pipe(function () {
return gulpif(enabled.hashStatic, hash())
})();
};
// ### Write to rev manifest
// If there are any revved files then write them to the rev manifest.
// See https://github.com/sindresorhus/gulp-rev
var writeToManifest = function (directory) {
return lazypipe()
.pipe(gulp.dest, path.dist + directory)
.pipe(browserSync.stream, {match: '**/*.{js,css}'})();
};
// ## Gulp tasks
// Run `gulp -T` for a task summary
// ### Styles
// `gulp styles` - Compiles, combines, and optimizes project CSS.
// By default this task only logs a warning if a precompiler error is
// raised. If the `--production` flag is set: this task fails outright.
gulp.task('styles', function () {
var merged = merge();
for (var i in project["globs"]) {
var dep = project["globs"][i];
if (dep["type"] == 'css') {
var cssTasksInstance = cssTasks(dep.name);
if (!enabled.failStyleTask) {
cssTasksInstance.on('error', function (err) {
console.error(err.message);
this.emit('end');
});
}
merged.add(gulp.src(dep.globs, {base: 'scss'})
.pipe(cssTasksInstance));
}
}
return merged
.pipe(writeToManifest('css'))
.pipe(hash.manifest("css.json"))
.pipe(gulp.dest("data/manifests"));
});
// ### Scripts
// `gulp scripts` - Runs JSHint then compiles, combines, and optimizes project JS.
gulp.task('scripts', function () {
var merged = merge();
for (var i in project["globs"]) {
var dep = project["globs"][i];
if (dep["type"] == 'js') {
merged.add(
gulp.src(dep.globs, {base: 'js'})
.pipe(jsTasks(dep.name))
);
}
}
return merged
.pipe(writeToManifest('js'))
.pipe(hash.manifest("js.json"))
.pipe(gulp.dest("data/manifests"));
});
// ### JSHint
// `gulp jshint` - Lints configuration JSON and project JS.
gulp.task('jshint', function () {
return gulp.src([
'gulpfile.js'
].concat(project.js))
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'))
.pipe(gulpif(enabled.failJSHint, jshint.reporter('fail')));
});
// IMAGES
gulp.task("images", function () {
del(["static/images/**/*"]);
gulp.src("src/images/**/*")
.pipe(gulpif(enabled.hashStatic, hash()))
.pipe(gulp.dest("static/images"))
.pipe(hash.manifest("images.json"))
.pipe(gulp.dest("data/manifests"))
});
// ### Clean
// `gulp clean` - Deletes the build folder entirely.
gulp.task('clean', require('del').bind(null, [path.dist]));
// ### Watch
// `gulp watch` - Use BrowserSync to proxy your dev server and synchronize code
// changes across devices. Specify the hostname of your dev server at
// `manifest.config.devUrl`. When a modification is made to an asset, run the
// build step for that asset and inject the changes into the page.
// See: http://www.browsersync.io
gulp.task('watch', function () {
// browserSync.init({
// files: ['{lib,templates}/**/*.php', '*.php'],
// proxy: config.devUrl,
// snippetOptions: {
// whitelist: ['/wp-admin/admin-ajax.php'],
// blacklist: ['/wp-admin/**']
// }
// });
gulp.watch([path.source + 'scss/**/*'], ['styles']);
gulp.watch([path.source + 'js/**/*'], ['scripts']);
gulp.watch([path.source + 'images/**/*'], ['images']);
gulp.watch([path.html + '**/*.js'], ['scripts']);
gulp.watch([path.html + '**/*.scss'], ['styles']);
});
// ### Build
// `gulp build` - Run all the build tasks but don't clean up beforehand.
// Generally you should be running `gulp` instead of `gulp build`.
gulp.task('build', function (callback) {
runSequence(
'styles',
'scripts',
'images',
callback);
});
// ### Gulp
// `gulp` - Run a complete build. To compile for production run `gulp --production`.
gulp.task('default', function () {
gulp.start('build');
});