-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
executable file
·289 lines (254 loc) · 6.22 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
/**
* Browser Sync proxy URL.
* Update this URL to point to your local environment URL.
*/
const proxy_url = '127.0.0.1:8080/basetheme';
/**
* Require
*/
const gulp = require( 'gulp' );
const sass = require( 'gulp-sass' );
const postcss = require( 'gulp-postcss' );
const autoprefixer = require( 'autoprefixer' );
const cssnano = require( 'cssnano' );
const sourcemaps = require( 'gulp-sourcemaps' );
const uglify = require( 'gulp-uglify' );
const babel = require( 'gulp-babel' );
const eslint = require( 'gulp-eslint' );
const concat = require( 'gulp-concat' );
const mergeStream = require( 'merge-stream' );
const through2 = require( 'through2' );
const browserSync = require( 'browser-sync' ).create();
const fs = require( 'fs' );
const del = require( 'del' );
/**
* Directories that we'll watch and/or process
*/
const paths = {
styles: {
src: [
'./src/scss/**/*.scss',
'!./src/scss/blocks/enqueue_style/**/*.scss'
],
dest: './dist/css/',
blocks: {
src: [ './src/scss/blocks/enqueue_style/**/*.scss' ],
dest: './dist/css/blocks/'
}
},
js: {
blocks: {
dir: './src/js/blocks/',
src: [ './src/js/blocks/' ],
dest: './dist/js/blocks'
},
head: {
src: {
vendor: [
'./src/js/head/vendor/**/*.js',
'./src/js/head/bootstrap/**/*.js'
],
custom: [ './src/js/head/custom/**/*.js' ]
},
dest: './dist/js'
},
footer: {
src: {
vendor: [
'./src/js/footer/vendor/**/*.js',
'./src/js/footer/bootstrap/**/*.js'
],
custom: [ './src/js/footer/custom/**/*.js' ]
},
dest: './dist/js'
}
}
};
/**
* SCSS
*/
function style() {
return do_styles( paths.styles.src, paths.styles.dest );
}
function blockStyle() {
return do_styles( paths.styles.blocks.src, paths.styles.blocks.dest );
}
function do_styles( src, dest ) {
return (
gulp
.src( src )
// Initialize sourcemaps before compilation starts
.pipe( sourcemaps.init() )
.pipe( sass() )
.on( 'error', sass.logError )
// Use postcss with autoprefixer and compress the compiled file using cssnano
.pipe( postcss([ autoprefixer(), cssnano() ]) )
// Now add/write the sourcemaps
.pipe( sourcemaps.write( './' ) )
.pipe(
through2.obj( function( file, enc, cb ) {
var date = new Date();
file.stat.atime = date;
file.stat.mtime = date;
cb( null, file );
})
)
.pipe( gulp.dest( dest ) )
// Add browsersync stream pipe after compilation
.pipe( browserSync.stream() )
);
}
/**
* Javascript
*/
function doJS( scripts_nobabel, scripts_babel, scripts_dest, filename ) {
var streams = [];
/**
* Don't put vendor scripts through babel.
*/
scripts_nobabel.forEach( function( path ) {
streams.push( gulp.src( path ).pipe( uglify() ) );
});
/**
* Process custom scripts through eslint, babel, and uglify.
*/
scripts_babel.forEach( function( path ) {
streams.push(
gulp
.src( path )
.pipe( eslint() )
.pipe( eslint.format() )
//.pipe( eslint.failAfterError() )
.pipe( babel({ presets: [ '@babel/preset-env' ] }) )
.pipe( uglify() )
);
});
/**
* Merge everything into a single destination.
*/
return mergeStream( streams )
.pipe( concat( filename ) )
.pipe( gulp.dest( scripts_dest ) )
.pipe( browserSync.stream() );
}
function scriptshead() {
return doJS(
paths.js.head.src.vendor,
paths.js.head.src.custom,
paths.js.head.dest,
'head.min.js'
);
}
function scriptsfooter() {
return doJS(
paths.js.footer.src.vendor,
paths.js.footer.src.custom,
paths.js.footer.dest,
'scripts.min.js'
);
}
function scriptsblocks() {
var streams = [];
const dir = paths.js.blocks.dir;
/**
* Delete all javascript in the /dist/js/blocks/ folder.
* If we don't do this, sometimes old block scripts will remain.
*/
del([ paths.js.blocks.dest + '/**' ]);
files = fs.readdirSync( dir );
files.forEach( file => {
/**
* Loop through each block javascript directory.
*/
if ( fs.lstatSync( dir + file ).isDirectory() ) {
const block_dir = dir + file;
var block_stream = [];
if ( fs.existsSync( block_dir + '/vendor/' ) ) {
/**
* Add the vendor scripts for this block first.
*/
block_stream.push(
gulp.src( block_dir + '/vendor/**/*.js' ).pipe( uglify() )
);
}
/**
* Now add the remaining javascript files.
* Run those through babel and uglify.
*/
block_stream.push(
gulp
.src([
block_dir + '/**/*.js',
'!' + block_dir + '/vendor/**/*.js'
])
.pipe( eslint() )
.pipe( eslint.format() )
//.pipe( eslint.failAfterError() )
.pipe( babel({ presets: [ '@babel/preset-env' ] }) )
.pipe( uglify() )
);
/**
* Concat everything into a single block-{name}.min.js file.
*/
streams.push(
mergeStream( block_stream ).pipe(
concat( 'block-' + file + '.min.js' )
)
);
}
});
if ( 0 < streams.length ) {
/**
* Merge all streams and pipe to the /dist/ folder.
*/
return mergeStream( streams )
.pipe( gulp.dest( paths.js.blocks.dest ) )
.pipe( browserSync.stream() );
}
return gulp.src( dir );
}
/**
* Watch
*/
function watch() {
/**
* BrowserSync.
* Watch for changes to PHP or image files.
*/
browserSync.init([ '**/*.php', 'img/**/*.{png,jpg,gif}' ], {
proxy: proxy_url,
open: false
});
/**
* Watch various src folders and run appropriate functions.
*/
gulp.watch( paths.styles.src, style );
gulp.watch( paths.styles.blocks.src, blockStyle );
gulp.watch( paths.js.head.src.vendor, scriptshead );
gulp.watch( paths.js.head.src.custom, scriptshead );
gulp.watch( paths.js.footer.src.vendor, scriptsfooter );
gulp.watch( paths.js.footer.src.custom, scriptsfooter );
gulp.watch( paths.js.blocks.src, scriptsblocks );
}
/**
* Export
*/
exports.watch = watch;
/**
* Expose the task by exporting it
* This allows you to run it from the commandline using
* $ gulp style
*/
exports.style = style;
exports.blockStyle = blockStyle;
exports.scriptsblocks = scriptsblocks;
exports.scriptshead = scriptshead;
exports.scriptsfooter = scriptsfooter;
/*
* Specify if tasks run in series or parallel using `gulp.series` and `gulp.parallel`
*/
const build = gulp.parallel( watch );
/*
* Define default task that can be called by just running `gulp` from cli
*/
gulp.task( 'default', build );