@kaliber/build 0.0.150 → 0.0.152-beta.1
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/serve.js +38 -18
- package/lib/universalComponents.js +11 -1
- package/package.json +1 -1
package/lib/serve.js
CHANGED
@@ -112,7 +112,7 @@ async function findFile(path, file) {
|
|
112
112
|
return null
|
113
113
|
}
|
114
114
|
|
115
|
-
|
115
|
+
function fileExists(path) {
|
116
116
|
return isProduction
|
117
117
|
? (!fileExists.cache || fileExists.cache[path] === undefined)
|
118
118
|
? accessFile(path).then(exists => (addPathToCache(path, exists), exists))
|
@@ -124,7 +124,7 @@ async function fileExists(path) {
|
|
124
124
|
}
|
125
125
|
}
|
126
126
|
|
127
|
-
|
127
|
+
function accessFile(path) {
|
128
128
|
return new Promise(resolve => access(path, err => resolve(!err)))
|
129
129
|
}
|
130
130
|
|
@@ -140,24 +140,44 @@ function possibleDirectories(path) {
|
|
140
140
|
|
141
141
|
function serveIndexWithRouting(req, res, file) {
|
142
142
|
const envRequire = isProduction ? require : require('import-fresh')
|
143
|
+
|
143
144
|
const routeTemplate = envRequire(file)
|
144
145
|
|
145
|
-
const routes = routeTemplate.routes
|
146
146
|
const location = parsePath(req.url)
|
147
147
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
})
|
159
|
-
.
|
160
|
-
|
161
|
-
|
162
|
-
|
148
|
+
const [dataOrPromise, template] = getDataAndRouteTemplate(routeTemplate, { location, req })
|
149
|
+
|
150
|
+
if (dataOrPromise.then)
|
151
|
+
dataOrPromise
|
152
|
+
.then(({ status, headers, data }) => {
|
153
|
+
const html = renderTemplate(template, location, { data })
|
154
|
+
res.status(status).set(headers).send(html)
|
155
|
+
})
|
156
|
+
else {
|
157
|
+
const { data, status, headers } = dataOrPromise
|
158
|
+
const html = renderTemplate(template, location, { data })
|
159
|
+
res.status(status).set(headers).send(html)
|
160
|
+
}
|
161
|
+
}
|
162
|
+
|
163
|
+
function getDataAndRouteTemplate(routeTemplate, { location, req }) {
|
164
|
+
const envRequire = isProduction ? require : require('import-fresh')
|
165
|
+
|
166
|
+
const routes = routeTemplate.routes
|
167
|
+
const dataOrPromise = (routes && routes.match(location, req)) || { status: 200, data: null }
|
168
|
+
|
169
|
+
if (!routes || !routes.resolveIndex)
|
170
|
+
return [dataOrPromise, routeTemplate]
|
171
|
+
|
172
|
+
const indexLocation = routes.resolveIndex(location, req)
|
173
|
+
if (!indexLocation)
|
174
|
+
return [dataOrPromise, routeTemplate]
|
175
|
+
|
176
|
+
const indexPath = resolve(target, publicPathDir, indexLocation, indexWithRouting)
|
177
|
+
return [dataOrPromise, envRequire(indexPath)]
|
178
|
+
}
|
179
|
+
|
180
|
+
function renderTemplate(template, location, { data }) {
|
181
|
+
const html = template({ location, data })
|
182
|
+
return html
|
163
183
|
}
|
@@ -63,9 +63,18 @@ function createVirtualReactContainer({ initialNodes, startNode, endNode }) {
|
|
63
63
|
removeEventListener: (...args) => parent.removeEventListener(...args),
|
64
64
|
dispatchEvent: (...args) => parent.dispatchEvent(...args),
|
65
65
|
get firstChild() { return nodes[0] || null },
|
66
|
-
get nodeType() { return parent.
|
66
|
+
get nodeType() { return parent.ELEMENT_NODE },
|
67
67
|
get ownerDocument() { return parent.ownerDocument },
|
68
68
|
get nodeName() { return 'virtualized container' },
|
69
|
+
get textContent() { return '' },
|
70
|
+
set textContent(value) {
|
71
|
+
if (value) throw new Error(`Unexpected value set by React: "${value}"`)
|
72
|
+
nodes.forEach(node => parent.removeChild(node))
|
73
|
+
nodes.length = 0
|
74
|
+
},
|
75
|
+
get tagName() { return 'DIV' },
|
76
|
+
get namespaceURI() { return parent.namespaceURI },
|
77
|
+
get onclick() { return undefined }, // this is accessed as part of an obscure fix for bubbling behavior in Safari. By returning undefined we disable that 'fix'
|
69
78
|
removeChild(child) {
|
70
79
|
const result = parent.removeChild(child)
|
71
80
|
nodes = nodes.filter(x => x !== child)
|
@@ -83,6 +92,7 @@ function createVirtualReactContainer({ initialNodes, startNode, endNode }) {
|
|
83
92
|
return result
|
84
93
|
},
|
85
94
|
}
|
95
|
+
|
86
96
|
// The statement below is a lie. We supply an object that has all methods that React calls on it
|
87
97
|
return /** @type {Element} */ (container)
|
88
98
|
}
|
package/package.json
CHANGED