-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
233 lines (216 loc) · 7.05 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
/**
* Template last updated: 2019-02-21.
* File last updated: 2019-04-12.
*/
/**
* Directories.
*/
var dir = {
php: 'public_html/wp-content/themes/coderdojo-2019',
input: {
js: 'public_html/wp-content/themes/coderdojo-2019/js',
sass: 'public_html/wp-content/themes/coderdojo-2019/sass',
},
output: {
js: 'public_html/wp-content/themes/coderdojo-2019/assets/js',
sass: 'public_html/wp-content/themes/coderdojo-2019/assets/css',
},
};
/**
* Packages.
*/
var gulp = require( 'gulp' );
var autoprefixer = require( 'gulp-autoprefixer' );
var cleancss = require( 'gulp-clean-css' );
var concat = require( 'gulp-concat' );
var filter = require( 'gulp-filter' );
var gulpif = require( 'gulp-if' );
var livereload = require( 'gulp-livereload' );
var notify = require( 'gulp-notify' );
var plumber = require( 'gulp-plumber' );
var rename = require( 'gulp-rename' );
var sass = require( 'gulp-sass' );
var sassglob = require( 'gulp-sass-glob' );
var sourcemaps = require( 'gulp-sourcemaps' );
var uglify = require( 'gulp-uglify' );
var util = require( 'gulp-util' );
var babel = require( 'gulp-babel' );
/**
* Environment.
*/
var env = ( util.env.env ? util.env.env : 'dev' );
/**
* Config.
*/
var config = {
run_js_sourcemaps: ( env == 'dev' ? true : false ),
run_sass_sourcemaps: ( env == 'dev' ? true : false ),
run_js_minification: ( env == 'dev' ? false : true ),
run_sass_minification: ( env == 'dev' ? false : true ),
};
/**
* Feedback.
*/
console.log( '' );
console.log( 'Environment: '+( env == 'dev' ? 'Development' : 'Production' ) );
console.log( '' );
console.log( 'JS sourcemaps: '+( config.run_js_sourcemaps ? 'Yes' : 'No' ) );
console.log( 'CSS sourcemaps: '+( config.run_sass_sourcemaps ? 'Yes' : 'No' ) );
console.log( 'JS minification: '+( config.run_js_minification ? 'Yes' : 'No' ) );
console.log( 'CSS minification: '+( config.run_sass_minification ? 'Yes' : 'No' ) );
console.log( '' );
/**
* Procedures.
*/
var app = [];
app.processJS = function ( args ) {
// use all the files
return gulp.src( args.inputFiles )
// catch errors
.pipe( plumber( { errorHandler: notify.onError( {
title: args.name,
message: '<%= error.type %> error on line <%= error.line %>\n\n<%= error.filename %>',
} ) } ) )
// start the sourcemap
.pipe( gulpif( config.run_js_sourcemaps, sourcemaps.init() ) )
// compile
.pipe( babel( { presets: ['@babel/env'] } ) )
// concat the js
.pipe( concat( args.outputFile ) )
// minify the js
.pipe( gulpif( config.run_js_minification, uglify() ) )
// finish the sourcemap
.pipe( gulpif( config.run_js_sourcemaps, sourcemaps.write( '.' ) ) )
// place the output file
.pipe( gulp.dest( args.outputDir ) )
// remove the sourcemap from the stream
.pipe( gulpif( config.run_js_sourcemaps, filter( [ '**/*.js' ] ) ) )
// reload the site
.pipe( livereload() );
};
app.processSass = function ( args ) {
// use all the files
return gulp.src( args.inputFiles )
// catch errors
.pipe( plumber( { errorHandler: notify.onError( {
title: 'Error in ' + args.name,
message: '<%= error.messageOriginal %>\n\n<%= error.relativePath %>\n\nLine <%= error.line %>, column <%= error.column %>.',
} ) } ) )
// start the sourcemap
.pipe( gulpif( config.run_sass_sourcemaps, sourcemaps.init() ) )
// analyse the globs
.pipe( sassglob() )
// compile the sass to css
.pipe( sass( { includePaths: ['node_modules'] } ) )
// autoprefix the css
.pipe( autoprefixer( 'last 10 versions' ) )
// minify the css
.pipe( gulpif( config.run_sass_minification, cleancss( { keepSpecialComments: 0 } ) ) )
// name the output file
.pipe( rename( args.outputFile ) )
// finish the sourcemap
.pipe( gulpif( config.run_sass_sourcemaps, sourcemaps.write( '.' ) ) )
// place the output file
.pipe( gulp.dest( args.outputDir ) )
// remove the sourcemap from the stream
.pipe( gulpif( config.run_sass_sourcemaps, filter( [ '**/*.css' ] ) ) )
// reload the site
.pipe( livereload() );
};
/**
* Tasks: JS.
*/
gulp.task( 'js_theme', function ( done ) {
app.processJS({
'name' : 'theme js',
'inputFiles' : [ dir.input.js+'/theme.js' ],
'outputDir' : dir.output.js,
'outputFile' : 'theme.min.js',
});
done();
});
gulp.task( 'js_wpadmin', function ( done ) {
app.processJS({
'name' : 'wpadmin js',
'inputFiles' : [ dir.input.js+'/wpadmin.js' ],
'outputDir' : dir.output.js,
'outputFile' : 'wpadmin.min.js',
});
done();
});
gulp.task( 'js_login', function ( done ) {
app.processJS({
'name' : 'login js',
'inputFiles' : [ dir.input.js+'/login.js' ],
'outputDir' : dir.output.js,
'outputFile' : 'login.min.js',
});
done();
});
/**
* Tasks: Sass.
*/
gulp.task( 'sass_theme', function ( done ) {
app.processSass({
'name' : 'theme sass',
'inputFiles' : [ dir.input.sass+'/theme.scss' ],
'outputDir' : dir.output.sass,
'outputFile' : 'theme.min.css',
});
done();
});
gulp.task( 'sass_wpadmin', function ( done ) {
app.processSass({
'name' : 'wpadmin sass',
'inputFiles' : [ dir.input.sass+'/wpadmin.scss' ],
'outputDir' : dir.output.sass,
'outputFile' : 'wpadmin.min.css',
});
done();
});
gulp.task( 'sass_tinymce', function ( done ) {
app.processSass({
'name' : 'tinymce sass',
'inputFiles' : [ dir.input.sass+'/tinymce.scss' ],
'outputDir' : dir.output.sass,
'outputFile' : 'tinymce.min.css',
});
done();
});
/**
* Task: Livereload.
*/
gulp.task( 'livereload', function ( done ) {
livereload.reload();
done();
});
/**
* Task: Watch.
*/
gulp.task( 'watch', function () {
// start livereload
livereload.listen();
// JavaScript
gulp.watch( dir.input.js+'/theme.js', gulp.parallel( 'js_theme' ) );
// gulp.watch( dir.input.js+'/wpadmin.js', gulp.parallel( 'js_wpadmin' ) );
// gulp.watch( dir.input.js+'/login.js', gulp.parallel( 'js_login' ) );
// Sass
gulp.watch( dir.input.sass+'/**/*.scss', gulp.parallel( 'sass_theme' ) );
// gulp.watch( dir.input.sass+'/wpadmin.scss', gulp.parallel( 'sass_wpadmin' ) );
// gulp.watch( dir.input.sass+'/tinymce.scss', gulp.parallel( 'sass_tinymce' ) );
// PHP
gulp.watch( dir.php+'/**/*.php', gulp.parallel( 'livereload' ) );
// notify
gulp.src( 'node_modules/gulp-notify/test/fixtures/1.txt' ).pipe( notify( 'Gulp is watching.' ) );
});
/**
* Task: Default.
*/
gulp.task( 'default', gulp.parallel(
'js_theme',
// 'js_wpadmin',
// 'js_login',
'sass_theme',
// 'sass_wpadmin',
// 'sass_tinymce',
));