1+ import { Buffer } from 'node:buffer'
12import { createUnplugin , type UnpluginInstance } from 'unplugin'
23import { createFilter } from 'unplugin-utils'
34import { resolveOptions , type Options } from './core/options'
45import { transformRaw } from './core/transform'
56import type { PluginContext } from 'rollup'
67
78const rawRE = / [ & ? ] r a w (?: & | $ ) /
9+ const bytesRE = / [ & ? ] b y t e s (?: & | $ ) /
810const postfixRE = / [ # ? ] .* $ / s
911function cleanUrl ( url : string ) {
1012 return url . replace ( postfixRE , '' )
@@ -28,8 +30,9 @@ const unplugin: UnpluginInstance<Options | undefined, false> = createUnplugin(
2830 id += `${ id . includes ( '?' ) ? '&' : '?' } raw`
2931 } else if ( attributeType === 'bytes' ) {
3032 id += `${ id . includes ( '?' ) ? '&' : '?' } bytes`
33+ } else if ( ! rawRE . test ( id ) && ! bytesRE . test ( id ) ) {
34+ return
3135 }
32- if ( ! rawRE . test ( id ) ) return
3336
3437 const file = cleanUrl ( id )
3538 const resolved = await ( this as PluginContext ) . resolve (
@@ -44,23 +47,28 @@ const unplugin: UnpluginInstance<Options | undefined, false> = createUnplugin(
4447
4548 load : {
4649 filter : {
47- id : { include : rawRE } ,
50+ id : { include : [ rawRE , bytesRE ] } ,
4851 } ,
4952 async handler ( id ) {
53+ const isBytes = bytesRE . test ( id )
5054 const file = cleanUrl ( id )
5155 const context = this . getNativeBuildContext ?.( )
5256 const transform =
5357 context ?. framework === 'esbuild'
5458 ? context . build . esbuild . transform
5559 : undefined
56- const contents = await transformRaw (
60+ let contents = await transformRaw (
5761 file ,
58- false ,
62+ isBytes ,
5963 transform ,
6064 transformFilter ,
6165 options . transform ? options . transform . options : undefined ,
6266 )
63- return contents as string
67+ if ( Buffer . isBuffer ( contents ) ) {
68+ contents = `export default new Uint8Array([${ contents . join ( ', ' ) } ])`
69+ }
70+
71+ return contents
6472 } ,
6573 } ,
6674
0 commit comments