-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
130 lines (102 loc) · 3.99 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
// Defining requirements
var gulp = require( 'gulp' );
var gulpif = require('gulp-if');
var plumber = require( 'gulp-plumber' );
var sass = require( 'gulp-sass' );
var watch = require( 'gulp-watch' );
var concat = require( 'gulp-concat' );
var uglify = require( 'gulp-uglify' );
var streamqueue = require('streamqueue');
var sourcemaps = require( 'gulp-sourcemaps' );
var browserSync = require( 'browser-sync' ).create();
var del = require( 'del' );
var cleanCSS = require( 'gulp-clean-css' );
var autoprefixer = require( 'gulp-autoprefixer' );
// Configuration file to keep your code DRY
var cfg = require( './gulpconfig.json' );
var paths = cfg.paths;
function cp_watch_helper_css_js(enviroment = pro) {
gulp.watch( paths.assets + '/css/*.css', gulp.parallel( enviroment + '-style') );
gulp.watch( paths.scss + '/*.scss', gulp.series( enviroment + '-style') );
gulp.watch( paths.assets + '/js/*.js', gulp.series( enviroment + '-script') );
browserSync.reload();
}
function cp_css_dev_helper(sources,output = 'pro', filename, source_maps = true) {
var stream = sources
.pipe( sourcemaps.init( { loadMaps: true } ) )
.pipe(autoprefixer({ browsers: ['last 30 versions'],cascade: false}))
.pipe( gulpif(output == 'pro', cleanCSS( { compatibility: '*' } )) )
.pipe( plumber( {
errorHandler: function( err ) {
console.log( err );
this.emit( 'end' );
}
} ) )
.pipe(concat(filename))
.pipe( gulpif(source_maps == true, sourcemaps.write( './' )) )
.pipe( gulp.dest( paths.dist + '/css' ) );
return stream;
}
function cp_style_export_helper(enviroment) {
var stream = streamqueue({ objectMode: true },
gulp.src(paths.wp_stylesheet),
cp_css_dev_helper(gulp.src( paths.scss + '/*.scss' ).pipe( sass( { errLogToConsole: true } ) ), enviroment, 'sass-output.css', false),
cp_css_dev_helper(gulp.src( paths.normal_css_file_list ), enviroment, 'normal.min.css', false)
)
.pipe(concat('style.css'))
.pipe( sourcemaps.write( './' ) )
.pipe( gulp.dest( './' ) );
return stream;
}
gulp.task('pro-style', function(){
return cp_style_export_helper('pro');
});
gulp.task('dev-style', function(){
return cp_style_export_helper('dev');
});
gulp.task('dev-normal-css', function(){
var stream = cp_css_dev_helper(gulp.src( paths.normal_css_file_list ), 'dev', 'normal.css');
return stream;
});
gulp.task('pro-normal-css', function(){
var stream = cp_css_dev_helper(gulp.src( paths.normal_css_file_list ), 'pro', 'normal.min.css');
return stream;
});
gulp.task('dev-sass', function(){
var stream = cp_css_dev_helper(gulp.src( paths.scss + '/*.scss' ).pipe( sass( { errLogToConsole: true } ) ), 'dev', 'sass-output.css');
return stream;
});
gulp.task('pro-sass', function(){
var stream = cp_css_dev_helper(gulp.src( paths.scss + '/*.scss' ).pipe( sass( { errLogToConsole: true } ) ), 'pro', 'sass-output.min.css');
return stream;
});
gulp.task('dev-script', function(){
var stream = gulp.src( paths.js_file_list )
.pipe( concat( 'script.js' ) )
.pipe( gulp.dest( paths.dist + '/js' ) );
return stream;
});
gulp.task('pro-script', function(){
var stream = gulp.src( paths.js_file_list )
.pipe( concat( 'script.min.js' ) )
.pipe( uglify() )
.pipe( gulp.dest( paths.dist + '/js' ) );
return stream;
});
gulp.task('pro-watch', function(done){
del(['./style.css', './style.css.map', cfg.paths.dist + '/css/*', cfg.paths.dist + '/js/*']);
cp_watch_helper_css_js('pro');
browserSync.reload();
done();
});
gulp.task( 'dev-watch', function(done) {
cp_watch_helper_css_js('dev');
browserSync.reload();
done();
});
gulp.task( 'browser-sync', function(done) {
browserSync.init( cfg.browserSyncWatchFiles, cfg.browserSyncOptions );
done();
} );
gulp.task( 'dev', gulp.series('browser-sync', 'dev-watch'));
gulp.task( 'pro', gulp.series('browser-sync', 'pro-watch'));