About creators
Creators are, with templates, the central part of an ssgo project. You can see them as page factories. They are basically the entry points of your data fetching and your page building.
In order to be known by ssgo and to be ran, creators must meet two conditions:
- creators must be put inside of the
creators/directory, or nested inside of it. You can have as much creators as you want, even a single one that build all of your pages: that is up to you. - creators must be
.jsor.tsfiles, and must default export a function. This function will be evaluated byssgoand will be given two arguments: thebuildPagefunction, and an object filled with helper functions. This function can beasyncif needed.
The buildPage function
The buildPage function is the function given by ssgo as first parameter of every creator's default exported function.
You can call it as much as needed to create pages.
The type of the buildPage function is the following:
(template: string, data: IContextData, options: IBuildPageOptions) => void
IContextData and IBuildPageOptions are defined inside of https://deno.land/x/ssgo/mod.ts
Here's details about the parameters accepted by buildPage:
template: (string - required) The path of the Template to use to build the page (relative to the root of the templates/ directory). It must be a valid.htmlof.htmfile.data: (object - required) A key/value object that represents the contextual data used inside of your template. Keys will be callable inside of thetemplate(see first parameter ☝️) using the{{ key }}notation. Values can be any valid JavaScript statement (primitives, functions, operators...). If your template doesn't use any data, you can give an empty object{}in place of this parameter.options: (object - required) The options to use to build the page:filename: (string - required) The name (without extension) of the HTML page to create.dir: (string - optional) The sub-directory ofdist/to create the HTML page in, relative to the root of thedist/directory. If the specified directory doesn't exists, it will be created. If this option isn't provided, the page will be built at the root of thedist/directory.
The ssgoBag object
The ssgoBag is an object containing utilities you might need inside of your creators. It is given by ssgo to every creator's default exported function, as second parameter.
At the moment, the ssgoBag exposes 3 utility functions:
watchDir((path: string) => void): Recursively watches the directory given as first parameter and re-run the creator whenever one of the files inside of the directory changes. Thepathgiven must be relative to the root of the project.watchFile((path: string) => void): Same aswatchDirbut for a single file. Whenever the watched file changes, the creator is re-ran. Here again, thepathgiven must be relative to the root of the project.addStaticToBundle((path: string, bundleDest: string, compile?: boolean, override?: boolean) => void): Adds the file given aspathparameter to thedist/directory, inside of thebundleDestsubdirectory. If the file must be put at the root ofdist/, you can give an empty string as thebundleDestparameter.compileis an optional boolean tellingssgoto try to compile the file to add to the bundle. Can be useful if the file is a.tsfile for example.overrideis an optional boolean tellingssgowether it should override the file in the case it already exists in the bundle.
log({ info, success, warning, error }): Logs a message using the internalssgologger.log.erroraccepts an additionalthrowErr: booleanargument, throwing an error if set totrue(default tofalse).context({ mode }):ssgocontext object containing:mode(development | production): Whether ssgo is ran in production (ssgo build) or development mode (ssgo dev)projectRoot(string): The root of the project containing the current creator
This bag of utilities aims to be filled with more content in the future. If you have an idea of something useful that could be added to the ssgoBag, please feel free to fill and issue.
Tips
- Paths given to
watchDir,watchFileandaddStaticToBundleare automatically resolved for the current working directory. - When running
ssgousing the--cwdoption, calling Deno standard library functions (likefs/walkfor example) might not resolve the paths correctly: you might want to prepend your paths with thecontext.projectRootvalue given byssgoBag(second argument given to your creators functions).
Something about this page is wrong? Edit it on GitHub.
