Skip to content

Commit 7edf5cf

Browse files
committed
feat: support bytes for rollup
1 parent af25296 commit 7edf5cf

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,12 @@ export interface Options {
9898
```ts
9999
import text from './js.js?raw'
100100
import text2 from './jsx.jsx?raw'
101+
import bytes from './png.png?bytes'
101102
import text3 from './ts.ts?raw'
102103

103104
// Import attributes (Rolldown doesn't support this syntax)
104105
import text4 from './with.js' with { type: 'text' }
106+
import bytes2 from './with.png' with { type: 'bytes' }
105107
```
106108

107109
## Sponsors

src/index.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import { Buffer } from 'node:buffer'
12
import { createUnplugin, type UnpluginInstance } from 'unplugin'
23
import { createFilter } from 'unplugin-utils'
34
import { resolveOptions, type Options } from './core/options'
45
import { transformRaw } from './core/transform'
56
import type { PluginContext } from 'rollup'
67

78
const rawRE = /[&?]raw(?:&|$)/
9+
const bytesRE = /[&?]bytes(?:&|$)/
810
const postfixRE = /[#?].*$/s
911
function 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

tests/__snapshots__/basic.test.ts.snap

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ var text3 = "export const msg = /* @__PURE__ */ React.createElement(\\"div\\", n
6565
6666
var text4 = "export const msg = \\"hello with\\";\\n";
6767
68-
console.log(text, text2, text3, text4);
68+
var bytes1 = new Uint8Array([97, 98, 99, 10]);
69+
70+
console.log(text, text2, text3, text4, bytes1);
6971
"
7072
`;
7173
@@ -74,6 +76,7 @@ exports[`vite 1`] = `
7476
const text2 = 'export const msg = "hello js";\\n';
7577
const text3 = 'export const msg = /* @__PURE__ */ React.createElement("div", null, "hello jsx");\\n';
7678
const text4 = 'export const msg = "hello with";\\n';
77-
console.log(text, text2, text3, text4);
79+
const bytes1 = new Uint8Array([97, 98, 99, 10]);
80+
console.log(text, text2, text3, text4, bytes1);
7881
"
7982
`;

tests/basic.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ import text from "./ts.ts?raw"
4040
import text2 from "./js.js?raw"
4141
import text3 from "./jsx.jsx?raw"
4242
import text4 from "./with.js" with { type: "text" }
43-
console.log(text, text2, text3, text4)
43+
import bytes1 from "./abc.txt" with { type: "bytes" }
44+
console.log(text, text2, text3, text4, bytes1)
4445
`
4546
const entryFile = path.resolve(resolveDir, 'main.js')
4647
const rollupPlugin = (code: string): Plugin => ({

0 commit comments

Comments
 (0)