import { InputPluginOption } from "@rollup/browser"; import type { IsomorphicRollupFs } from "../fs-iface"; import { exists } from "../utils/fs-utils"; export type WebFSLoaderOptions = { fs: IsomorphicRollupFs; }; /** * Adds a plugin that handles very last check to see if a file exists and that it can be loaded. * This plugin also provides a web based filesystem that can be used by all other plugins in this package. * This plugin must be declared FIRST in the rollup config. This plugin is ideally injected at runtime by * the IDE. If the desired filesystem does not exist, it will fall back to the available node filesystem. */ export default (opt: WebFSLoaderOptions): InputPluginOption => { const fileExists = exists(opt.fs); return { name: "rollup-in-browser-example", resolveId: { order: "post", async handler(importee, importer) { if (await fileExists(importee)) { return importee; } throw new Error( `Could not resolve import "${importee}" from "${importer}"`, ); }, }, load: { order: "post", handler(id) { return opt.fs.readFile(id, "utf8") as Promise; }, }, }; };