-
Notifications
You must be signed in to change notification settings - Fork 16
/
gulpfile.js
204 lines (173 loc) · 4.71 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
const gulp = require('gulp'),
plumber = require('gulp-plumber'),
rename = require('gulp-rename'),
sass = require('gulp-sass'),
autoprefixer = require('gulp-autoprefixer'),
rtlcss = require('gulp-rtlcss'),
concat = require('gulp-concat'),
cleanCSS = require('gulp-clean-css'),
clone = require('gulp-clone'),
merge = require('merge-stream'),
webpack = require('webpack'),
webpackConfig = require('./webpack.splitted.js');
const config = {
destPath: 'assets/blocks/',
cssOutputStyle: 'expanded', // compressed, expanded,
cssFormatStyle: 0 <= process.argv.indexOf('--minified') ? undefined : 'beautify'
}
const complexBlocks = {
'accordion': 'accordion',
'content-timeline': 'content-timeline',
'media-text-slider': 'media-text-slider',
'tabs': 'tabs',
'toggle': 'toggle',
'template-parts': {
'template-parts/acf': 'acf'
},
};
const rtlConfig = {
plugins: [
{
'name': 'disable processors',
'priority': 50,
'directives': {
'control': {},
'value': []
},
'processors': [
{
'name': 'ignore all',
'expr': /./im,
'action': function (prop, value) {
return { 'prop': prop, 'value': value }
}
}
]
}
],
}
/**
* Create and run task per each file defined inside function
*/
const buildIndependentCSS = (done) => {
const styles = {
'common.style': 'src/style.scss'
};
let tasks = [];
for (const [name, path] of Object.entries(styles)) {
tasks.push(taskDone => {
const baseSource = gulp.src(path)
.pipe(plumber())
.pipe(sass({outputStyle: config.cssOutputStyle}))
.pipe(autoprefixer())
.pipe(rename({
basename: name
}));
const rtl = baseSource
.pipe(clone())
.pipe(rtlcss(rtlConfig))
.pipe(rename({ suffix: '.rtl' }))
merge(baseSource, rtl)
.pipe(cleanCSS({format: config.cssFormatStyle}))
.pipe(gulp.dest(config.destPath));
taskDone();
});
}
return gulp.parallel(...tasks, parallelDone => {
parallelDone();
done();
})();
}
/**
* Compile CSS for all blocks except blocks from complexBlocks var
*/
const buildSimpleBlocksCSS = () => {
const excludeBlocks = Object.keys(complexBlocks).map(path => '!src/blocks/' + path + '/**');
const baseSource = gulp.src(['src/blocks/**/style.scss', ...excludeBlocks])
.pipe(plumber())
.pipe(sass({outputStyle: config.cssOutputStyle}))
.pipe(autoprefixer());
const rtl = baseSource
.pipe(clone())
.pipe(rtlcss(rtlConfig))
.pipe(rename({ suffix: '.rtl' }))
return merge(baseSource, rtl)
.pipe(cleanCSS({format: config.cssFormatStyle}))
.pipe(gulp.dest(config.destPath));
}
/**
* Compile CSS only for blocks defined in complexBlocks var
*/
const buildComplexBlocksCSS = (done) => {
let tasks = [];
buildComplexBlockCSS(tasks, '', complexBlocks);
return gulp.parallel(...tasks, parallelDone => {
parallelDone();
done();
})();
}
/**
* Helper function; runs task for each block in complexBlocks var
*/
const buildComplexBlockCSS = (tasks, block, subBlocks) => {
let excludedBlocks = [];
if (typeof subBlocks === 'object') {
excludedBlocks = Object.keys(subBlocks).map(path => '!src/blocks/' + path + '/**');
for (const [subBlock, value] of Object.entries(subBlocks)) {
buildComplexBlockCSS(tasks, subBlock, value);
}
}
block !== '' && tasks.push(taskDone => {
buildComplexBlockPartCSS(block, excludedBlocks);
taskDone();
})
}
/**
* Helper function; compile single block CSS
*/
const buildComplexBlockPartCSS = (block, excludeBlocks) => {
const baseSource = gulp.src(['src/blocks/' + block + '/**/style.scss', ...excludeBlocks])
.pipe(plumber())
.pipe(sass({outputStyle: config.cssOutputStyle}))
.pipe(autoprefixer())
.pipe(concat('style.css'));
const rtl = baseSource
.pipe(clone())
.pipe(rtlcss(rtlConfig))
.pipe(rename({ suffix: '.rtl' }));
return merge(baseSource, rtl)
.pipe(cleanCSS({ format: config.cssFormatStyle }))
.pipe(gulp.dest(config.destPath + block + '/'));
}
/**
* Runs tasks for compiling all blocks CSS parallel
*/
const buildFrontendCSS = (done) => {
return gulp.parallel(buildSimpleBlocksCSS, buildComplexBlocksCSS, buildIndependentCSS, parallelDone => {
parallelDone();
done();
})();
}
/**
* Runs parallel tasks for .js compiling with webpack and .scss compiling
*/
const watchFrontendAssets = (done) => {
return gulp.parallel(
() => {
return new Promise(resolve => webpack(webpackConfig, (err, stats) => {
if (err) console.log('Webpack', err)
console.log(stats.toString())
resolve()
}))
},
() => {
return gulp.watch('src/**/*.scss', gulp.series(buildFrontendCSS))
},
(parallelDone) => {
parallelDone();
done();
}
)();
}
gulp.task('build-splitted', gulp.parallel(buildFrontendCSS));
gulp.task('watch-splitted', gulp.series(buildFrontendCSS, watchFrontendAssets));