-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
206 lines (174 loc) · 5.56 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
/* global Buffer */
const fs = require('fs');
const gulp = require('gulp');
const merge = require('merge');
const sass = require('gulp-sass');
const rename = require('gulp-rename');
const sassLint = require('gulp-sass-lint');
const autoprefixer = require('gulp-autoprefixer');
const cleanCSS = require('gulp-clean-css');
const readme = require('gulp-readme-to-markdown');
const tap = require('gulp-tap');
const css = require('css');
const browserSync = require('browser-sync').create();
let config = {
src: {
scssPath: './src/scss'
},
dist: {
cssPath: './static/css'
},
packagesPath: './node_modules',
sync: false,
syncTarget: 'http://localhost/wordpress/'
};
/* eslint-disable no-sync */
if (fs.existsSync('./gulp-config.json')) {
const overrides = JSON.parse(fs.readFileSync('./gulp-config.json'));
config = merge(config, overrides);
}
/* eslint-enable no-sync */
//
// Helper functions
//
// BrowserSync reload function
function serverReload(done) {
if (config.sync) {
browserSync.reload();
}
done();
}
// BrowserSync serve function
function serverServe(done) {
if (config.sync) {
browserSync.init({
proxy: {
target: config.syncTarget
}
});
}
done();
}
//
// CSS
//
// Lint all SCSS files
gulp.task('scss-lint', () => {
return gulp.src(`${config.src.scssPath}/*.scss`)
.pipe(sassLint())
.pipe(sassLint.failOnError());
});
// Compile primary SCSS files
gulp.task('css-main', () => {
return gulp.src(`${config.src.scssPath}/athena-gf.scss`)
.pipe(sass({
includePaths: [config.src.scssPath, config.packagesPath]
})
.on('error', sass.logError))
.pipe(tap((file) => {
return filterAthenaCSS(file);
}))
.pipe(cleanCSS())
.pipe(autoprefixer({
// Supported browsers added in package.json ("browserslist")
cascade: false
}))
.pipe(rename({
extname: '.min.css'
}))
.pipe(gulp.dest(config.dist.cssPath));
});
// Removes Athena-specific styles. Leaves only selectors and media queries
// with selectors that begin with '.gform_wrapper' or 'ui-datepicker'.
//
// NOTE: This function will strip out any at-rules besides @media--if custom
// @import, @keyframe, etc rules ever need to be added to athena-gf.min.css,
// this function will need to be updated!
function filterAthenaCSS(file) {
const cssObj = css.parse(file.contents.toString());
if (cssObj) {
const rules = cssObj.stylesheet.rules;
const filteredRules = [];
// Loop through every rule. Store valid rules in filteredRules.
for (let i = 0; i < rules.length; i++) {
const rule = rules[i];
let filteredSelectors = [];
if (rule.type === 'rule') {
// Check each selector in the rule for selectors we want to keep.
// If a selector in the rule's selector list matches, add it to
// filteredSelectors.
filteredSelectors = getFilteredSelectors(rule, filteredSelectors);
// Check the rule's filteredSelectors array; if it's not empty, add
// the rule to filteredRules.
if (filteredSelectors.length) {
rule.selectors = filteredSelectors;
filteredRules.push(rule);
}
} else if (rule.type === 'media') {
const filteredSubnodes = [];
for (let k = 0; k < rule.rules.length; k++) {
const subnode = rule.rules[k];
let filteredSubnodeSelectors = [];
if (subnode.type === 'rule') {
// Check each selector in the rule for selectors we want to keep.
// If a selector in the rule's selector list matches, add it to
// filteredSubnodeSelectors.
filteredSubnodeSelectors = getFilteredSelectors(subnode, filteredSubnodeSelectors);
// Check the rule's filteredSubnodeSelectors array; if it's not empty, add
// the rule to filteredSubnodes.
if (filteredSubnodeSelectors.length) {
subnode.selectors = filteredSubnodeSelectors;
filteredSubnodes.push(subnode);
}
}
}
if (filteredSubnodes.length) {
rule.rules = filteredSubnodes;
filteredRules.push(rule);
}
}
}
// Finally, replace cssObj's old ruleset with our filtered one:
cssObj.stylesheet.rules = filteredRules;
// Return a buffer for gulp to continue with
file.contents = Buffer.from(css.stringify(cssObj));
} else {
console.log('Couldn\'t parse CSS--skipping'); // eslint-disable-line no-console
}
}
// Returns an array of filtered selectors present in a given node.
function getFilteredSelectors(node, filteredSelectors) {
for (let i = 0; i < node.selectors.length; i++) {
const selector = node.selectors[i];
if (selector.startsWith('.gform_wrapper') || selector.startsWith('.ui-datepicker')) {
filteredSelectors.push(selector);
}
}
return filteredSelectors;
}
// All css-related tasks
gulp.task('css', gulp.series('scss-lint', 'css-main'));
//
// Documentation
//
// Create a Github-flavored markdown file from the plugin readme.txt
gulp.task('readme', () => {
return gulp.src(['readme.txt'])
.pipe(readme({
details: false,
screenshot_ext: [] // eslint-disable-line camelcase
}))
.pipe(gulp.dest('.'));
});
//
// Rerun tasks when files change
//
gulp.task('watch', (done) => {
serverServe(done);
gulp.watch(`${config.src.scssPath}/**/*.scss`, gulp.series('css', serverReload));
gulp.watch('./**/*.php', gulp.series(serverReload));
});
//
// Default task
//
gulp.task('default', gulp.series('css', 'readme'));