1
- const path = require ( 'path' ) ;
2
- const log = require ( 'fancy-log' ) ;
3
- const PluginError = require ( 'plugin-error' ) ;
4
- const through = require ( 'through2-concurrent' ) ;
5
- const prettyBytes = require ( 'pretty-bytes' ) ;
6
- const chalk = require ( 'chalk' ) ;
7
- const imagemin = require ( 'imagemin' ) ;
8
- const plur = require ( 'plur' ) ;
1
+ import { createRequire } from 'node:module' ;
2
+ import path from 'node:path' ;
3
+ import process from 'node:process' ;
4
+ import log from 'fancy-log' ;
5
+ import PluginError from 'plugin-error' ;
6
+ import through from 'through2-concurrent' ;
7
+ import prettyBytes from 'pretty-bytes' ;
8
+ import chalk from 'chalk' ;
9
+ import imagemin from 'imagemin' ;
10
+ import plur from 'plur' ;
11
+
12
+ const require = createRequire ( import . meta. url ) ;
9
13
10
14
const PLUGIN_NAME = 'gulp-imagemin' ;
11
15
const defaultPlugins = [ 'gifsicle' , 'mozjpeg' , 'optipng' , 'svgo' ] ;
12
16
13
17
const loadPlugin = ( plugin , ...args ) => {
14
18
try {
15
19
return require ( `imagemin-${ plugin } ` ) ( ...args ) ;
16
- } catch ( _ ) {
17
- log ( `${ PLUGIN_NAME } : Couldn't load default plugin " ${ plugin } " ` ) ;
20
+ } catch {
21
+ log ( `${ PLUGIN_NAME } : Could not load default plugin \` ${ plugin } \` ` ) ;
18
22
}
19
23
} ;
20
24
21
25
const exposePlugin = plugin => ( ...args ) => loadPlugin ( plugin , ...args ) ;
22
26
23
- const getDefaultPlugins = ( ) =>
24
- defaultPlugins . reduce ( ( plugins , plugin ) => {
25
- const instance = loadPlugin ( plugin ) ;
27
+ const getDefaultPlugins = ( ) => defaultPlugins . flatMap ( plugin => loadPlugin ( plugin ) ) ;
26
28
27
- if ( ! instance ) {
28
- return plugins ;
29
- }
30
-
31
- return plugins . concat ( instance ) ;
32
- } , [ ] ) ;
33
-
34
- module . exports = ( plugins , options ) => {
29
+ export default function gulpImagemin ( plugins , options ) {
35
30
if ( typeof plugins === 'object' && ! Array . isArray ( plugins ) ) {
36
31
options = plugins ;
37
32
plugins = undefined ;
@@ -41,17 +36,17 @@ module.exports = (plugins, options) => {
41
36
// TODO: Remove this when Gulp gets a real logger with levels
42
37
silent : process . argv . includes ( '--silent' ) ,
43
38
verbose : process . argv . includes ( '--verbose' ) ,
44
- ...options
39
+ ...options ,
45
40
} ;
46
41
47
- const validExtensions = [ '.jpg' , '.jpeg' , '.png' , '.gif' , '.svg' ] ;
42
+ const validExtensions = new Set ( [ '.jpg' , '.jpeg' , '.png' , '.gif' , '.svg' ] ) ;
48
43
49
44
let totalBytes = 0 ;
50
45
let totalSavedBytes = 0 ;
51
46
let totalFiles = 0 ;
52
47
53
48
return through . obj ( {
54
- maxConcurrency : 8
49
+ maxConcurrency : 8 ,
55
50
} , ( file , encoding , callback ) => {
56
51
if ( file . isNull ( ) ) {
57
52
callback ( null , file ) ;
@@ -63,7 +58,7 @@ module.exports = (plugins, options) => {
63
58
return ;
64
59
}
65
60
66
- if ( ! validExtensions . includes ( path . extname ( file . path ) . toLowerCase ( ) ) ) {
61
+ if ( ! validExtensions . has ( path . extname ( file . path ) . toLowerCase ( ) ) ) {
67
62
if ( options . verbose ) {
68
63
log ( `${ PLUGIN_NAME } : Skipping unsupported image ${ chalk . blue ( file . relative ) } ` ) ;
69
64
}
@@ -77,14 +72,14 @@ module.exports = (plugins, options) => {
77
72
( async ( ) => {
78
73
try {
79
74
const data = await imagemin . buffer ( file . contents , {
80
- plugins : localPlugins
75
+ plugins : localPlugins ,
81
76
} ) ;
82
77
const originalSize = file . contents . length ;
83
78
const optimizedSize = data . length ;
84
79
const saved = originalSize - optimizedSize ;
85
80
const percent = originalSize > 0 ? ( saved / originalSize ) * 100 : 0 ;
86
- const savedMsg = `saved ${ prettyBytes ( saved ) } - ${ percent . toFixed ( 1 ) . replace ( / \. 0 $ / , '' ) } %` ;
87
- const msg = saved > 0 ? savedMsg : 'already optimized' ;
81
+ const savedMessage = `saved ${ prettyBytes ( saved ) } - ${ percent . toFixed ( 1 ) . replace ( / \. 0 $ / , '' ) } %` ;
82
+ const message = saved > 0 ? savedMessage : 'already optimized' ;
88
83
89
84
if ( saved > 0 ) {
90
85
totalBytes += originalSize ;
@@ -93,7 +88,7 @@ module.exports = (plugins, options) => {
93
88
}
94
89
95
90
if ( options . verbose ) {
96
- log ( `${ PLUGIN_NAME } :` , chalk . green ( '✔ ' ) + file . relative + chalk . gray ( ` (${ msg } )` ) ) ;
91
+ log ( `${ PLUGIN_NAME } :` , chalk . green ( '✔ ' ) + file . relative + chalk . gray ( ` (${ message } )` ) ) ;
97
92
}
98
93
99
94
file . contents = data ;
@@ -105,20 +100,20 @@ module.exports = (plugins, options) => {
105
100
} , callback => {
106
101
if ( ! options . silent ) {
107
102
const percent = totalBytes > 0 ? ( totalSavedBytes / totalBytes ) * 100 : 0 ;
108
- let msg = `Minified ${ totalFiles } ${ plur ( 'image' , totalFiles ) } ` ;
103
+ let message = `Minified ${ totalFiles } ${ plur ( 'image' , totalFiles ) } ` ;
109
104
110
105
if ( totalFiles > 0 ) {
111
- msg += chalk . gray ( ` (saved ${ prettyBytes ( totalSavedBytes ) } - ${ percent . toFixed ( 1 ) . replace ( / \. 0 $ / , '' ) } %)` ) ;
106
+ message += chalk . gray ( ` (saved ${ prettyBytes ( totalSavedBytes ) } - ${ percent . toFixed ( 1 ) . replace ( / \. 0 $ / , '' ) } %)` ) ;
112
107
}
113
108
114
- log ( `${ PLUGIN_NAME } :` , msg ) ;
109
+ log ( `${ PLUGIN_NAME } :` , message ) ;
115
110
}
116
111
117
112
callback ( ) ;
118
113
} ) ;
119
- } ;
114
+ }
120
115
121
- module . exports . gifsicle = exposePlugin ( 'gifsicle' ) ;
122
- module . exports . mozjpeg = exposePlugin ( 'mozjpeg' ) ;
123
- module . exports . optipng = exposePlugin ( 'optipng' ) ;
124
- module . exports . svgo = exposePlugin ( 'svgo' ) ;
116
+ export const gifsicle = exposePlugin ( 'gifsicle' ) ;
117
+ export const mozjpeg = exposePlugin ( 'mozjpeg' ) ;
118
+ export const optipng = exposePlugin ( 'optipng' ) ;
119
+ export const svgo = exposePlugin ( 'svgo' ) ;
0 commit comments