This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.js
338 lines (301 loc) · 9.68 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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/**
* This Gulp script generates Wordpress themes using Jade + Stylus
* + support gulp 4.0.2, node 12.13.0 LTS
* + Install Wordpress only if it hasn't been installed yet
*/
const fs = require('fs');
const _ = require('lodash');
const gulp = require('gulp');
const babel = require('gulp-babel');
const download = require('gulp-download');
const unzip = require('gulp-unzip');
const jade = require('gulp-jade-php');
const concat = require('gulp-concat');
const wrap = require('gulp-wrap');
const gulpif = require('gulp-if');
const stylus = require('gulp-stylus');
const uglify = require('gulp-uglify');
const order = require('gulp-order');
const plumber = require('gulp-plumber');
const minifyCSS = require('gulp-clean-css');
const imagemin = require('gulp-imagemin');
const cache = require('gulp-cached');
const pot = require('gulp-wp-pot');
const sort = require('gulp-sort');
const replace = require('gulp-replace');
const gettext = require('gulp-gettext');
const sourcemaps = require('gulp-sourcemaps');
const remember = require('gulp-remember');
const nib = require('nib');
const jeet = require('jeet');
const del = require('del');
const hasFile = fs.existsSync;
const utils = require('./utils');
/**
* Configuration object, following this priority:
* 1°) the default parameters
* 2°) the config.json file at the root
* 3°) the arguments
*/
const config = _.merge({
latestWordpressURL: 'https://wordpress.org/latest.zip',
production: false,
locals: {
version: Date.now()
},
server: {
open: false,
notify: false
},
rename: false // rename the theme name
},
require('./config.json'),
require('yargs').argv
);
if (_.isUndefined(config.domain) && !_.isUndefined(config.theme)) {
config.domain = _.kebabCase(config.theme);
config.server.logPrefix = config.theme;
}
/**
* Configuring browser-sync
* This is obviously ugly because we don't install browser-sync in production
*/
let server;
if (config.production) {
server = {
stream: function() {
return true;
}
};
} else {
server = require('browser-sync').create();
}
/**
* The assets paths
*/
const paths = {
root: 'themes/' + config.theme,
config: 'themes/' + config.theme + '/config.json',
stylesheets: 'themes/' + config.theme + '/stylesheets',
languages: 'themes/' + config.theme + '/languages/*.po',
javascripts: 'themes/' + config.theme + '/javascripts/**/*.js',
templates: 'themes/' + config.theme + '/templates/**/*.jade',
images: 'themes/' + config.theme + '/images/**/*',
functions: 'themes/' + config.theme + '/functions.php',
destination: 'public/wp-content/themes/' + config.theme,
misc: [
'themes/' + config.theme + '/**/*',
'!themes/' + config.theme + '/{templates,javascripts,stylesheets,languages,images}/**/*',
'!themes/' + config.theme + '/{templates,javascripts,stylesheets,languages,images,config.json,functions.php}'
]
};
/**
* Creates the `public` folder from unzipping the latest Wordpress release
*/
/**
* Downloads the latest Wordpress release
*/
gulp.task('download', function() {
return download(config.latestWordpressURL).pipe(gulp.dest(__dirname + '/tmp'));
});
/**
* Unzips the latest release to the current directory
*/
gulp.task('unzip', function() {
return gulp.src(__dirname + '/tmp/latest.zip')
.pipe(unzip())
.pipe(gulp.dest(__dirname));
});
/**
* Copies all the files in the `wordpress` folder to a `public` folder
*/
gulp.task('rename', function() {
return gulp.src(__dirname + '/wordpress/**/*')
.pipe(gulp.dest(__dirname + '/public'));
});
/**
* Deletes the previously created `wordpress` folder
*/
gulp.task('delete', function(callback) {
return del([
__dirname + '/wordpress',
__dirname + '/tmp'
], callback);
});
/**
* Compiles all the javascripts files into a core.js file
* If we're running this in production, minifies the file
*/
gulp.task('compileJavascripts', function() {
const fileName = 'core.js';
return gulp.src(paths.javascripts)
.pipe(plumber())
.pipe(order([ 'jquery.js' ]))
.pipe(cache('core'))
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(babel({
presets: [['env', { targets: { ie: 9 } }]],
plugins: ['transform-remove-strict-mode']
}))
.pipe(remember('core'))
.pipe(concat(fileName))
.pipe(gulpif(config.production, uglify({compress: false})))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(paths.destination))
.pipe(gulpif(!config.production, server.stream()));
});
/**
* Compiles the Stylus style.styl file into a style.css file at the theme's root
* Also appends the config.json file at the top of the style.css, based on the
* css-template.txt file
*/
gulp.task('compileStylesheets', function() {
const configPath = __dirname + '/' + paths.config;
let themeMeta = false;
if (hasFile(configPath)) {
const json = require(configPath);
if (_.isUndefined(json['text-domain'])) {
json['text-domain'] = config.domain;
}
if (!_.isUndefined(config.headers)) {
_.merge(json, config.headers);
}
themeMeta = utils.parseConfigFile(json);
}
return gulp.src(paths.stylesheets + '/style.styl')
.pipe(plumber())
.pipe(stylus({ use: [nib(), jeet()] }))
.pipe(gulpif(config.production, minifyCSS()))
.pipe(gulpif(!!themeMeta, wrap({ src: __dirname + '/css-template.txt'}, { meta: themeMeta })))
.pipe(gulp.dest(paths.destination))
.pipe(gulpif(!config.production, server.stream()));
});
/**
* Compiles Jade templates into theme directory
*/
gulp.task('compileTemplates', function() {
return gulp.src(paths.templates)
.pipe(plumber())
.pipe(jade({ locals: config.locals }))
.pipe(gulp.dest(paths.destination))
.pipe(gulpif(!config.production, server.stream()));
});
/**
* Analyzes PHP files and generates a POT file
*/
gulp.task('compilePOT', function() {
const configPath = __dirname + '/' + paths.config;
const potConfig = {
domain: '$text_domain'
};
if (hasFile(configPath)) {
const json = require(configPath);
if (!_.isUndefined(json['author-uri'])) {
potConfig.bugReport = json['author-uri'];
}
if (!_.isUndefined(json['author'])) {
potConfig.team = json['author'];
}
}
return gulp.src(paths.destination + '/**/*.php')
.pipe(pot(potConfig))
.pipe(gulp.dest(paths.destination + '/languages/' + config.domain + '.pot'))
.pipe(gulp.dest(paths.root + '/languages/' + config.domain + '.pot'));
});
/**
* Compiles PO files into MO files
*/
gulp.task('compilePO', function() {
return gulp.src(paths.languages)
.pipe(gettext())
.pipe(gulp.dest(paths.destination + '/languages'))
.pipe(gulpif(!config.production, server.stream()));
});
/**
* Compress images into theme directory
*/
gulp.task('compileImages', function() {
return gulp.src(paths.images)
.pipe(plumber())
.pipe(cache('images'))
.pipe(imagemin())
.pipe(gulp.dest(paths.destination + '/images'))
.pipe(gulpif(!config.production, server.stream()));
});
/**
* Add the text domain into the functions.php file and automatically reloads the page when the functions.php changes
*/
gulp.task('compileFunctions', function() {
return gulp.src(paths.functions)
.pipe(plumber())
.pipe(replace('$text_domain', '"' + config.domain + '"'))
.pipe(wrap('<?php global $text_domain; $text_domain = "' + config.domain + '"; $version = "' + Date.now() + '"; ?><%= contents %>'))
.pipe(gulp.dest(paths.destination))
.pipe(gulpif(!config.production, server.stream()));
});
/**
* Copy all the files in themes that are not in the
* templates/javascripts/stylesheets folders or the config.json file
*/
gulp.task('compileMisc', function() {
return gulp.src(paths.misc)
.pipe(gulp.dest(paths.destination));
});
/**
* Compiles all the assets
*/
gulp.task('compile', gulp.series(
'compileTemplates',
'compileStylesheets',
'compileJavascripts',
'compileImages',
'compileFunctions',
'compilePOT',
'compilePO',
'compileMisc'
));
/**
* Watch all the assets
*/
gulp.task('watchers', function() {
gulp.watch([paths.stylesheets + '/**/*.styl', paths.config], gulp.series('compileStylesheets'));
gulp.watch([paths.templates], gulp.series('compileTemplates', 'compilePOT'));
gulp.watch([paths.javascripts], gulp.series('compileJavascripts'));
gulp.watch([paths.images], gulp.series('compileImages'));
gulp.watch([paths.root + '/functions/**/*.php'], gulp.series('compileFunctions'));
gulp.watch([paths.functions], gulp.series('compilePOT'));
gulp.watch([paths.languages], gulp.series('compilePO'));
});
/**
* Starts the live-reloaded web server
*/
gulp.task('live-reload', function(done) {
if (config.server && !_.isUndefined(config.server.proxy)) {
server.init(config.server);
}
done();
});
/**
* Cleans everything by deleting newly created folders
*/
gulp.task('hard-clean', function(callback) {
del([
__dirname + '/public',
__dirname + '/wordpress',
__dirname + '/tmp'
], callback);
});
/**
* Compiles then watch assets if not in production
*/
gulp.task('launch', gulp.series(function loadTasks(done){
if (!hasFile(__dirname + '/public') && !config.production) {
gulp.series('download', 'unzip', 'rename', 'delete', 'compile')();
} else if (!config.production) {
gulp.series('compile', 'live-reload', 'watchers')();
} else{
gulp.series('compile')();
}
done();
}));
gulp.task('default', gulp.series('launch'));