11const gulp = require ( 'gulp' ) ;
22const gulpif = require ( 'gulp-if' ) ;
3- const imagemin = require ( 'gulp-imagemin' ) ;
43const log = require ( 'fancy-log' ) ;
54const mozjpeg = require ( 'imagemin-mozjpeg' ) ;
65const newer = require ( 'gulp-newer' ) ;
76const plumber = require ( 'gulp-plumber' ) ;
87const upng = require ( 'gulp-upng' ) ;
98
10- /**
11- * @description Function for optimizing JPEG images
12- * @param {string } input Path to JPEG files
13- * @param {string } output Path to save files
14- * @param {boolean } params.rewriteExisting Switcher for rewriting files
15- * @returns {* } Optimized JPEG images
16- */
9+ async function loadPlugin ( plugin ) {
10+ try {
11+ const module = await import ( plugin ) ;
12+ return module . default || module ;
13+ } catch ( error ) {
14+ log . error ( `Failed to load plugin: ${ plugin } ` ) ;
15+ throw error ;
16+ }
17+ }
18+
19+ async function processImages ( input , output , plugins , params = { } ) {
20+ const imagemin = await loadPlugin ( 'gulp-imagemin' ) ;
1721
18- const optimizeJpg = ( input , output , params = { } ) => {
1922 const rewriteExisting = ! ! (
2023 params . rewriteExisting &&
2124 typeof params . rewriteExisting === 'boolean' &&
2225 params . rewriteExisting === true
2326 ) ;
2427
2528 if ( params . verbose ) {
26- log ( ` 🟡 Start: ${ output } /*.jpg ` ) ;
29+ log ( `🟡🟡🟡 Start: ${ output } ` ) ;
2730 }
2831
2932 gulp
3033 . src ( input )
3134 . pipe ( plumber ( ) )
3235 . pipe ( gulpif ( ! rewriteExisting , newer ( output ) ) )
33- . pipe (
34- imagemin ( [
35- mozjpeg ( {
36- quantTable : 3 ,
37- dcScanOpt : 2 ,
38- } ) ,
39- ] )
40- )
36+ . pipe ( imagemin ( plugins ) )
4137 . pipe ( gulp . dest ( output ) )
4238 . on ( 'end' , ( ) => {
4339 if ( params . verbose ) {
44- log ( ` 🟡 End: ${ output } /*.jpg ` ) ;
40+ log ( `🟡🟡🟡 End: ${ output } ` ) ;
4541 }
4642 params . cb ( ) ;
4743 } ) ;
48- } ;
44+ }
4945
50- /**
51- * @description Function for optimizing PNG images
52- * @param {string } input Path to PNG files
53- * @param {string } output Path to save files
54- * @param {boolean } params.rewriteExisting Switcher for rewriting files
55- * @returns {* } Optimized PNG images
56- */
46+ const optimizeJpg = async ( input , output , params = { } ) => {
47+ const plugins = [
48+ mozjpeg ( {
49+ quantTable : 3 ,
50+ dcScanOpt : 2 ,
51+ } ) ,
52+ ] ;
53+ await processImages ( input , output , plugins , params ) ;
54+ } ;
5755
5856const optimizePng = ( input , output , params = { } ) => {
5957 const rewriteExisting = ! ! (
@@ -80,47 +78,23 @@ const optimizePng = (input, output, params = {}) => {
8078 } ) ;
8179} ;
8280
83- /**
84- * @description Function for optimizing SVG images
85- * @param {string } input Path to SVG files
86- * @param {string } output Path to save files
87- * @param {boolean } params.rewriteExisting Switcher for rewriting files
88- * @returns {* } Optimized SVG images
89- */
90-
91- const optimizeSvg = ( input , output , params = { } ) => {
92- const rewriteExisting = ! ! (
93- params . rewriteExisting &&
94- typeof params . rewriteExisting === 'boolean' &&
95- params . rewriteExisting === true
96- ) ;
97-
98- if ( params . verbose ) {
99- log ( `🟡🟡🟡 Start: ${ output } /*.svg` ) ;
100- }
101- gulp
102- . src ( input )
103- . pipe ( plumber ( ) )
104- . pipe ( gulpif ( ! rewriteExisting , newer ( output ) ) )
105- . pipe (
106- imagemin ( [
107- imagemin . svgo ( {
108- plugins : [
109- {
110- removeViewBox : false ,
111- collapseGroups : true ,
112- } ,
113- ] ,
114- } ) ,
115- ] )
116- )
117- . pipe ( gulp . dest ( output ) )
118- . on ( 'end' , ( ) => {
119- if ( params . verbose ) {
120- log ( `🟡🟡🟡 End: ${ output } /*.svg` ) ;
121- }
122- params . cb ( ) ;
123- } ) ;
81+ const optimizeSvg = async ( input , output , params = { } ) => {
82+ const svgo = await loadPlugin ( 'imagemin-svgo' ) ;
83+ const plugins = [
84+ svgo ( {
85+ plugins : [
86+ {
87+ name : 'removeViewBox' ,
88+ active : false ,
89+ } ,
90+ {
91+ name : 'collapseGroups' ,
92+ active : true ,
93+ } ,
94+ ] ,
95+ } ) ,
96+ ] ;
97+ await processImages ( input , output , plugins , params ) ;
12498} ;
12599
126100module . exports = {
0 commit comments