Import / bundle static files (like json or txt) at build time

Is it possible in Motoko with dfx to import a static file and include its content in the wasm output bundle?

For example, I would like to have a .json or .txt file next to my Motoko code, use it and bundle it at build time.

Side note: at build time because I am looking to use these static data in actor canister that are generated on the fly upon calls from my web app

Not at the moment. You’re best bet is to include it as a Text or Blob constant, or write a preprocessor that takes the .txt/.json file and turns it into a Motoko declaration. Something like:

foo.txt:

hello
world

becomes:

Resources.mo

module {
  public let foo = "hello\n world";
}
2 Likes

Thanks for the quick feedback. I’ll probably follow the preprocessor way then.

I wrote a NodeJS script regarding this subject which I ultimately might not use but, if it can be useful to someone one day, here you go → Gist

Note: it generates the variable as a Blob / Nat8 array to avoid text conflicts, like double quotes within double quotes

2 Likes