-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Gulpfile.js
201 lines (186 loc) · 5.45 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
// Require our dependencies
const autoprefixer = require('autoprefixer');
const babel = require('gulp-babel');
const browserSync = require('browser-sync').create();
const concat = require('gulp-concat');
const cssnano = require('cssnano');
const eslint = require('gulp-eslint');
const fs = require('fs');
const gulp = require('gulp');
const iife = require('gulp-iife');
const imagemin = require('gulp-imagemin');
const packagejson = JSON.parse(fs.readFileSync('./package.json'));
const mqpacker = require( 'css-mqpacker' );
const plumber = require('gulp-plumber');
const postcss = require('gulp-postcss');
const rename = require('gulp-rename');
const sass = require('gulp-sass')(require('sass'));
const sassGlob = require('gulp-sass-glob');
const sort = require( 'gulp-sort' );
const gulpStylelint = require('gulp-stylelint');
const sourcemaps = require('gulp-sourcemaps');
const uglify = require('gulp-uglify');
const wpPot = require('gulp-wp-pot');
const exec = require('child_process').exec;
// Some config data for our tasks
const config = {
styles: {
admin_src: 'assets/sass/**/*.scss',
lint_dest: 'assets/sass/',
dest: 'assets/css'
},
scripts: {
admin_src: './assets/js/src/*.js',
uglify: [ 'assets/js/*.js', '!assets/js/*.min.js' ],
dest: './assets/js'
},
images: {
docs_src: './docs/assets/img/**/*',
docs_dest: './docs/assets/img/'
},
languages: {
src: [ './**/*.php', '!.git/*', '!.svn/*', '!bin/**/*', '!node_modules/*', '!release/**/*', '!vendor/**/*' ],
dest: './languages/' + packagejson.name + '.pot'
},
changelog: {
src: 'changelog.md',
edit: 'changelog.txt',
dest: '.'
},
browserSync: {
active: false,
localURL: 'mylocalsite.local'
}
};
function adminstyles() {
return gulp.src(config.styles.admin_src)
.pipe(sourcemaps.init()) // Sourcemaps need to init before compilation
.pipe(sassGlob()) // Allow for globbed @import statements in SCSS
.pipe(sass()) // Compile
.on('error', sass.logError) // Error reporting
.pipe(postcss([
mqpacker( {
'sort': true
} ),
cssnano( {
'safe': true // Use safe optimizations.
} ) // Minify
]))
.pipe(sourcemaps.write()) // Write the sourcemap files
.pipe(gulp.dest(config.styles.dest)) // Drop the resulting CSS file in the specified dir
.pipe(browserSync.stream());
}
function sasslint() {
return gulp.src(config.styles.admin_src)
.pipe(gulpStylelint({
fix: true
}))
.pipe(gulp.dest(config.styles.lint_dest));
}
function adminscripts() {
return gulp.src(config.scripts.admin_src)
.pipe(sourcemaps.init())
.pipe(babel({
presets: ['@babel/preset-env']
}))
.pipe(concat(packagejson.name + '-admin.js')) // Concatenate
.pipe(sourcemaps.write())
.pipe(eslint())
.pipe(iife({
useStrict: false,
params: ['$'],
args: ['jQuery']
}))
.pipe(gulp.dest(config.scripts.dest))
.pipe(browserSync.stream());
}
function uglifyscripts() {
return gulp.src(config.scripts.uglify)
.pipe(uglify()) // Minify + compress
.pipe(rename({
suffix: '.min'
}))
//.pipe(sourcemaps.write())
.pipe(gulp.dest(config.scripts.dest))
.pipe(browserSync.stream());
}
// Optimize Images
function images() {
return gulp.src(config.images.docs_src)
.pipe(
imagemin([
imagemin.gifsicle({ interlaced: true }),
imagemin.mozjpeg({ quality: 90, progressive: true }),
imagemin.optipng({ optimizationLevel: 5 }),
imagemin.svgo({
plugins: [
{
removeViewBox: false,
collapseGroups: true
}
]
})
])
)
.pipe(gulp.dest(config.images.docs_dest));
}
// Generates translation file.
function translate() {
return gulp
.src( config.languages.src )
.pipe( wpPot( {
domain: packagejson.name,
package: packagejson.name
} ) )
.pipe( gulp.dest( config.languages.dest ) );
}
// Generates changelog.txt as a copy of changelog.md
function changelog() {
return gulp
.src( config.changelog.src )
.pipe( rename( config.changelog.edit ) )
.pipe( gulp.dest( config.changelog.dest ) );
}
// Generate readme.txt from readme.md
function build_readme(cb) {
exec('php bin/wp-readme.php', function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
cb(err);
});
}
// Injects changes into browser
function browserSyncTask() {
if (config.browserSync.active) {
browserSync.init({
proxy: config.browserSync.localURL
});
}
}
// Reloads browsers that are using browsersync
function browserSyncReload(done) {
browserSync.reload();
done();
}
// Watch directories, and run specific tasks on file changes
function watch() {
gulp.watch(config.styles.admin_src, adminstyles);
gulp.watch(config.scripts.admin_src, adminscripts);
// Reload browsersync when PHP files change, if active
if (config.browserSync.active) {
gulp.watch('./**/*.php', browserSyncReload);
}
}
// define complex gulp tasks
const styles = gulp.series(sasslint, adminstyles);
const scripts = gulp.series(adminscripts, uglifyscripts);
const build = gulp.series(gulp.parallel(styles, scripts, images, translate, changelog, build_readme));
// export tasks
exports.styles = styles;
exports.scripts = scripts;
exports.images = images;
exports.translate = translate;
exports.changelog = changelog;
exports.readme = build_readme;
exports.watch = watch;
exports.default = build;