@@ -19,17 +19,16 @@ const unplugin: UnpluginInstance<Options | undefined, false> = createUnplugin(
1919 enforce : options . enforce ,
2020
2121 resolveId :
22- meta . framework === 'rollup'
22+ meta . framework === 'rollup' || meta . framework === 'rolldown'
2323 ? async function ( this , id , importer , opt ) {
24- if ( ( opt as any ) ?. attributes ?. type === 'text' ) {
25- if ( id . includes ( '?' ) ) {
26- id += '&raw'
27- } else {
28- id += '?raw'
29- }
24+ const attributeType = ( opt as any ) ?. attributes ?. type
25+ if ( attributeType === 'text' ) {
26+ id += `${ id . includes ( '?' ) ? '&' : '?' } raw`
27+ } else if ( attributeType === 'bytes' ) {
28+ id += `${ id . includes ( '?' ) ? '&' : '?' } bytes`
3029 }
31-
3230 if ( ! rawRE . test ( id ) ) return
31+
3332 const file = cleanUrl ( id )
3433 const resolved = await ( this as PluginContext ) . resolve (
3534 file ,
@@ -43,32 +42,39 @@ const unplugin: UnpluginInstance<Options | undefined, false> = createUnplugin(
4342
4443 load : {
4544 filter : { id : { include : rawRE } } ,
46- handler ( id ) {
45+ async handler ( id ) {
4746 const file = cleanUrl ( id )
4847 const context = this . getNativeBuildContext ?.( )
4948 const transform =
5049 context ?. framework === 'esbuild'
5150 ? context . build . esbuild . transform
5251 : undefined
53- return transformRaw (
52+ const contents = await transformRaw (
5453 file ,
5554 transformFilter ,
55+ false ,
5656 options . transform . options ,
5757 transform ,
5858 )
59+ return contents as string
5960 } ,
6061 } ,
6162 esbuild : {
6263 setup ( build ) {
6364 build . onLoad ( { filter : / .* / } , async ( args ) => {
64- if ( args . with . type === 'text' ) {
65+ const isBytes = args . with . type === 'bytes'
66+ if ( args . with . type === 'text' || isBytes ) {
6567 const contents = await transformRaw (
6668 args . path ,
6769 transformFilter ,
70+ isBytes ,
6871 options . transform . options ,
6972 build . esbuild . transform ,
7073 )
71- return { contents, loader : 'js' }
74+ return {
75+ contents,
76+ loader : typeof contents === 'string' ? 'js' : 'binary' ,
77+ }
7278 }
7379 } )
7480 } ,
@@ -79,6 +85,7 @@ const unplugin: UnpluginInstance<Options | undefined, false> = createUnplugin(
7985export default unplugin
8086
8187const rawRE = / [ & ? ] r a w (?: & | $ ) /
88+ // const bytesRE = /[&?]bytes(?:&|$)/
8289const postfixRE = / [ # ? ] .* $ / s
8390function cleanUrl ( url : string ) {
8491 return url . replace ( postfixRE , '' )
@@ -109,16 +116,17 @@ export function guessLoader(id: string): Loader {
109116async function transformRaw (
110117 file : string ,
111118 transformFilter : ( id : string | unknown ) => boolean ,
119+ isBytes : boolean ,
112120 options : TransformOptions ,
113121 transform ?: typeof import ( 'esbuild' ) . transform ,
114122) {
115- let contents = await readFile ( file , 'utf-8 ')
123+ let contents = await readFile ( file , isBytes ? undefined : 'utf8 ')
116124
117- if ( transformFilter ( file ) ) {
125+ if ( ! isBytes && transformFilter ( file ) ) {
118126 transform ||= ( await import ( 'esbuild' ) ) . transform
119127 contents = (
120128 await transform ( contents , { loader : guessLoader ( file ) , ...options } )
121129 ) . code
122130 }
123- return `export default ${ JSON . stringify ( contents ) } `
131+ return isBytes ? contents : `export default ${ JSON . stringify ( contents ) } `
124132}
0 commit comments