-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
84 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import {tryreturn} from "runtime-compat/async"; | ||
import errors from "./errors.js"; | ||
|
||
export default async path => | ||
tryreturn(_ => import(`${path}.js`)) | ||
.orelse(_ => errors.MissingComponent.throw(path.name, path)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import handlebars from "handlebars"; | ||
import runtime from "handlebars/runtime.js"; | ||
|
||
export const compile = { | ||
server(text) { | ||
return `export default ${handlebars.precompile(text)}`; | ||
}, | ||
}; | ||
|
||
export const render = precompiled => runtime.template(precompiled); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import {Response, Status} from "runtime-compat/http"; | ||
import {filter} from "runtime-compat/object"; | ||
import {compile, peers, load} from "../common/exports.js"; | ||
|
||
const handler = ({directory, render}) => | ||
(name, props = {}, {status = Status.OK, page} = {}) => async app => { | ||
const components = app.runpath(app.config.location.server, directory); | ||
const {default : component} = await load(components.join(name)); | ||
|
||
const body = render(component)(props); | ||
|
||
const headers = await app.headers(); | ||
|
||
return new Response(await app.render({body, page}), {status, headers}); | ||
}; | ||
|
||
const name = "handlebars"; | ||
const dependencies = ["handlebars"]; | ||
const default_extension = "hbs"; | ||
const on = filter(peers, ([key]) => dependencies.includes(key)); | ||
|
||
export default ({ | ||
directory, | ||
extension = default_extension, | ||
} = {}) => { | ||
const rootname = name; | ||
let imports = {}; | ||
|
||
return { | ||
name: `primate:${name}`, | ||
async init(app, next) { | ||
await app.depend(on, `frontend:${name}`); | ||
|
||
imports = await import("./imports.js"); | ||
|
||
return next(app); | ||
}, | ||
register(app, next) { | ||
const {config} = app; | ||
|
||
app.register(extension, handler({ | ||
directory: directory ?? config.location.components, | ||
render: imports.render, | ||
})); | ||
|
||
return next(app); | ||
}, | ||
async compile(app, next) { | ||
await compile({ | ||
app, | ||
directory: directory ?? app.config.location.components, | ||
extension, | ||
rootname, | ||
compile: imports.compile.server, | ||
}); | ||
|
||
return next(app); | ||
}, | ||
}; | ||
}; | ||
|