@@ -20,17 +20,13 @@ export default function convertEncoding(options) {
20
20
throw new PluginError ( pluginName , ErrorBindings . missingEncoding ) ;
21
21
}
22
22
const { from = UTF8 , to = UTF8 } = options ;
23
- if ( ! iconv . encodingExists ( from ) ) {
24
- throw new PluginError (
25
- pluginName ,
26
- `${ ErrorBindings . unsupportedEncoding } : ${ from } ` ,
27
- ) ;
28
- }
29
- if ( ! iconv . encodingExists ( to ) ) {
30
- throw new PluginError (
31
- pluginName ,
32
- `${ ErrorBindings . unsupportedEncoding } : ${ to } ` ,
33
- ) ;
23
+ for ( const option of [ from , to ] ) {
24
+ if ( ! iconv . encodingExists ( option ) ) {
25
+ throw new PluginError (
26
+ pluginName ,
27
+ `${ ErrorBindings . unsupportedEncoding } : ${ option } ` ,
28
+ ) ;
29
+ }
34
30
}
35
31
if ( from === to ) {
36
32
console . warn ( `${ pluginName } : ${ ErrorBindings . sameEncoding } ` ) ;
@@ -49,17 +45,25 @@ export default function convertEncoding(options) {
49
45
return file ;
50
46
}
51
47
if ( file . isBuffer ( ) ) {
52
- const decodedContent = iconv . decode (
53
- file . contents ,
54
- from ,
55
- iconvOptions . decode ,
56
- ) ;
57
- file . contents = iconv . encode ( decodedContent , to , iconvOptions . encode ) ;
48
+ try {
49
+ const decodedContent = iconv . decode (
50
+ file . contents ,
51
+ from ,
52
+ iconvOptions . decode ,
53
+ ) ;
54
+ file . contents = iconv . encode ( decodedContent , to , iconvOptions . encode ) ;
55
+ } catch ( err ) {
56
+ throw new PluginError ( pluginName , err ) ;
57
+ }
58
58
}
59
59
if ( file . isStream ( ) ) {
60
- file . contents = file . contents
61
- . pipe ( iconv . decodeStream ( from , iconvOptions . decode ) )
62
- . pipe ( iconv . encodeStream ( to , iconvOptions . encode ) ) ;
60
+ try {
61
+ file . contents = file . contents
62
+ . pipe ( iconv . decodeStream ( from , iconvOptions . decode ) )
63
+ . pipe ( iconv . encodeStream ( to , iconvOptions . encode ) ) ;
64
+ } catch ( err ) {
65
+ throw new PluginError ( pluginName , err ) ;
66
+ }
63
67
}
64
68
return file ;
65
69
} ,
0 commit comments