diff --git a/src/rollup/plugins/raw.ts b/src/rollup/plugins/raw.ts index e32c90ef01..778f4f9015 100644 --- a/src/rollup/plugins/raw.ts +++ b/src/rollup/plugins/raw.ts @@ -1,5 +1,6 @@ import { promises as fsp } from "node:fs"; import { extname } from "pathe"; +import mime from "mime"; import type { Plugin } from "rollup"; export interface RawOptions { @@ -18,15 +19,9 @@ export function raw(opts: RawOptions = {}): Plugin { ".css", ".htm", ".html", - ".json", - ".json5", - ".csv", ...(opts.extensions || []), ]); - // TODO: use ext=>mime - const isBinary = (id) => !extensions.has(extname(id)); - return { name: "raw", resolveId(id) { @@ -79,6 +74,17 @@ export function raw(opts: RawOptions = {}): Plugin { }; } +function isBinary(id: string) { + const idMime = mime.getType(id) || ""; + if (idMime.startsWith("text/")) { + return false; + } + if (/application\/(json|xml|yaml)/.test(idMime)) { + return false; + } + return true; +} + function getHelpers() { const js = String.raw; return js` diff --git a/test/fixture/assets/test.json b/test/fixture/assets/test.json new file mode 100644 index 0000000000..c8c4105eb5 --- /dev/null +++ b/test/fixture/assets/test.json @@ -0,0 +1,3 @@ +{ + "foo": "bar" +} diff --git a/test/fixture/routes/assets/all.ts b/test/fixture/routes/assets/all.ts new file mode 100644 index 0000000000..58b16b9843 --- /dev/null +++ b/test/fixture/routes/assets/all.ts @@ -0,0 +1,25 @@ +export default eventHandler(async (event) => { + const serverAssets = useStorage("assets/server"); + + const keys = await serverAssets.getKeys(); + const items = await Promise.all( + keys.map(async (key) => { + return { + key, + meta: await serverAssets.getMeta(key), + data: await serverAssets.getItem(key).then((r) => + // prettier-ignore + typeof r === "string" ? r.slice(0, 32) : (isPureObject(r) ? r : ``) + ), + }; + }) + ); + + return items; +}); + +function isPureObject(value) { + return ( + value !== null && typeof value === "object" && value.constructor === Object + ); +}