Import Statement

Enabling Stof import statements.

Stof import statements use the file system library. Although this library is not enabled by default in a WebAssembly context, we can add it ourselves.

See Imports for more information on the Stof import statement.

Text Files

For the Stof import statement, we need the 'fs.read(..)' function. This function is used internally by the loaded formats in the document.

Note that if you are aware of the file to be included, it's probably easier to use the "importString(..)" function after reading the file into a string yourself.

import { Stof } from 'jsr:@formata/stof';

const doc = await Stof.create();

doc.insertLibrary('fs', [
    ['read', (path: string): string => Deno.readTextFileSync(path)]
]);
doc.importString('stof', `
    import 'import.toml'; // Calls fs.read('import.toml') in the background...

    #[main]
    fn main() {
        console.log('Hello, ' + self.name);
    }
`);

doc.run();
> deno run --allow-all example.ts
Hello, Bob Smith

Last updated

Was this helpful?