1
- import { createRequire } from 'node:module' ;
2
1
import path from 'node:path' ;
3
2
import process from 'node:process' ;
4
- import log from 'fancy-log' ;
5
- import PluginError from 'plugin-error' ;
6
- import through from 'through2-concurrent' ;
7
3
import prettyBytes from 'pretty-bytes' ;
8
4
import chalk from 'chalk' ;
9
5
import imagemin from 'imagemin' ;
10
6
import plur from 'plur' ;
11
-
12
- const require = createRequire ( import . meta. url ) ;
7
+ import { gulpPlugin } from 'gulp-plugin-extras' ;
13
8
14
9
const PLUGIN_NAME = 'gulp-imagemin' ;
15
10
const defaultPlugins = [ 'gifsicle' , 'mozjpeg' , 'optipng' , 'svgo' ] ;
16
11
17
- const loadPlugin = ( plugin , ...args ) => {
12
+ const loadPlugin = async ( pluginName , ...arguments_ ) => {
18
13
try {
19
- return require ( `imagemin-${ plugin } ` ) ( ...args ) ;
20
- } catch {
21
- log ( `${ PLUGIN_NAME } : Could not load default plugin \`${ plugin } \`` ) ;
14
+ const { default : plugin } = await import ( `imagemin-${ pluginName } ` ) ;
15
+ return plugin ( ...arguments_ ) ;
16
+ } catch ( error ) {
17
+ console . log ( 'er' , error ) ;
18
+ console . log ( `${ PLUGIN_NAME } : Could not load default plugin \`${ pluginName } \`` ) ;
22
19
}
23
20
} ;
24
21
25
- const exposePlugin = plugin => ( ...args ) => loadPlugin ( plugin , ...args ) ;
22
+ const exposePlugin = async plugin => ( ...arguments_ ) => loadPlugin ( plugin , ...arguments_ ) ;
26
23
27
- const getDefaultPlugins = ( ) => defaultPlugins . flatMap ( plugin => loadPlugin ( plugin ) ) ;
24
+ const getDefaultPlugins = async ( ) => Promise . all ( defaultPlugins . flatMap ( plugin => loadPlugin ( plugin ) ) ) ;
28
25
29
26
export default function gulpImagemin ( plugins , options ) {
30
27
if ( typeof plugins === 'object' && ! Array . isArray ( plugins ) ) {
@@ -45,75 +42,58 @@ export default function gulpImagemin(plugins, options) {
45
42
let totalSavedBytes = 0 ;
46
43
let totalFiles = 0 ;
47
44
48
- return through . obj ( {
49
- maxConcurrency : 8 ,
50
- } , ( file , encoding , callback ) => {
51
- if ( file . isNull ( ) ) {
52
- callback ( null , file ) ;
53
- return ;
45
+ return gulpPlugin ( 'gulp-imagemin' , async file => {
46
+ if ( ! validExtensions . has ( path . extname ( file . path ) . toLowerCase ( ) ) ) {
47
+ if ( options . verbose ) {
48
+ console . log ( `${ PLUGIN_NAME } : Skipping unsupported image ${ chalk . blue ( file . relative ) } ` ) ;
49
+ }
50
+
51
+ return file ;
54
52
}
55
53
56
- if ( file . isStream ( ) ) {
57
- callback ( new PluginError ( PLUGIN_NAME , 'Streaming not supported' ) ) ;
58
- return ;
54
+ if ( Array . isArray ( plugins ) ) {
55
+ plugins = await Promise . all ( plugins ) ;
59
56
}
60
57
61
- if ( ! validExtensions . has ( path . extname ( file . path ) . toLowerCase ( ) ) ) {
62
- if ( options . verbose ) {
63
- log ( `${ PLUGIN_NAME } : Skipping unsupported image ${ chalk . blue ( file . relative ) } ` ) ;
64
- }
58
+ const localPlugins = plugins ?? await getDefaultPlugins ( ) ;
59
+ const data = await imagemin . buffer ( file . contents , { plugins : localPlugins } ) ;
60
+ const originalSize = file . contents . length ;
61
+ const optimizedSize = data . length ;
62
+ const saved = originalSize - optimizedSize ;
63
+ const percent = originalSize > 0 ? ( saved / originalSize ) * 100 : 0 ;
64
+ const savedMessage = `saved ${ prettyBytes ( saved ) } - ${ percent . toFixed ( 1 ) . replace ( / \. 0 $ / , '' ) } %` ;
65
+ const message = saved > 0 ? savedMessage : 'already optimized' ;
66
+
67
+ if ( saved > 0 ) {
68
+ totalBytes += originalSize ;
69
+ totalSavedBytes += saved ;
70
+ totalFiles ++ ;
71
+ }
65
72
66
- callback ( null , file ) ;
67
- return ;
73
+ if ( options . verbose ) {
74
+ console . log ( ` ${ PLUGIN_NAME } :` , chalk . green ( '✔ ' ) + file . relative + chalk . gray ( ` ( ${ message } )` ) ) ;
68
75
}
69
76
70
- const localPlugins = plugins || getDefaultPlugins ( ) ;
71
-
72
- ( async ( ) => {
73
- try {
74
- const data = await imagemin . buffer ( file . contents , {
75
- plugins : localPlugins ,
76
- } ) ;
77
- const originalSize = file . contents . length ;
78
- const optimizedSize = data . length ;
79
- const saved = originalSize - optimizedSize ;
80
- const percent = originalSize > 0 ? ( saved / originalSize ) * 100 : 0 ;
81
- const savedMessage = `saved ${ prettyBytes ( saved ) } - ${ percent . toFixed ( 1 ) . replace ( / \. 0 $ / , '' ) } %` ;
82
- const message = saved > 0 ? savedMessage : 'already optimized' ;
83
-
84
- if ( saved > 0 ) {
85
- totalBytes += originalSize ;
86
- totalSavedBytes += saved ;
87
- totalFiles ++ ;
88
- }
77
+ file . contents = data ;
89
78
90
- if ( options . verbose ) {
91
- log ( `${ PLUGIN_NAME } :` , chalk . green ( '✔ ' ) + file . relative + chalk . gray ( ` (${ message } )` ) ) ;
79
+ return file ;
80
+ } , {
81
+ async * onFinish ( ) { // eslint-disable-line require-yield
82
+ if ( ! options . silent ) {
83
+ const percent = totalBytes > 0 ? ( totalSavedBytes / totalBytes ) * 100 : 0 ;
84
+ let message = `Minified ${ totalFiles } ${ plur ( 'image' , totalFiles ) } ` ;
85
+
86
+ if ( totalFiles > 0 ) {
87
+ message += chalk . gray ( ` (saved ${ prettyBytes ( totalSavedBytes ) } - ${ percent . toFixed ( 1 ) . replace ( / \. 0 $ / , '' ) } %)` ) ;
92
88
}
93
89
94
- file . contents = data ;
95
- callback ( null , file ) ;
96
- } catch ( error ) {
97
- callback ( new PluginError ( PLUGIN_NAME , error , { fileName : file . path } ) ) ;
98
- }
99
- } ) ( ) ;
100
- } , callback => {
101
- if ( ! options . silent ) {
102
- const percent = totalBytes > 0 ? ( totalSavedBytes / totalBytes ) * 100 : 0 ;
103
- let message = `Minified ${ totalFiles } ${ plur ( 'image' , totalFiles ) } ` ;
104
-
105
- if ( totalFiles > 0 ) {
106
- message += chalk . gray ( ` (saved ${ prettyBytes ( totalSavedBytes ) } - ${ percent . toFixed ( 1 ) . replace ( / \. 0 $ / , '' ) } %)` ) ;
90
+ console . log ( `${ PLUGIN_NAME } :` , message ) ;
107
91
}
108
-
109
- log ( `${ PLUGIN_NAME } :` , message ) ;
110
- }
111
-
112
- callback ( ) ;
92
+ } ,
113
93
} ) ;
114
94
}
115
95
116
- export const gifsicle = exposePlugin ( 'gifsicle' ) ;
117
- export const mozjpeg = exposePlugin ( 'mozjpeg' ) ;
118
- export const optipng = exposePlugin ( 'optipng' ) ;
119
- export const svgo = exposePlugin ( 'svgo' ) ;
96
+ export const gifsicle = await exposePlugin ( 'gifsicle' ) ;
97
+ export const mozjpeg = await exposePlugin ( 'mozjpeg' ) ;
98
+ export const optipng = await exposePlugin ( 'optipng' ) ;
99
+ export const svgo = await exposePlugin ( 'svgo' ) ;
0 commit comments