forked from WebDevStudios/constant-contact-forms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
191 lines (160 loc) · 5.24 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
const autoprefixer = require('autoprefixer');
const browserSync = require('browser-sync');
const cssnano = require('cssnano');
const del = require('del');
const fs = require('fs'); // node file system manipulation
const gulp = require('gulp');
const gutil = require('gulp-util');
const mqpacker = require('css-mqpacker');
const notify = require('gulp-notify');
const path = require('path'); // node path module
const plumber = require('gulp-plumber');
const postcss = require('gulp-postcss');
const rename = require('gulp-rename');
const sass = require('gulp-sass')(require('sass'));
const sourcemaps = require('gulp-sourcemaps');
const webpack = require('webpack');
const webpackStream = require('webpack-stream');
const wpPot = require('gulp-wp-pot');
const pluginConfig = require('./plugin-config');
const webpackConfig = require( './webpack.config' );
/**
* Handle errors and alert the user.
*/
function handleErrors () {
var args = Array.prototype.slice.call(arguments);
notify.onError({
title: 'Task Failed [<%= error.message %>',
message: 'See console.',
sound: 'Sosumi' // See: https://github.com/mikaelbr/node-notifier#all-notification-options-with-their-defaults
}).apply(this, args);
gutil.beep(); // Beep 'sosumi' again
// Prevent the 'watch' task from stopping
this.emit('end');
}
/**
* Read directory name for filename
*/
function getFolders(dir) {
return fs.readdirSync(dir)
.filter(function(file) {
return fs.statSync(path.join(dir, file)).isDirectory();
});
}
/**
* Use webpack to transpile and bundle scripts.
*/
gulp.task('scripts', function(done) {
gulp.src( [
'./assets/js/ctct-plugin-frontend/index.js',
'./assets/js/ctct-plugin-gutenberg/index.js',
'./assets/js/ctct-plugin-admin/index.js',
'./assets/js/ctct-plugin-recaptcha/index.js',
'./assets/js/ctct-plugin-recaptcha-v2/index.js'
] )
.pipe( webpackStream( webpackConfig ), webpack )
.pipe( gulp.dest( './assets/js' ) );
done();
});
/**
* Delete style.css and style.min.css before we minify and optimize
*/
gulp.task('clean:styles', function() {
return del(['assets/css/style.css', 'assets/css/style.min.css'])
});
/**
* Compile Sass
*
* https://www.npmjs.com/package/gulp-sass
*/
gulp.task('sass', function() {
return gulp.src('assets/sass/*.scss')
// Deal with errors.
.pipe(plumber({ errorHandler: handleErrors }))
// Compile Sass using LibSass.
.pipe(sass({
outputStyle: 'expanded' // Options: nested, expanded, compact, compressed
}))
// Create style.css.
.pipe(gulp.dest('./assets/css'))
});
/**
* Run stylesheet through PostCSS.
*
* https://www.npmjs.com/package/gulp-postcss
* https://www.npmjs.com/package/gulp-autoprefixer
* https://www.npmjs.com/package/css-mqpacker
*/
gulp.task('postcss', gulp.series('sass', function() {
return gulp.src('assets/css/style.css')
// Wrap tasks in a sourcemap.
.pipe(sourcemaps.init())
// Deal with errors.
.pipe(plumber({ errorHandler: handleErrors }))
// Parse with PostCSS plugins.
.pipe(postcss([
autoprefixer(),
mqpacker({
sort: true
}),
]))
// Create sourcemap.
.pipe(sourcemaps.write())
// Create style.css.
.pipe(gulp.dest('./assets/css'))
}));
/**
* Minify and optimize style.css.
*
* https://www.npmjs.com/package/cssnano
*/
gulp.task('cssnano', gulp.series('postcss', function(done) {
gulp.src('assets/css/style.css')
// Wrap tasks in a sourcemap.
.pipe(sourcemaps.init())
// handle any errors
.pipe(plumber({ errorHandler: handleErrors }))
.pipe(postcss([
cssnano(),
]))
// rename file from style.css to style.min.css
.pipe(rename('style.min.css'))
// Create sourcemap.
.pipe(sourcemaps.write())
.pipe(gulp.dest('./assets/css'));
done();
}));
/**
* Define default Gulp watch task
*/
gulp.task('watch', function() {
browserSync( {
open: false,
injectChanges: true,
proxy: pluginConfig.localURL
});
gulp.watch( './assets/sass/**/*.scss', gulp.series('cssnano') );
gulp.watch( './assets/js/ctct-plugin-admin/**/*.js', gulp.series('scripts') );
gulp.watch( './assets/js/ctct-plugin-gutenberg/**/*.js', gulp.series('scripts') );
gulp.watch( './assets/js/ctct-plugin-frontend/**/*.js', gulp.series('scripts') );
gulp.watch( './assets/js/ctct-plugin-recaptcha/**/*.js', gulp.series('scripts') );
gulp.watch( './assets/js/ctct-plugin-recaptcha-v2/**/*.js', gulp.series('scripts') );
gulp.watch( './assets/sass/**/*.scss' ).on( 'change', browserSync.reload );
gulp.watch( './assets/js/ctct-plugin-admin/**/*.js' ).on( 'change', browserSync.reload );
gulp.watch( './assets/js/ctct-plugin-gutenberg/**/*.js' ).on( 'change', browserSync.reload );
gulp.watch( './assets/js/ctct-plugin-frontend/**/*.js' ).on( 'change', browserSync.reload );
gulp.watch( './assets/js/ctct-plugin-recaptcha/**/*.js').on('change', browserSync.reload );
gulp.watch( './assets/js/ctct-plugin-recaptcha-v2/**/*.js').on('change', browserSync.reload );
});
gulp.task('makepot', function() {
return gulp.src( [ './constant-contact-forms.php', './includes/*.php' ] ).pipe( wpPot({
domain: 'constant-contact-forms',
package: 'Constant Contact Forms'
}) ).pipe( gulp.dest( './languages/constant-contact-forms.pot' ) );
});
/**
* Create individual tasks.
*/
gulp.task('js', gulp.series('scripts'));
gulp.task('styles', gulp.series('clean:styles','cssnano'));
gulp.task('default', gulp.parallel('styles', 'js', 'makepot' ));