forked from gambitph/Stackable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
393 lines (353 loc) · 14.6 KB
/
gulpfile.js
File metadata and controls
393 lines (353 loc) · 14.6 KB
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/**
* These tasks are for building styles and packaging the plugin.
*
* IMPORTANT: DO NOT CALL GULP DIRECTLY. Use `npm run start` or `npm run build` instead.
*/
const autoprefixer = require( 'autoprefixer' ),
collect = require( 'gulp-collect-pattern' ),
concat = require( 'gulp-concat' ),
cssnano = require( 'cssnano' ),
footer = require( 'gulp-footer' ),
fs = require( 'fs' ),
glob = require( 'glob' ),
gulp = require( 'gulp' ),
header = require( 'gulp-header' ),
mqpacker = require( 'css-mqpacker' ),
path = require( 'path' ),
postcss = require( 'gulp-postcss' ),
removeDuplicateLines = require( 'gulp-remove-duplicate-lines' ),
rename = require( 'gulp-rename' ),
replace = require( 'gulp-replace' ),
sass = require( 'gulp-sass' )( require( 'sass' ) ),
zip = require( 'gulp-zip' )
// These files are the ones which will be included in the `package` task.
const buildInclude = [
path.resolve( __dirname, './*.+(txt|php)' ), // All files in the root.
path.resolve( __dirname, './src/**/*.php' ), // Only PHP files in our source files, others will be compiled into dist.
path.resolve( __dirname, './src/**/block.json' ), // Allow block metadata files.
path.resolve( __dirname, './dist/**' ),
path.resolve( __dirname, './freemius/**' ),
path.resolve( __dirname, './languages/**' ),
path.resolve( __dirname, './images/**' ),
path.resolve( __dirname, './src/welcome/images/**' ), // Welcome screen / settings images.
'!' + path.resolve( __dirname, './dist/videos/**' ), // Help tooltip videos.
'!' + path.resolve( __dirname, './dist/**/*.js.map' ), // JS Map files.
]
const postCSSOptions = [
autoprefixer(),
mqpacker(), // Combine media query rules.
cssnano(), // Minify.
]
const sassOptions = {
includePaths: [
path.resolve( __dirname, './src/' ),
path.resolve( __dirname, './src/styles' ),
],
}
module.exports = {
buildInclude,
postCSSOptions,
sassOptions,
}
// Gets all directories recursively.
const getDirectories = function( dir, filelist ) {
const fs = require( 'fs' )
const files = fs.readdirSync( dir )
filelist = filelist || []
if ( fs.statSync( dir ).isDirectory() ) {
filelist.push( dir )
}
files.forEach( function( file ) {
if ( fs.statSync( path.join( dir, file ) ).isDirectory() ) {
filelist = getDirectories( path.join( dir, file ), filelist )
}
} )
return filelist
}
// Puts an index.php for all directories.
gulp.task( 'generate-indexphp', function() {
const fs = require( 'fs' )
const dirs = getDirectories( path.resolve( __dirname, 'build/stackable' ) )
let g = gulp.src( path.resolve( __dirname, 'index.php' ) )
dirs.filter( dir => {
// Only do this for those who don't have an index.php yet.
return ! fs.existsSync( path.resolve( dir, 'index.php' ) )
} ).forEach( dir => {
g = g.pipe( gulp.dest( dir ) )
} )
return g
} )
gulp.task( 'generate-translations-js', gulp.series(
// The collect function has an issue where it will not continue if the
// folder will it writes to doesn't exist, create it to prevent an error.
function translationsEnsureDistDir( cb ) {
if ( ! fs.existsSync( path.resolve( __dirname, './dist' ) ) ) {
fs.mkdirSync( path.resolve( __dirname, './dist' ) )
}
cb()
},
function gatherJSGetTextFuncs() {
return gulp.src( [
// Premium files aren't included in the json translation files, add
// them.
path.resolve( __dirname, './pro__premium_only/src/**/*.js' ),
'!' + path.resolve( __dirname, './**/__test__/*.js' ),
'!' + path.resolve( __dirname, './**/*.test.js' ),
'!' + path.resolve( __dirname, './src/translation-strings.js' ),
] )
// Extract all gettext calls.
.pipe( collect( {
file: path.resolve( __dirname, './dist/translation-strings.js' ),
regex: /(\/\/ translators:(.*)?[\s\n](.*)?)?_.\(\s*['"](.*?)\s?(i18n|STACKABLE_I18N)\s?\)/g,
} ) )
},
function gatherBlockJsonStrings( cb ) {
const strings = []
// Block definitions block.json isn't automatically included in the
// json translation files, we need to add our own translations.
glob.sync( path.resolve( __dirname, './{src,pro__premium_only/src}/**/block.json' ) ).forEach( file => {
// Ignore deprecated blocks.
if ( file.includes( 'deprecated/' ) ) {
return
}
const blockData = JSON.parse( fs.readFileSync( path.resolve( file ), 'utf8' ) )
// Gather all the translatable strings.
if ( blockData.title && ! strings.includes( blockData.title ) ) {
strings.push( blockData.title )
}
if ( blockData.description && ! strings.includes( blockData.description ) ) {
strings.push( blockData.description )
}
( blockData.keywords || [] ).forEach( keyword => {
if ( ! strings.includes( keyword ) ) {
strings.push( keyword )
}
} )
} )
// Append all the strings to the translation-strings.js file.
const s = strings.map( s => `__( '${ s.replace( /'/g, `\'` ) }', i18n )` ).join()
fs.appendFileSync( path.resolve( __dirname, './dist/translation-strings.js' ), `${ s }` )
cb()
},
function cleanupJSTranslationFile() {
return gulp.src( [ path.resolve( __dirname, './dist/translation-strings.js' ) ] )
.pipe( replace( /((i18n|STACKABLE_I18N)\s?\))/g, '$1\n' ) ) // Separate translation into lines.
.pipe( replace( /i18n(\s?\))/g, 'STACKABLE_I18N$1' ) ) // Replace i18n with STACKABLE_I18N.
.pipe( replace( /STACKABLE_I18N/g, '\'stackable-ultimate-gutenberg-blocks\'' ) ) // Replace i18n with STACKABLE_I18N.
// To lessen filesize, remove tabs, and other code like variable
// assignments before the translation function.
.pipe( replace( /^(.*?)(_.\()/gm, '$2' ) )
// Put the translators comment on the end of the translation, since
// we will be removing duplicates, and the comment might get
// jumbled.
.pipe( replace( /(\/\/ translators:(.*?))\n(_.\((.*?);)/g, '$3 $1' ) )
.pipe( removeDuplicateLines( {
include: '^_.', // Only remove duplicate lines on gettext calls.
} ) ) // Remove duplicate lines.
.pipe( header( `/**
* This translation file is automatically generated by gulp generate-translations-js.
*/
// This doesn't run anything, this is just for loading the strings since WP
// won't generate them in the json/js language packs if they're not present in
// the script.
if ( false ) {
` ) )
.pipe( footer( `
}
` ) )
.pipe( gulp.dest( 'dist/' ) )
}
) )
gulp.task( 'generate-translations-php', gulp.series(
// The collect function has an issue where it will not continue if the
// folder will it writes to doesn't exist, create it to prevent an error.
function translationsEnsureDistDir( cb ) {
if ( ! fs.existsSync( './dist' ) ) {
fs.mkdirSync( './dist' )
}
cb()
},
function gatherAllGetTextFuncs() {
return gulp.src( [
path.resolve( __dirname, './src/**/*.js' ),
path.resolve( __dirname, './pro__premium_only/src/**/*.js' ),
'!' + path.resolve( __dirname, './src/test/*.js' ),
'!' + path.resolve( __dirname, './**/__test__/*.js' ),
'!' + path.resolve( __dirname, './src/translation-strings.js' ),
path.resolve( __dirname, './pro__premium_only/*.php' ),
path.resolve( __dirname, './pro__premium_only/src/**/*.php' ),
'!./**/dist/**/*',
] )
// Extract all gettext calls.
.pipe( collect( {
file: 'dist/translation-strings.php',
regex: /(\/\/ translators:(.*)?[\s\n](.*)?)?_.\(\s*['"](.*?)\s?(i18n|STACKABLE_I18N)\s?\)/g,
} ) )
},
function cleanupPHPTranslationFile() {
return gulp.src( [ 'dist/translation-strings.php' ] )
.pipe( replace( /((i18n|STACKABLE_I18N)\s?\))/g, '$1;\n' ) ) // Separate translation into lines.
.pipe( replace( /i18n(\s?\))/g, 'STACKABLE_I18N$1' ) ) // Replace i18n with STACKABLE_I18N.
.pipe( replace( /STACKABLE_I18N/g, '\'stackable-ultimate-gutenberg-blocks\'' ) ) // Replace i18n with STACKABLE_I18N.
// To lessen filesize, remove tabs, and other code like variable
// assignments before the translation function.
.pipe( replace( /^(.*?)(_.\()/gm, '$2' ) )
// Put the translators comment on the end of the translation, since
// we will be removing duplicates, and the comment might get
// jumbled.
.pipe( replace( /(\/\/ translators:(.*?))\n(_.\((.*?);)/g, '$3 $1' ) )
.pipe( removeDuplicateLines( {
include: '^_.', // Only remove duplicate lines on gettext calls.
} ) ) // Remove duplicate lines.
.pipe( header( `<?php
/**
* Auto-generated translation file.
*/
// Exit if accessed
exit;
` ) )
.pipe( gulp.dest( 'dist/' ) )
}
) )
gulp.task( 'style-editor', function() {
return gulp.src( [ path.resolve( __dirname, './src/**/editor.scss' ), '!' + path.resolve( __dirname, './src/deprecated/**/editor.scss' ) ] )
.pipe( sass( sassOptions ).on( 'error', sass.logError ) )
.pipe( concat( 'editor_blocks.css' ) )
// @see https://make.wordpress.org/core/2020/08/04/new-editor-preview-options/
.pipe( header( '#start-resizable-editor-section{display:none}' ) )
.pipe( postcss( postCSSOptions ) )
.pipe( footer( '#end-resizable-editor-section{display:none}' ) )
// Remove the dummy styles added in src/styles/breakpoints.scss which
// are added to force correct sorting of media queries by mqpacker
.pipe( replace( /.z\s?{\s?opacity:\s?1;?}/g, '' ) )
.pipe( gulp.dest( 'dist/' ) )
} )
gulp.task( 'style', gulp.series(
function styleGenerateFrontend() {
return gulp.src( [ path.resolve( __dirname, './src/common.scss' ), path.resolve( __dirname, './src/styles/*.scss' ), path.resolve( __dirname, './src/**/style.scss' ), '!' + path.resolve( __dirname, './src/styles/editor-*.scss' ), '!' + path.resolve( __dirname, './src/deprecated/**/style.scss' ) ] )
.pipe( sass( sassOptions ).on( 'error', sass.logError ) )
.pipe( concat( 'frontend_blocks.css' ) )
// @see https://make.wordpress.org/core/2020/08/04/new-editor-preview-options/
.pipe( header( '#start-resizable-editor-section{display:none}' ) )
.pipe( postcss( postCSSOptions ) )
.pipe( footer( '#end-resizable-editor-section{display:none}' ) )
// Remove the dummy styles added in src/styles/breakpoints.scss
// which are added to force correct sorting of media queries by
// mqpacker
.pipe( replace( /.z\s?{\s?opacity:\s?1;?}/g, '' ) )
.pipe( gulp.dest( 'dist/' ) )
},
function generateResponsiveCSS() {
return gulp.src( [ 'dist/frontend_blocks.css' ] )
// Extract media queries and move them to another file.
.pipe( collect( {
file: 'dist/frontend_blocks_responsive.css',
regex: /@media [\w\s\d]*screen(.*?)\}\}/g,
} ) )
// Remove the media queries from the stylesheet.
.pipe( replace( /@media [\w\s\d]*screen(.*?)\}\}/g, '' ) )
.pipe( gulp.dest( 'dist/' ) )
},
// Cleanup the responsiveness file.
function styleCleanupResponsiveCSS() {
return gulp.src( [ 'dist/frontend_blocks_responsive.css' ] )
.pipe( header( '#start-resizable-editor-section{display:none}' ) )
.pipe( footer( '#end-resizable-editor-section{display:none}' ) )
.pipe( gulp.dest( 'dist/' ) )
},
// Add the responsive styles to the dynamic breakpoint file.
function styleGenerateResponsivePHP() {
const css = fs.readFileSync( 'dist/frontend_blocks_responsive.css', 'utf8' )
return gulp.src( [ path.resolve( __dirname, './src/dynamic-breakpoints.php' ) ] )
.pipe( replace(
/return <<<STK_RESPONSIVE_CSS([\s\S]*?)STK_RESPONSIVE_CSS;/gm,
`return <<<STK_RESPONSIVE_CSS
${ css }
STK_RESPONSIVE_CSS;`
) )
.pipe( gulp.dest( path.resolve( __dirname, './src/' ) ) )
}
) )
gulp.task( 'welcome-styles', function() {
return gulp.src( path.resolve( __dirname, './src/welcome/admin.scss' ) )
.pipe( sass( sassOptions ).on( 'error', sass.logError ) )
.pipe( postcss( postCSSOptions ) )
.pipe( rename( {
basename: 'admin_welcome',
dirname: '',
} ) )
.pipe( gulp.dest( 'dist/' ) )
} )
/*********************************************************************
* START deprecated build styles, we still build these
********************************************************************/
const deprecatedV2SassOptions = {
includePaths: path.resolve( __dirname, './src/deprecated/v2/' ),
}
module.exports.deprecatedV2SassOptions = deprecatedV2SassOptions
gulp.task( 'style-editor-deprecated-v2', function() {
return gulp.src( [ path.resolve( __dirname, './src/deprecated/v2/**/editor.scss' ) ] )
.pipe( sass( deprecatedV2SassOptions ).on( 'error', sass.logError ) )
.pipe( concat( 'editor_blocks_deprecated_v2.css' ) )
// @see https://make.wordpress.org/core/2020/08/04/new-editor-preview-options/
.pipe( header( '#start-resizable-editor-section{display:none}' ) )
.pipe( postcss( postCSSOptions ) )
.pipe( footer( '#end-resizable-editor-section{display:none}' ) )
.pipe( gulp.dest( 'dist/deprecated/' ) )
} )
gulp.task( 'style-deprecated-v2', function() {
return gulp.src( [
path.resolve( __dirname, './src/deprecated/v2/common.scss' ),
path.resolve( __dirname, './src/format-types/highlight/style.scss' ),
path.resolve( __dirname, './src/deprecated/v2/**/style.scss' ),
] )
.pipe( sass( deprecatedV2SassOptions ).on( 'error', sass.logError ) )
.pipe( concat( 'frontend_blocks_deprecated_v2.css' ) )
// @see https://make.wordpress.org/core/2020/08/04/new-editor-preview-options/
.pipe( header( '#start-resizable-editor-section{display:none}' ) )
.pipe( postcss( postCSSOptions ) )
.pipe( footer( '#end-resizable-editor-section{display:none}' ) )
.pipe( gulp.dest( 'dist/deprecated/' ) )
} )
gulp.task( 'style-deprecated-v1', function() {
return gulp.src( [ path.resolve( __dirname, './src/deprecated/v1/*.scss' ) ] )
.pipe( sass( deprecatedV2SassOptions ).on( 'error', sass.logError ) )
.pipe( concat( 'frontend_blocks_deprecated.css' ) )
.pipe( postcss( postCSSOptions ) )
.pipe( gulp.dest( 'dist/deprecated/' ) )
} )
gulp.task( 'style-deprecated', gulp.parallel(
'style-editor-deprecated-v2',
'style-deprecated-v2',
'style-deprecated-v1',
) )
/*********************************************************************
* END deprecated build styles, we still build these
********************************************************************/
gulp.task( 'build-process', gulp.parallel( 'style', 'style-editor', 'welcome-styles', 'style-deprecated', 'generate-translations-js' ) )
gulp.task( 'build', gulp.series( 'build-process' ) )
gulp.task( 'package', function() {
return gulp.src( buildInclude, { base: './' } )
.pipe( gulp.dest( 'build/stackable' ) )
} )
// Zips the build folder.
gulp.task( 'zip', function() {
return gulp.src( 'build/stackable/**/*' )
.pipe( zip( 'stackable.zip' ) )
.pipe( gulp.dest( 'build' ) )
} )
const watchFuncs = ( basePath = '.' ) => {
gulp.watch(
[ `${ basePath }/src/**/*.scss` ],
gulp.parallel( [ 'style', 'style-editor', 'welcome-styles', 'style-deprecated' ] )
)
gulp.watch(
[ `${ basePath }/src/welcome/**/*.scss` ],
gulp.parallel( [ 'welcome-styles' ] )
)
}
gulp.task( 'watch', gulp.series( 'build-process', function watch( done ) {
watchFuncs()
done()
} ) )
module.exports.watchFuncs = watchFuncs