-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
205 lines (188 loc) · 5.3 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
/**
* Toscani's Gulp 4 gulpfile template.
*
* Template last updated: 2019-12-17.
* File last updated: 2020-12-03.
*/
/**
* Directories.
*/
var dir = {
php: '.',
input: {
js: '.',
less: '.',
},
output: {
js: '.',
less: '.',
},
};
/**
* 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 less = require( 'gulp-less' );
var sourcemaps = require( 'gulp-sourcemaps' );
var uglify = require( 'gulp-uglify' );
var babel = require( 'gulp-babel' );
var argv = require( 'minimist' )( process.argv.slice( 2 ) );
/**
* Environment.
*/
var env = ( argv.env ? argv.env : 'dev' );
/**
* Config.
*/
var config = {
run_sourcemaps: ( env == 'dev' ? true : false ),
run_minification: ( env == 'dev' ? false : true ),
};
/**
* Feedback.
*/
console.log( '' );
console.log( 'Environment: '+( env == 'dev' ? 'Development' : 'Production' ) );
console.log( '' );
console.log( 'Sourcemaps: '+( config.run_sourcemaps ? 'Yes' : 'No' ) );
console.log( 'Minification: '+( config.run_minification ? 'Yes' : 'No' ) );
console.log( '' );
/**
* Plumber notification.
*/
var onError = function ( error ) {
notify.onError({
title: "Error in "+error.filename.replace( /^.*[\\\/]/, '' )+" on line "+error.line,
message: "-\n"+error.extract,
appID: "Gulp",
})( error );
this.emit('end');
};
/**
* Procedures.
*/
var app = [];
app.processJS = function ( args ) {
// use all the files
return gulp.src( args.inputFiles )
// catch errors
.pipe( plumber( { errorHandler: onError } ) )
// start the sourcemap
.pipe( gulpif( config.run_sourcemaps, sourcemaps.init() ) )
// compile
.pipe( babel( { presets: ['@babel/env'] } ) )
// concat the js
.pipe( concat( args.outputFile ) )
// minify the js
.pipe( gulpif( config.run_minification, uglify() ) )
// finish the sourcemap
.pipe( gulpif( config.run_sourcemaps, sourcemaps.write( '.' ) ) )
// place the output file
.pipe( gulp.dest( args.outputDir ) )
// remove the sourcemap from the stream
.pipe( gulpif( config.run_sourcemaps, filter( [ '**/*.js' ] ) ) )
// notify
.pipe( notify({
title: "Processed",
message: args.name,
appID: "Gulp",
}) )
// reload the site
.pipe( livereload() );
};
app.processLess = function ( args ) {
// use all the files
return gulp.src( args.inputFiles )
// catch errors
.pipe( plumber( { errorHandler: onError } ) )
// start the sourcemap
.pipe( gulpif( config.run_sourcemaps, sourcemaps.init() ) )
// compile the less to css
.pipe( less() )
// autoprefix the css
.pipe( autoprefixer( 'last 10 versions' ) )
// minify the css
.pipe( gulpif( config.run_minification, cleancss( { keepSpecialComments: 0 } ) ) )
// name the output file
.pipe( rename( args.outputFile ) )
// finish the sourcemap
.pipe( gulpif( config.run_sourcemaps, sourcemaps.write( '.' ) ) )
// place the output file
.pipe( gulp.dest( args.outputDir ) )
// remove the sourcemap from the stream
.pipe( gulpif( config.run_sourcemaps, filter( [ '**/*.css' ] ) ) )
// notify
.pipe( notify({
title: "Processed",
message: args.name,
appID: "Gulp",
}) )
// reload the site
.pipe( livereload() );
};
/**
* Tasks: JS.
*/
gulp.task( 'js_plugin', function ( done ) {
app.processJS({
'name' : 'plugin js',
'inputFiles' : [ dir.input.js+'/we-use-cookies.js' ],
'outputDir' : dir.output.js,
'outputFile' : 'we-use-cookies.min.js',
});
done();
});
/**
* Tasks: Less.
*/
gulp.task( 'less_plugin', function ( done ) {
app.processLess({
'name' : 'plugin less',
'inputFiles' : [ dir.input.less+'/we-use-cookies.less' ],
'outputDir' : dir.output.less,
'outputFile' : 'we-use-cookies.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+'/we-use-cookies.js', gulp.parallel( 'js_plugin' ) );
// Less
gulp.watch( dir.input.less+'/we-use-cookies.less', gulp.parallel( 'less_plugin' ) );
// PHP
gulp.watch( dir.php+'/**/*.php', gulp.parallel( 'livereload' ) );
// notify
gulp.src( 'node_modules/gulp-notify/test/fixtures/1.txt' ).pipe( notify({
title: "Gulp watch is ready.",
message: " ",
appID: "Gulp",
}) );
});
/**
* Task: Default.
*/
gulp.task( 'default', gulp.parallel(
'js_plugin',
'less_plugin',
));