1 |
|
2 |
|
3 |
|
4 | 'use strict';
|
5 |
|
6 |
|
7 | const gulp = require('gulp');
|
8 | const gm = require('gulp-gm');
|
9 | const gulpif = require('gulp-if');
|
10 | const imagemin = require('gulp-imagemin');
|
11 | const rename = require('gulp-rename');
|
12 | const { terminal } = require('@absolunet/terminal');
|
13 | const flow = require('~/helpers/flow');
|
14 | const paths = require('~/helpers/paths');
|
15 | const toolbox = require('~/helpers/toolbox');
|
16 | const util = require('~/helpers/util');
|
17 |
|
18 |
|
19 | module.exports = () => {
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | flow.createTask('icons-favicon', () => {
|
27 | const sizes = [
|
28 | 16,
|
29 | 24,
|
30 | 32,
|
31 | 48,
|
32 | 64
|
33 | ];
|
34 |
|
35 | return util.assetsProcess(paths.files.iconsFavicon, (stream) => {
|
36 |
|
37 | return stream
|
38 | .pipe(toolbox.plumber())
|
39 |
|
40 | .pipe(gm((gmfile) => {
|
41 | return gmfile
|
42 | .define(`icon:auto-resize=${sizes.join(',')}`)
|
43 | .setFormat('ico')
|
44 | ;
|
45 | }, { imageMagick: true }))
|
46 |
|
47 | .pipe(rename(util.assetsRename()))
|
48 | ;
|
49 | });
|
50 | });
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | flow.createTask('icons-touch', () => {
|
61 | const touchSizes = [
|
62 | 57,
|
63 | 72,
|
64 | 76,
|
65 | 114,
|
66 | 120,
|
67 | 144,
|
68 | 152,
|
69 | 167,
|
70 | 180,
|
71 | 512
|
72 | ];
|
73 |
|
74 | const streams = [];
|
75 |
|
76 |
|
77 | for (const size of touchSizes) {
|
78 |
|
79 |
|
80 | streams.push(
|
81 | util.assetsProcess(paths.files.iconsTouch, (stream) => {
|
82 |
|
83 | return stream
|
84 | .pipe(toolbox.plumber())
|
85 |
|
86 | .pipe(gulpif(size !== 512, gm((gmfile, done) => {
|
87 | gmfile.identify((error, info) => {
|
88 | if (error) {
|
89 | terminal.error(error);
|
90 | }
|
91 | done(null, toolbox.gmOptimization(gmfile.resize(size, size), info));
|
92 | });
|
93 | })))
|
94 |
|
95 | .pipe(rename(util.assetsRename(`touch-${size}`)))
|
96 |
|
97 | .pipe(imagemin())
|
98 | ;
|
99 | })
|
100 | );
|
101 |
|
102 | }
|
103 |
|
104 | return toolbox.mergeStreams(streams);
|
105 | });
|
106 |
|
107 |
|
108 |
|
109 | flow.createTask('icons-icon', () => {
|
110 | const iconSizes = [
|
111 | 64,
|
112 | 96,
|
113 | 192,
|
114 | 195,
|
115 | 196,
|
116 | 228
|
117 | ];
|
118 |
|
119 | const streams = [];
|
120 |
|
121 |
|
122 | for (const size of iconSizes) {
|
123 |
|
124 |
|
125 | streams.push(
|
126 | util.assetsProcess(paths.files.iconsIcon, (stream) => {
|
127 |
|
128 | return stream
|
129 | .pipe(toolbox.plumber())
|
130 |
|
131 | .pipe(gulpif(size !== 256, gm((gmfile, done) => {
|
132 | gmfile.identify((error, info) => {
|
133 | if (error) {
|
134 | terminal.error(error);
|
135 | }
|
136 | done(null, toolbox.gmOptimization(gmfile.resize(size, size), info));
|
137 | });
|
138 | })))
|
139 |
|
140 | .pipe(rename(util.assetsRename(`icon-${size}`)))
|
141 |
|
142 | .pipe(imagemin())
|
143 | ;
|
144 | })
|
145 | );
|
146 |
|
147 | }
|
148 |
|
149 | return toolbox.mergeStreams(streams);
|
150 | });
|
151 |
|
152 |
|
153 |
|
154 | flow.createTask('icons-large', () => {
|
155 | return util.assetsProcess(paths.files.iconsLarge, (stream) => {
|
156 | return stream
|
157 | .pipe(toolbox.plumber())
|
158 | .pipe(rename(util.assetsRename('large')))
|
159 | .pipe(imagemin())
|
160 | ;
|
161 | });
|
162 | });
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | flow.createTask('icons-tile', () => {
|
169 | const sizes = {
|
170 | small: [128, 128],
|
171 | medium: [270, 270],
|
172 | large: [558, 558],
|
173 | wide: [558, 270]
|
174 | };
|
175 |
|
176 | const streams = [];
|
177 |
|
178 |
|
179 | for (const name of Object.keys(sizes)) {
|
180 | const size = sizes[name];
|
181 |
|
182 |
|
183 | streams.push(
|
184 | util.assetsProcess(paths.files.iconsTile, (stream) => {
|
185 |
|
186 | return stream
|
187 | .pipe(toolbox.plumber())
|
188 |
|
189 | .pipe(gm((gmfile, done) => {
|
190 | gmfile.identify((error, info) => {
|
191 | if (error) {
|
192 | terminal.error(error);
|
193 | }
|
194 |
|
195 | const file = toolbox.gmOptimization(gmfile.resize(size[0], size[1]), info);
|
196 |
|
197 | if (name === 'wide') {
|
198 | file.background('transparent').gravity('Center').extent(size[0], size[1]);
|
199 | }
|
200 |
|
201 | done(null, file);
|
202 | });
|
203 | }))
|
204 |
|
205 | .pipe(rename(util.assetsRename(`tile-${name}`)))
|
206 |
|
207 | .pipe(imagemin())
|
208 | ;
|
209 | })
|
210 | );
|
211 |
|
212 | }
|
213 |
|
214 | return toolbox.mergeStreams(streams);
|
215 | });
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 | flow.createSequence('icons', gulp.parallel('icons-favicon', 'icons-touch', 'icons-icon', 'icons-large', 'icons-tile'), {
|
224 | cleanBundle: ({ bundle }) => {
|
225 | return [`${paths.directory.root}/${bundle.output.build}/${paths.build.icons}`];
|
226 | }
|
227 | });
|
228 |
|
229 | };
|