-
Notifications
You must be signed in to change notification settings - Fork 13
/
gulpfile.js
351 lines (310 loc) · 11.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
var path = require('path');
var name = path.basename(__dirname);
var basePaths = { // Paths for source and bundled parts of app
src: 'src/', dest: 'assets/', npm: 'node_modules/'
},
gulp = require( 'gulp' ), // Require plugins
es = require( 'event-stream' ),
zip = require('gulp-zip');
gutil = require( 'gulp-util' ),
babel = require('gulp-babel'),
prettier = require('gulp-prettier'),
bourbon = require( 'node-bourbon' ),
path = require( 'relative-path' ),
runSequence = require( 'run-sequence' ),
plugins = require( 'gulp-load-plugins' )({ // Plugins - load gulp-* plugins without direct calls
pattern: [ 'gulp-*', 'gulp.*' ], replaceString: /\bgulp[\-.]/
}),
concat = require('gulp-concat'),
jsImport = require('gulp-js-import'),
// Env - call gulp --prod to go into production mode
sassStyle = 'expanded', // SASS syntax
sourceMap = false, // Wheter to build source maps
isProduction = false, // Mode flag
changeEvent = function( evt ) { // Log
gutil.log( 'File', gutil.colors.cyan( evt.path.replace( new RegExp( '/.*(?=/' + basePaths.src + ')/' ), '' ) ), 'was', gutil.colors.magenta( evt.type ) );
};
if ( true === gutil.env.prod ) {
isProduction = true;
sassStyle = 'compressed';
sourceMap = false;
}
const run = require('gulp-run');
const wpPot = require('gulp-wp-pot');
const po2json = require('gulp-po2json');
//js
gulp.task( 'build-js', function() {
var vendorFiles = [
//basePaths.npm + 'imagesloaded/imagesloaded.pkgd.js'
], appFiles = [ basePaths.src + 'js/front/*' ]; //our own JS files
return gulp.src( vendorFiles.concat( appFiles ) ) //join them
.pipe( plugins.filter( '*.js' ) )//select only .js ones
.pipe( plugins.concat( 'scripts.js' ) )//combine them into bundle.js
.pipe( isProduction ? plugins.uglify() : gutil.noop() ) //minification
.pipe( plugins.size() ) //print size for log
.on( 'error', console.log ) //log
.pipe( gulp.dest( basePaths.dest + 'js' ) ); //write results into file
});
//admin.js
gulp.task( 'build-admin-js', function() {
var vendorFiles = [
// basePaths.npm + 'imagesloaded/imagesloaded.pkgd.js'
], appFiles = [ basePaths.src + 'js/admin/*' ]; //our own JS files
return gulp.src( vendorFiles.concat( appFiles ) ) //join them
.pipe( plugins.filter( '*.js' ) )//select only .js ones
.pipe( plugins.concat( 'admin.js' ) )//combine them into bundle.js
.pipe( isProduction ? plugins.uglify() : gutil.noop() ) //minification
.pipe( plugins.size() ) //print size for log
.on( 'error', console.log ) //log
.pipe( gulp.dest( basePaths.dest + 'js' ) ); //write results into file
});
//js
gulp.task( 'build-blocks-js', function() {
var vendorFiles = [
//basePaths.npm + 'imagesloaded/imagesloaded.pkgd.js'
], appFiles = [ basePaths.src + 'js/blocks/*' ]; //our own JS files
return gulp.src( vendorFiles.concat( appFiles ) ) //join them
.pipe( plugins.filter( '*.js' ) )//select only .js ones
.pipe( plugins.concat( 'blocks.js' ) )//combine them into bundle.js
.pipe( isProduction ? plugins.uglify() : gutil.noop() ) //minification
.pipe( plugins.size() ) //print size for log
.on( 'error', console.log ) //log
.pipe( gulp.dest( basePaths.dest + 'js' ) ); //write results into file
});
gulp.task( 'jsx', function () {
return gulp
.src( './src/jsx/**/*.jsx' )
.pipe(babel({
presets: [
[
"@babel/preset-env", // https://babeljs.io/docs/en/babel-preset-env
{
"targets": {
"chrome": "80",
}
}
],
[
"@babel/preset-react",
{
"pragma": "el", // default pragma is React.createElement (only in classic runtime)
}
]
]
}))
.pipe( prettier({ // Prettier docs https://prettier.io/docs/en/options.html
singleQuote: false,
useTabs: true,
bracketSpacing: true,
printWidth: 40,
//proseWrap: 'never',
}))
.pipe( gulp.dest( './assets/js/' ));
});
// Sass
gulp.task( 'build-css', function() {
// Paths for mdl and bourbon
var paths = require( 'node-bourbon' ).includePaths;
paths.push( basePaths.npm + 'modularscale-sass/stylesheets' );
var vendorFiles = gulp.src('.', {allowEmpty: true}),//gulp.src( [] ), // Components
appFiles = gulp.src( basePaths.src + 'sass/front-main.scss' ) // Main file with @import-s
.pipe( plugins.sass( {
outputStyle: sassStyle, // SASS syntax
indentType: 'tab',
indentWidth: 1,
includePaths: paths // Add bourbon
} ).on( 'error', plugins.sass.logError ) ) // SASS own error log
.pipe( plugins.autoprefixer( { // Aautoprefixer
browsers: [ 'last 4 versions' ], cascade: false
} ) )
.on( 'error', console.log ); // Log
return es.concat( appFiles, vendorFiles ) // Combine vendor CSS files and our files after-SASS
.pipe( plugins.concat( 'style.css' ) ) // Combine into file
.pipe( isProduction ? plugins.cssmin() : gutil.noop() ) // Minification on production
.pipe( plugins.size() ) // Display size
.pipe( gulp.dest( basePaths.dest + 'css' ) ) // Write file
.on( 'error', console.log ); // Log
});
// Sass
gulp.task( 'build-gutenberg-css', function() {
// Paths for mdl and bourbon
var paths = require( 'node-bourbon' ).includePaths;
paths.push( basePaths.npm + 'modularscale-sass/stylesheets' );
var vendorFiles = gulp.src('.', {allowEmpty: true}),//gulp.src( [] ), // Components
appFiles = gulp.src( basePaths.src + 'sass/gutenberg.scss' ) // Main file with @import-s
.pipe( plugins.sass( {
outputStyle: sassStyle, // SASS syntax
indentType: 'tab',
indentWidth: 1,
includePaths: paths // Add bourbon
} ).on( 'error', plugins.sass.logError ) ) // SASS own error log
.pipe( plugins.autoprefixer( { // Aautoprefixer
browsers: [ 'last 4 versions' ], cascade: false
} ) )
.on( 'error', console.log ); // Log
return es.concat( appFiles, vendorFiles ) // Combine vendor CSS files and our files after-SASS
.pipe( plugins.concat( 'gutenberg.css' ) ) // Combine into file
.pipe( isProduction ? plugins.cssmin() : gutil.noop() ) // Minification on production
.pipe( plugins.size() ) // Display size
.pipe( gulp.dest( basePaths.dest + 'css' ) ) // Write file
.on( 'error', console.log ); // Log
});
// Blocks
gulp.task( 'build-blocks-css', function() {
// Paths for mdl and bourbon
var paths = require( 'node-bourbon' ).includePaths;
paths.push( basePaths.npm + 'modularscale-sass/stylesheets' );
var vendorFiles = gulp.src('.', {allowEmpty: true}),//gulp.src( [] ), // Components
appFiles = gulp.src( basePaths.src + 'sass/wp-blocks.scss' ) // Main file with @import-s
.pipe( plugins.sass( {
outputStyle: sassStyle, // SASS syntax
indentType: 'tab',
indentWidth: 1,
includePaths: paths // Add bourbon
} ).on( 'error', plugins.sass.logError ) ) // SASS own error log
.pipe( plugins.autoprefixer( { // Aautoprefixer
browsers: [ 'last 4 versions' ], cascade: false
} ) )
.on( 'error', console.log ); // Log
return es.concat( appFiles, vendorFiles ) // Combine vendor CSS files and our files after-SASS
.pipe( plugins.concat( 'blocks.css' ) ) // Combine into file
.pipe( isProduction ? plugins.cssmin() : gutil.noop() ) // Minification on production
.pipe( plugins.size() ) // Display size
.pipe( gulp.dest( basePaths.dest + 'css' ) ) // Write file
.on( 'error', console.log ); // Log
});
gulp.task( 'build-editor-css', function() {
var paths = require( 'node-bourbon' ).includePaths, vendorFiles = gulp.src('.', {allowEmpty: true});
paths.push( basePaths.npm + 'modularscale-sass/stylesheets' );
var appFiles = gulp.src( basePaths.src + 'sass/editor-main.scss' )
.pipe( plugins.sass( {
outputStyle: sassStyle, //SASS syntas
indentType: 'tab',
indentWidth: 1,
includePaths: paths //add bourbon + mdl
} ).on( 'error', plugins.sass.logError ) )//sass own error log
.pipe( plugins.autoprefixer( { //autoprefixer
browsers: [ 'last 4 versions' ], cascade: false
} ) )
.on( 'error', console.log ); //log
return es.concat( appFiles, vendorFiles ) //combine vendor CSS files and our files after-SASS
.pipe( plugins.concat( 'editor.css' ) ) //combine into file
.pipe( isProduction ? plugins.cssmin() : gutil.noop() ) //minification on production
.pipe( plugins.size() ) //display size
.pipe( gulp.dest( basePaths.dest + 'css' ) ) //write file
.on( 'error', console.log ); //log
});
gulp.task( 'build-admin-css', function() {
var paths = require( 'node-bourbon' ).includePaths,
appFiles = gulp.src( basePaths.src + 'sass/admin-main.scss' )
.pipe( plugins.sass( {
outputStyle: sassStyle, //SASS syntas
indentType: 'tab',
indentWidth: 1,
includePaths: paths //add bourbon + mdl
} ).on( 'error', plugins.sass.logError ) )//sass own error log
.pipe( plugins.autoprefixer( { //autoprefixer
browsers: [ 'last 4 versions' ], cascade: false
} ) )
.on( 'error', console.log ); // Log
return appFiles.pipe( plugins.concat( 'admin.css' ) ) // Combine into file
.pipe( isProduction ? plugins.cssmin() : gutil.noop() ) // Minification on production
.pipe( plugins.size() ) // Display size
.pipe( gulp.dest( basePaths.dest + 'css' ) ) // Write file
.on( 'error', console.log ); // Log
});
// Svg - combine and clear svg assets
gulp.task( 'svg-opt', function() {
var icons = gulp.src( [ basePaths.src + 'svg/icon-*.svg' ] ).pipe( plugins.svgmin( {
plugins: [
{
removeTitle: true,
removeDesc: { removeAny: true },
removeEditorsNSData: true,
removeComments: true
}
]
} ) ) // Minification
.pipe( plugins.cheerio( {
run: function( $ ) { //remove fill from icons
$( '[fill]' ).removeAttr( 'fill' );
$( '[fill-rule]' ).removeAttr( 'fill-rule' );
}, parserOptions: { xmlMode: true }
} ) ), pics = gulp.src( [ basePaths.src + 'svg/pic-*.svg' ] ).pipe( plugins.svgmin( {
plugins: [
{
removeTitle: true,
removeDesc: { removeAny: true },
removeEditorsNSData: true,
removeComments: true
}
]
} ) ); // Minification
// Combine for inline usage
return es.concat( icons, pics ).pipe( plugins.svgstore( { inlineSvg: true } ) )
.pipe( gulp.dest( basePaths.dest + 'svg' ) );
});
gulp.task( 'translate', function () {
return gulp.src( ['**/*.php'] )
.pipe( wpPot( {
domain: 'knd',
package: 'knd'
} ))
//.pipe(gulp.dest('file.pot'));
.pipe( gulp.dest( './lang/knd.pot' ) );
});
gulp.task('po2json', function () {
return gulp.src(['lang/ru_RU.po'])
.pipe(po2json( {
pretty: true,
format: "jed",
fuzzy: true
} ))
.pipe( gulp.dest('lang/') );
});
// Builds
gulp.task( 'full-build-css',
gulp.series( 'build-css', 'build-blocks-css', 'build-gutenberg-css', 'build-editor-css', 'build-admin-css' )
);
gulp.task( 'full-build-js',
gulp.series( 'build-js', 'build-admin-js', 'build-blocks-js' )
);
gulp.task( 'full-build',
gulp.series( 'full-build-css', 'full-build-js', 'svg-opt' )
);
// Watchers
gulp.task( 'watch', () => {
gulp.watch(
[ basePaths.src + 'js/**/*.js' ],
gulp.series( [ 'full-build-js' ] )
);
gulp.watch(
[ basePaths.src + 'sass/*.scss', basePaths.src + 'sass/**/*.scss' ],
gulp.series([ 'full-build-css' ] )
);
});
// Default
gulp.task( 'default', gulp.series( 'full-build', 'watch' ) );
// Archive
gulp.task('zip', function(){
const distFiles = [
'**',
'!src/**',
'!node_modules/**',
'!.gitignore',
'!gulpfile.js',
'!LICENSE.txt',
'!README.md',
'!CHANGELOG.md',
'!package.json',
'!package-lock.json',
'!composer.json',
'!phpcs.xml',
'!phpcs.xml.dist',
'!security.xml',
'!**.zip'
];
return gulp.src( distFiles, { base: '../' } )
.pipe( zip( name + '.zip' ) )
.pipe( gulp.dest( './' ) )
});