-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.js
More file actions
77 lines (66 loc) · 2.04 KB
/
loader.js
File metadata and controls
77 lines (66 loc) · 2.04 KB
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
import m from "mithril";
import Loadable from "./index";
import router from "./router";
import { express as expressAdapter } from "./adapters";
export default class Loader {
constructor(adapter, options = {}) {
this.adapter = adapter;
this.html = options.html;
this.manifest = options.manifest;
this.createStore = options.createStore;
this.createSession = options.createSession;
this.routes = options.routes || {};
this.route(this.routes);
}
_inject(data, { html, title, meta, body, scripts, state } = {}) {
return data
.replace("<html>", `<html ${html}>`)
.replace(/<title>.*?<\/title>/g, title)
.replace("</head>", `${meta}</head>`)
.replace(
'<div id="root"></div>',
`<div id="root">${body}</div><script>window.__INITIAL_STATE__ = ${state};window.__SERVER_RENDERED__ = { location: window.location.href, date: new Date() };</script>`
)
.replace("</body>", scripts.join("") + "</body>");
}
_extract(chunks = []) {
if (!this.manifest || !chunks || chunks.length < 1) {
return [];
}
return Object.keys(this.manifest)
.filter(asset => chunks.indexOf(asset.replace(".js", "")) > -1)
.map(k => assets[k])
.map(c => `<script type="text/javascript" src="/${c}"></script>`);
}
_session(request, store = null) {
if (this.createSession) {
return this.createSession(request, store);
}
return null;
}
_store(url) {
// Create a store from the current url
if (this.createStore) {
return this.createStore({ title: "test" }, url);
}
return null;
}
route(routes = this.routes) {
this.router = router(this, routes);
}
attributes() {
// TODO: Create a module to handle dynamic head content in mithril
return {
html: "",
title: "",
meta: ""
};
}
middleware() {
return (...params) => {
const ctx = this.adapter(...params);
this.router.lookup(ctx.request, ctx.response);
};
}
}
export const express = options => new Loader(expressAdapter, options);