@kaliber/build 0.0.151 → 0.0.152-beta.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. package/lib/serve.js +55 -26
  2. package/package.json +1 -1
package/lib/serve.js CHANGED
@@ -69,12 +69,7 @@ app.use((err, req, res, next) => {
69
69
  console.error(err)
70
70
  if (reportError) reportError(err, req)
71
71
 
72
- const response = res.status(500)
73
- if (isProduction) {
74
- findFile(req.path, internalServerError)
75
- .then(file => file ? response.sendFile(file) : next())
76
- .catch(next)
77
- } else response.send(`<pre><title style='display: block;'>${err.stack || err.toString()}</title><pre>`)
72
+ serveInternalServerError(err, { req, res, next })
78
73
  })
79
74
 
80
75
  app.listen(port, () => console.log(`Server listening at port ${port}`))
@@ -84,7 +79,7 @@ async function resolveFile(req, res, next) {
84
79
  const { path } = req
85
80
  /** @type {Array<[string, (file:any) => any]>} */
86
81
  const combinations = [
87
- [indexWithRouting, file => serveIndexWithRouting(req, res, file)],
82
+ [indexWithRouting, file => serveIndexWithRouting(file, { req, res, next })],
88
83
  [notFound, file => res.status(404).sendFile(file)],
89
84
  [index, file => res.status(200).sendFile(file)],
90
85
  ]
@@ -112,7 +107,7 @@ async function findFile(path, file) {
112
107
  return null
113
108
  }
114
109
 
115
- async function fileExists(path) {
110
+ function fileExists(path) {
116
111
  return isProduction
117
112
  ? (!fileExists.cache || fileExists.cache[path] === undefined)
118
113
  ? accessFile(path).then(exists => (addPathToCache(path, exists), exists))
@@ -124,7 +119,7 @@ async function fileExists(path) {
124
119
  }
125
120
  }
126
121
 
127
- async function accessFile(path) {
122
+ function accessFile(path) {
128
123
  return new Promise(resolve => access(path, err => resolve(!err)))
129
124
  }
130
125
 
@@ -138,26 +133,60 @@ function possibleDirectories(path) {
138
133
  return possibleDirectories
139
134
  }
140
135
 
141
- function serveIndexWithRouting(req, res, file) {
136
+ function serveIndexWithRouting(file, { req, res, next }) {
142
137
  const envRequire = isProduction ? require : require('import-fresh')
138
+
143
139
  const routeTemplate = envRequire(file)
144
140
 
145
- const routes = routeTemplate.routes
146
141
  const location = parsePath(req.url)
147
142
 
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))
143
+ const [dataOrPromise, template] = getDataAndRouteTemplate(routeTemplate, { location, req })
144
+
145
+ if (dataOrPromise.then)
146
+ dataOrPromise
147
+ .then(({ status, headers, data }) => {
148
+ const html = renderTemplate(template, location, { data })
149
+ res.status(status).set(headers).send(html)
150
+ })
151
+ .catch(error => serveInternalServerError(error, { req, res, next }))
152
+ else {
153
+ try {
154
+ const { data, status, headers } = dataOrPromise
155
+ const html = renderTemplate(template, location, { data })
156
+ res.status(status).set(headers).send(html)
157
+ } catch (error) {
158
+ serveInternalServerError(error, { req, res, next })
159
+ }
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
183
+ }
184
+
185
+ function serveInternalServerError(error, { res, req, next }) {
186
+ const response = res.status(500)
187
+ if (isProduction) {
188
+ findFile(req.path, internalServerError)
189
+ .then(file => file ? response.sendFile(file) : next())
190
+ .catch(next)
191
+ } else response.send(`<pre><title style='display: block;'>${error.stack || error.toString()}</title><pre>`)
163
192
  }
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.151",
2
+ "version": "0.0.152-beta.2",
3
3
  "name": "@kaliber/build",
4
4
  "description": "Zero configuration, opinionated webpack / react build setup",
5
5
  "scripts": {