VirtualFS API
Complete API reference for the VirtualFS class.
Constructor
import { VirtualFS } from "browser-metro";
import type { FileMap } from "browser-metro";
const files: FileMap = {
"/index.js": 'console.log("hello");',
"/utils.js": 'module.exports = { add: (a, b) => a + b };',
};
const vfs = new VirtualFS(files);Methods
read(path: string): string | undefined
Returns the file contents, or undefined if the file doesn't exist.
const src = vfs.read("/index.js"); // 'console.log("hello");'
const missing = vfs.read("/nope"); // undefinedwrite(path: string, content: string): void
Creates or overwrites a file.
vfs.write("/newfile.js", "module.exports = 42;");exists(path: string): boolean
Checks if a file exists.
vfs.exists("/index.js"); // true
vfs.exists("/nope"); // falselist(): string[]
Returns all file paths.
vfs.list(); // ["/index.js", "/utils.js"]getEntryFile(): string | null
Returns the first matching entry file from: /index.js, /index.ts, /index.tsx, /index.jsx. Returns null if none found.
toFileMap(): FileMap
Returns a copy of the internal file map.