@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 CHANGED
@@ -112,7 +112,7 @@ async function findFile(path, file) {
112
112
  return null
113
113
  }
114
114
 
115
- async function fileExists(path) {
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
- async function accessFile(path) {
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
- return Promise.resolve(routes)
149
- .then(routes => (routes && routes.match(location, req)) || { status: 200, data: null })
150
- .then(data => {
151
- if (!routes || !routes.resolveIndex) return [data, routeTemplate]
152
-
153
- const indexLocation = routes.resolveIndex(location, req)
154
- if (!indexLocation) return [data, routeTemplate]
155
-
156
- const indexPath = resolve(target, publicPathDir, indexLocation, indexWithRouting)
157
- return [data, envRequire(indexPath)]
158
- })
159
- .then(([{ status, headers, data }, template]) =>
160
- Promise.resolve(template({ location, data })).then(html => [status, headers, html])
161
- )
162
- .then(([ status, headers, html ]) => res.status(status).set(headers).send(html))
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.DOCUMENT_FRAGMENT_NODE },
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
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.150",
2
+ "version": "0.0.152-beta.1",
3
3
  "name": "@kaliber/build",
4
4
  "description": "Zero configuration, opinionated webpack / react build setup",
5
5
  "scripts": {