support dynamic import() in frontend

Massimo Melina committed Jan 7, 2022 at 14:33 UTC 21647cde9d57788f1165712207dccf39babc3dd8
1 file changed +7 -3
src/serveFrontend.ts
+7 -3
@@ -30,11 +30,15 @@ function serveStaticFrontend() : Koa.Middleware {
30 if (method !== 'GET')
31 return ctx.status = METHOD_NOT_ALLOWED
32 if (path.endsWith('/')) {
33 - const res = await filePromise(BASE + 'index.html')
34 - ctx.body = replaceFrontEndRes(res.toString('utf8'))
33 + ctx.body = replaceFrontEndRes(String(await filePromise(BASE + 'index.html')))
34 ctx.type = 'html'
35 } else {
37 - ctx.body = createReadStream(BASE + path.slice(1))
36 + const fullPath = BASE + path.slice(1)
37 + if (path.includes('static/js')) // webpack
38 + ctx.body = String(await filePromise(fullPath))
39 + .replace(/(return")(static\/)/g, '$1'+FRONTEND_URI.substring(1)+'$2')
40 + else
41 + ctx.body = createReadStream(fullPath)
42 ctx.type = mime.lookup(path) || 'application/octet-stream'
43 }
44 await next()