-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
96 lines (87 loc) · 2.01 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"use strict";
require("svelte/register")({
preserveComments: true,
hydratable: true,
generate: "ssr",
customElement: false
});
{
const hljs = require("highlight.js");
const javascript = require("highlight.js/lib/languages/javascript");
hljs.registerLanguage("text", javascript);
}
const Adapter = require("@frctl/fractal").Adapter;
class SvelteAdapter extends Adapter {
/**
* @param {any} source
* @param {any} [app]
*/
constructor(source, app) {
super(null, source);
this.app = app;
}
/**
* @param {string} path
* @param {any} _str
* @param {object} context
* @param {object} meta
*/
async render(path, _str, context, meta) {
delete require.cache[path];
const component = require(path).default;
const wrapper = require("./wrapper.svelte").default;
const { html, css } = wrapper.render({
app: this.app,
env: meta.env,
component,
context
});
meta.env.heads = meta.env.heads || [];
meta.env.heads.push(css);
return html;
}
/**
* @param {string} path
* @param {string} _str
* @param {object} context
* @param {object} meta
*/
async renderLayout(path, _str, context, meta) {
delete require.cache[path];
const component = require(path).default;
const wrapper = require("./wrapper.svelte").default;
const { html, head, css } = wrapper.render({
app: this.app,
env: meta.env,
component,
context,
html: context.yield
});
return `<!doctype html>
<html>
<head>
${head || ""}
<style>${css.code}${meta.env.heads
.map(e => e.code)
.join("\n")}</style>
</head>
${html}
</html>
`;
}
}
/**
* @param {object?} _config;
*/
module.exports = function(_config) {
_config = _config || {}; // not doing anything with config right now!
return {
/**
* @param {string} source
* @param {object} app
*/
register(source, app) {
return new SvelteAdapter(source, app);
}
};
};