This repository has been archived by the owner on Sep 23, 2024. It is now read-only.
forked from geerlingguy/drupal-vm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
181 lines (158 loc) · 5.48 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
// This gulp support is experimental, and any given web project may have
// it's own gulp setup that should be used before using this setup.
// The gulp setup here may not even work for some web projects.
// To configure, copy `example.gulp-config.yml` to `example.gulp-config.yml`, and edit
// that file as required.
// Include gulp.
var gulp = require('gulp');
var browsersync = require('browser-sync').create();
// Include plugins.
var autoprefix = require('gulp-autoprefixer');
var critical = require('critical');
var cssNano = require('gulp-cssnano');
var glob = require('gulp-sass-glob');
var gutil = require('gulp-util');
var imagemin = require('gulp-imagemin');
var jshint = require('gulp-jshint');
var notify = require('gulp-notify');
var path = require('path');
var plumber = require('gulp-plumber');
var pngcrush = require('imagemin-pngcrush');
var rimraf = require('rimraf');
var rp = require('request-promise');
var sass = require('gulp-sass');
var scssLint = require('gulp-scss-lint');
var shell = require('gulp-shell');
var sourcemaps = require('gulp-sourcemaps');
var touch = require('gulp-touch-fd');
var uglify = require('gulp-uglify');
var urljoin = require('url-join');
// Load config
var YAML = require('yamljs');
var config = YAML.load('gulp-config.yml');
// CSS.
gulp.task('css', function() {
return gulp.src(config.themeDir + '/' + config.css.src, { sourcemaps: true })
.pipe(glob())
.pipe(plumber({
errorHandler: function (error) {
notify.onError({
title: "Gulp",
subtitle: "Failure!",
message: "Error: <%= error.message %>",
sound: "Beep"
}) (error);
this.emit('end');
}}))
.pipe(sourcemaps.init())
.pipe(sass({
outputStyle: config.css.outputStyle,
errLogToConsole: true,
includePaths: config.css.includePaths
}))
.pipe(autoprefix('last 2 versions', '> 1%', 'ie 9', 'ie 10'))
.pipe(cssNano())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(config.themeDir + '/' + config.css.dest))
// Set the file modification time.
// @see https://github.com/gulpjs/gulp/issues/1461
.pipe(touch())
.pipe(browsersync.stream({match: '**/*.css'}));
});
// Compress images.
gulp.task('images', function () {
return gulp.src(config.themeDir + '/' + config.images.src)
.pipe(imagemin({
progressive: true,
svgoPlugins: [{ removeViewBox: false }],
use: [pngcrush()]
}))
.pipe(gulp.dest(config.themeDir + '/' + config.images.dest));
});
// Fonts.
gulp.task('fonts', function() {
return gulp.src(config.fonts.src)
.pipe(gulp.dest(config.themeDir + '/' + config.fonts.dest));
});
// Watch task.
gulp.task('watch', function(done) {
gulp.watch(config.themeDir + '/' + config.css.src, gulp.series('css'));
gulp.watch(config.themeDir + '/' + config.images.src, gulp.series('images'));
done();
});
// Static Server + Watch
gulp.task('serve', gulp.series(gulp.parallel('css', 'fonts'), 'watch', function() {
browsersync.init({
proxy: config.browsersync.proxy,
browser: config.browsersync.browser,
files: [
config.themeDir + '/' + '**/*.{php,inc,module,theme,twig,yml}'
]
});
}));
// Run drush to clear the theme registry.
gulp.task('drush', shell.task([
'drush ' + config.drushAlias + ' cache-clear theme-registry'
]));
// SCSS Linting.
gulp.task('scss-lint', function() {
return gulp.src([config.themeDir + '/' + config.css.src])
.pipe(scssLint())
.pipe(scssLint.format())
.pipe(scssLint.failOnError());
});
// JS Linting.
gulp.task('js-lint', function() {
return gulp.src(config.themeDir + '/' + config.js.src)
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
// Default Task
gulp.task('default', gulp.series('serve'));
// Tasks to generate critical CSS.
// @see https://www.smashingmagazine.com/2015/08/understanding-critical-css/
// @see https://github.com/asilgag/critical-css-gulp-example/blob/master/gulp_tasks/critical.js
// Allows invalid HTTPS certificates
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
var criticalGenerate = function (done) {
'use strict';
Object.keys(config.critical.urls).map(function (url, index) {
var pageUrl = urljoin(config.critical.baseDomain, url);
var destCssPath = path.join(
config.critical.dest,
config.critical.urls[url] + '.css'
);
return rp({uri: pageUrl, strictSSL: false})
.then(function (body) {
var htmlString = body
.replace(/href="\//g, 'href="' + urljoin(config.critical.baseDomain, '/'))
.replace(/src="\//g, 'src="' + urljoin(config.critical.baseDomain, '/'));
gutil.log('Generating critical css', gutil.colors.magenta(destCssPath), 'from', pageUrl);
critical.generate({
base: config.themeDir,
html: htmlString,
src: '',
dest: destCssPath,
minify: false,
width: config.critical.width,
height: config.critical.height,
penthouse: config.critical.penthouse
});
if (index + 1 === Object.keys(config.critical.urls).length) {
return done();
}
})
.catch(function(err) {
console.log(err)
});
});
};
gulp.task('critical:clean', function (done) {
'use strict';
var criticalDest = path.join(config.themeDir, config.critical.dest);
return rimraf(criticalDest, function () {
gutil.log('Critical directory', gutil.colors.magenta(criticalDest), 'deleted');
return done();
});
});
gulp.task('critical', gulp.series('critical:clean', criticalGenerate));