1
1
import type { BunPlugin , OnLoadResult } from 'bun'
2
2
import type { Plugin } from 'vite'
3
3
import type { OptimizeResult , ProcessOptions } from './types'
4
- import { Buffer } from 'node:buffer'
5
4
import process from 'node:process'
6
5
import { process as processImage } from './core'
7
6
import { debugLog } from './utils'
@@ -17,7 +16,7 @@ export function viteImgxPlugin(options: ImgxPluginOptions = {}): Plugin {
17
16
const {
18
17
include = [ '**/*.{jpg,jpeg,png,webp,avif,svg}' ] ,
19
18
exclude = [ 'node_modules/**' ] ,
20
- disabled = process . env ?. NODE_ENV === 'development' ,
19
+ disabled = typeof process !== 'undefined' && process . env ?. NODE_ENV === 'development' ,
21
20
...processOptions
22
21
} = options
23
22
@@ -31,35 +30,38 @@ export function viteImgxPlugin(options: ImgxPluginOptions = {}): Plugin {
31
30
return {
32
31
name : 'vite-plugin-imgx' ,
33
32
apply : 'build' ,
34
- async transform ( code , id ) {
35
- if ( ! id . match ( / \. ( j p g | j p e g | p n g | w e b p | a v i f | s v g ) $ / i) )
36
- return null
37
-
38
- // Check include/exclude patterns
39
- const shouldInclude = include . some ( pattern => id . match ( new RegExp ( pattern ) ) )
40
- const shouldExclude = exclude . some ( pattern => id . match ( new RegExp ( pattern ) ) )
41
-
42
- if ( ! shouldInclude || shouldExclude )
33
+ async transform ( code : string , id : string ) {
34
+ if ( ! id . match ( / \. ( j p e ? g | p n g | w e b p | a v i f | s v g ) $ / ) )
43
35
return null
44
36
45
37
try {
46
- debugLog ( 'vite' , `Processing ${ id } ` )
38
+ // Filter files based on include/exclude patterns
39
+ const matchesInclude = include . some ( pattern => id . match ( new RegExp ( pattern . replace ( / \* \* / g, '.*' ) . replace ( / \* / g, '[^/]*' ) ) ) )
40
+ const matchesExclude = exclude . some ( pattern => id . match ( new RegExp ( pattern . replace ( / \* \* / g, '.*' ) . replace ( / \* / g, '[^/]*' ) ) ) )
41
+
42
+ if ( ! matchesInclude || matchesExclude ) {
43
+ return null
44
+ }
47
45
48
46
const result = await processImage ( {
49
47
...processOptions ,
50
48
input : id ,
51
49
} )
52
50
53
- return {
54
- code : `export default ${ JSON . stringify ( result . outputPath ) } ` ,
55
- map : null ,
51
+ debugLog ( 'vite-plugin' , `Processed ${ id } : saved ${ result . saved } bytes (${ result . savedPercentage . toFixed ( 2 ) } %)` )
52
+
53
+ if ( result . saved > 0 ) {
54
+ return {
55
+ code,
56
+ map : null ,
57
+ }
56
58
}
57
59
}
58
- catch ( error : unknown ) {
59
- const errorMessage = error instanceof Error ? error . message : String ( error )
60
- debugLog ( 'error' , `Failed to process ${ id } : ${ errorMessage } ` )
61
- return null
60
+ catch ( error ) {
61
+ console . error ( `[imgx] Error processing ${ id } :` , error )
62
62
}
63
+
64
+ return null
63
65
} ,
64
66
}
65
67
}
@@ -69,7 +71,7 @@ export function bunImgxPlugin(options: ImgxPluginOptions = {}): BunPlugin {
69
71
const {
70
72
include = [ '**/*.{jpg,jpeg,png,webp,avif,svg}' ] ,
71
73
exclude = [ 'node_modules/**' ] ,
72
- disabled = process . env ?. NODE_ENV === 'development' ,
74
+ disabled = false ,
73
75
...processOptions
74
76
} = options
75
77
@@ -83,32 +85,35 @@ export function bunImgxPlugin(options: ImgxPluginOptions = {}): BunPlugin {
83
85
return {
84
86
name : 'bun-plugin-imgx' ,
85
87
setup ( build ) {
86
- build . onLoad ( { filter : / \. ( j p g | j p e g | p n g | w e b p | a v i f | s v g ) $ / i } , async ( args ) => {
87
- const id = args . path
88
-
89
- // Check include/exclude patterns
90
- const shouldInclude = include . some ( pattern => id . match ( new RegExp ( pattern ) ) )
91
- const shouldExclude = exclude . some ( pattern => id . match ( new RegExp ( pattern ) ) )
92
-
93
- if ( ! shouldInclude || shouldExclude )
94
- return null as unknown as OnLoadResult
95
-
88
+ build . onLoad ( { filter : / \. ( j p e ? g | p n g | w e b p | a v i f | s v g ) $ / } , async ( args ) => {
96
89
try {
97
- debugLog ( 'bun' , `Processing ${ id } ` )
90
+ // Filter files based on include/exclude patterns
91
+ const matchesInclude = include . some ( pattern =>
92
+ args . path . match ( new RegExp ( pattern . replace ( / \* \* / g, '.*' ) . replace ( / \* / g, '[^/]*' ) ) ) ,
93
+ )
94
+ const matchesExclude = exclude . some ( pattern =>
95
+ args . path . match ( new RegExp ( pattern . replace ( / \* \* / g, '.*' ) . replace ( / \* / g, '[^/]*' ) ) ) ,
96
+ )
97
+
98
+ if ( ! matchesInclude || matchesExclude ) {
99
+ return null as unknown as OnLoadResult
100
+ }
98
101
99
102
const result = await processImage ( {
100
103
...processOptions ,
101
- input : id ,
104
+ input : args . path ,
102
105
} )
103
106
107
+ debugLog ( 'bun-plugin' , `Processed ${ args . path } : saved ${ result . saved } bytes (${ result . savedPercentage . toFixed ( 2 ) } %)` )
108
+
109
+ // Return the file contents as is - optimization happens in place
104
110
return {
105
- contents : `export default ${ JSON . stringify ( result . outputPath ) } ` ,
106
- loader : 'js ' ,
107
- }
111
+ contents : await Bun . file ( args . path ) . arrayBuffer ( ) ,
112
+ loader : 'file ' ,
113
+ } as OnLoadResult
108
114
}
109
- catch ( error : unknown ) {
110
- const errorMessage = error instanceof Error ? error . message : String ( error )
111
- debugLog ( 'error' , `Failed to process ${ id } : ${ errorMessage } ` )
115
+ catch ( error ) {
116
+ console . error ( `[imgx] Error processing ${ args . path } :` , error )
112
117
return null as unknown as OnLoadResult
113
118
}
114
119
} )
0 commit comments