-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
executable file
·315 lines (276 loc) · 7.77 KB
/
gulpfile.babel.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* eslint-env es6 */
'use strict';
/**
* To start theme building process, define the theme name below,
* then run "gulp" in command line.
*/
import gulp from 'gulp';
import autoprefixer from 'autoprefixer';
import browserSync from 'browser-sync';
import cssnano from 'gulp-cssnano';
import babel from 'gulp-babel';
import eslint from 'gulp-eslint';
import log from 'fancy-log';
import gulpif from 'gulp-if';
import image from 'gulp-image';
import newer from 'gulp-newer';
import partialImport from 'postcss-partial-import';
import postcssPresetEnv from 'postcss-preset-env';
import phpcs from 'gulp-phpcs';
import postcss from 'gulp-postcss';
import print from 'gulp-print';
import replace from 'gulp-string-replace';
import requireUncached from 'require-uncached';
import sass from 'gulp-sass';
import sourcemaps from 'gulp-sourcemaps';
import sort from 'gulp-sort';
import tabify from 'gulp-tabify';
import uglify from 'gulp-uglify';
import wppot from 'gulp-wp-pot';
import zip from 'gulp-zip';
// Import theme-specific configurations.
var config = require('./dev/config/themeConfig.js');
var themeConfig = config.theme;
themeConfig.isFirstRun = true;
// Project paths
const paths = {
config: {
cssVars: './dev/config/cssVariables.json',
themeConfig: './dev/config/themeConfig.js'
},
php: {
src: ['dev/**/*.php', '!dev/optional/**/*.*'],
dest: './'
},
styles: {
src: ['dev/**/*.css', '!dev/optional/**/*.*'],
dest: './',
sass: ['dev/**/*.scss']
},
scripts: {
src: ['dev/**/*.js', '!dev/**/*.min.js', '!dev/js/libs/**/*.js', '!dev/optional/**/*.*', '!dev/config/**/*'],
min: 'dev/**/*.min.js',
dest: './',
libs: 'dev/js/libs/**/*.js',
libsDest: './js/libs/',
verboseLibsDest: './verbose/js/libs/'
},
images: {
src: ['dev/**/*.{jpg,JPG,png,svg,gif,GIF}', '!dev/optional/**/*.*'],
dest: './'
},
languages: {
src: ['./**/*.php', '!dev/**/*.php', '!verbose/**/*.php'],
dest: './languages/' + config.theme.slug + '.pot'
},
verbose: './verbose/',
export: {
src: ['**/*', '!' + config.theme.slug, '!' + config.theme.slug + '/**/*', '!dev/**/*', '!node_modules', '!node_modules/**/*', '!vendor', '!vendor/**/*', '!.*', '!composer.*', '!gulpfile.*', '!package*.*', '!phpcs.*', '!*.zip'],
dest: './'
}
};
/**
* Conditionally set up BrowserSync.
* Only run BrowserSync if config.browserSync.live = true.
*/
// Create a BrowserSync instance:
const server = browserSync.create();
// Initialize the BrowserSync server conditionally:
function serve(done) {
if (config.dev.browserSync.live) {
server.init({
proxy: config.dev.browserSync.proxyURL,
port: config.dev.browserSync.bypassPort,
liveReload: true
});
}
done();
}
// Reload the live site:
function reload(done) {
config = requireUncached('./dev/config/themeConfig.js');
if (config.dev.browserSync.live) {
if (server.paused) {
server.resume();
}
server.reload();
} else {
server.pause();
}
done();
}
/**
* PHP via PHP Code Sniffer.
*/
export function php() {
config = requireUncached('./dev/config/themeConfig.js');
// Check if theme slug has been updated.
let isRebuild = themeConfig.isFirstRun ||
( themeConfig.slug !== config.theme.slug ) ||
( themeConfig.name !== config.theme.name );
if ( isRebuild ) {
themeConfig.slug = config.theme.slug;
themeConfig.name = config.theme.name;
}
// Reset first run.
if ( themeConfig.isFirstRun ) {
themeConfig.isFirstRun = false;
}
return gulp.src(paths.php.src)
// If not a rebuild, then run tasks on changed files only.
.pipe(gulpif(!isRebuild, newer(paths.php.dest)))
.pipe(phpcs({
standard: 'WordPress',
warningSeverity: 0
}))
// Log all problems that were found.
.pipe(phpcs.reporter('log'))
.pipe(replace('wprig', config.theme.slug))
.pipe(replace('WP Rig', config.theme.name))
.pipe(gulp.dest(paths.php.dest));
}
/**
* Sass, if that's being used.
*/
export function sassStyles() {
return gulp.src(paths.styles.sass, { base: "./" })
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(tabify(2, true))
.pipe(sourcemaps.write('./maps'))
.pipe(gulp.dest('.'));
}
/**
* CSS via PostCSS + CSSNext (includes Autoprefixer by default).
*/
export function styles() {
config = requireUncached('./dev/config/themeConfig.js');
// Reload cssVars every time the task runs.
let cssVars = requireUncached(paths.config.cssVars)
return gulp.src(paths.styles.src)
.pipe(print())
.pipe(phpcs({
standard: 'WordPress',
warningSeverity: 0
}))
// Log all problems that was found
.pipe(phpcs.reporter('log'))
.pipe(postcss([
postcssPresetEnv({
stage: 3,
browsers: config.dev.browserslist,
features: {
'custom-properties': {
preserve: false,
variables: cssVars.variables,
},
'custom-media-queries': {
preserve: false,
extensions: cssVars.queries,
}
}
})
]))
.pipe(replace('wprig', config.theme.slug))
.pipe(replace('WP Rig', config.theme.name))
.pipe(gulp.dest(paths.verbose))
.pipe(gulpif(!config.dev.debug.styles, cssnano()))
.pipe(gulp.dest(paths.styles.dest));
}
/**
* JavaScript via Babel, ESlint, and uglify.
*/
export function scripts() {
config = requireUncached('./dev/config/themeConfig.js');
return gulp .src(paths.scripts.src)
.pipe(newer(paths.scripts.dest))
.pipe(eslint())
.pipe(eslint.format())
.pipe(babel())
.pipe(gulp.dest(paths.verbose))
.pipe(gulpif(!config.dev.debug.scripts, uglify()))
.pipe(replace('wprig', config.theme.slug))
.pipe(replace('WP Rig', config.theme.name))
.pipe(gulp.dest(paths.scripts.dest));
}
/**
* Copy JS libraries without touching them.
*/
export function jsLibs() {
return gulp.src(paths.scripts.libs)
.pipe(newer(paths.scripts.verboseLibsDest))
.pipe(gulp.dest(paths.scripts.verboseLibsDest))
.pipe(gulp.dest(paths.scripts.libsDest));
}
/**
* Copy minified JS files without touching them.
*/
export function jsMin() {
return gulp.src(paths.scripts.min)
.pipe(newer(paths.scripts.dest))
.pipe(gulp.dest(paths.verbose))
.pipe(gulp.dest(paths.scripts.dest));
}
/**
* Optimize images.
*/
export function images() {
return gulp.src(paths.images.src)
.pipe(newer(paths.images.dest))
.pipe(image())
.pipe(gulp.dest(paths.images.dest));
}
/**
* Watch everything
*/
export function watch() {
gulp.watch(paths.php.src, gulp.series(php, reload));
gulp.watch(paths.config.themeConfig, gulp.series(php, reload));
gulp.watch(paths.config.cssVars, gulp.series(styles, reload));
gulp.watch(paths.styles.sass, sassStyles);
gulp.watch(paths.styles.src, gulp.series(styles, reload));
gulp.watch(paths.scripts.src, gulp.series(scripts, reload));
gulp.watch(paths.scripts.min, gulp.series(jsMin, reload));
gulp.watch(paths.scripts.libs, gulp.series(jsLibs, reload));
gulp.watch(paths.images.src, gulp.series(images, reload));
}
/**
* Map out the sequence of events on first load:
*/
const firstRun = gulp.series(php, gulp.parallel(scripts, jsMin, jsLibs), sassStyles, styles, images, serve, watch);
/**
* Run the whole thing.
*/
export default firstRun;
/**
* Generate translation files.
*/
export function translate() {
return gulp.src(paths.languages.src)
.pipe(sort())
.pipe(wppot({
domain: config.theme.slug,
package: config.theme.name,
bugReport: config.theme.name,
lastTranslator: config.theme.author
}))
.pipe(gulp.dest(paths.languages.dest));
}
/**
* Create zip archive from generated theme files.
*/
export function bundle() {
return gulp.src(paths.export.src)
.pipe(print())
.pipe(gulpif(config.export.compress, zip(config.theme.slug + '.zip'), gulp.dest(paths.export.dest + config.theme.slug)))
.pipe(gulpif(config.export.compress, gulp.dest(paths.export.dest)));
}
/**
* Test the theme.
*/
const testTheme = gulp.series(php);
/**
* Export theme for distribution.
*/
const bundleTheme = gulp.series(testTheme, gulp.parallel(scripts, jsMin, jsLibs), styles, images, translate, bundle);
export { testTheme, bundleTheme };