UNPKG

@appsemble/node-utils

Version:

NodeJS utilities used by Appsemble internally.

41 lines 1.81 kB
import { isIP } from 'node:net'; import expressToKoa from 'express-to-koa'; import compose from 'koa-compose'; import webpack from 'webpack'; import webpackDevMiddleware from 'webpack-dev-middleware'; /** * Bypass the dev server for API requests to speed them up. */ const skipRoute = /^\/(api|oauth2\/token)/; export async function frontend(webpackConfigs, argv, serveStudio = false) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore Because the webpack core config isn’t built when building the server, an error is // expected here at build time, but while type checking. const { createAppConfig, createStudioConfig } = await import('@appsemble/webpack-core'); const configApp = createAppConfig({ mode: 'development' }); const configStudio = createStudioConfig({ mode: 'development' }); const configs = [configApp, ...(serveStudio ? [configStudio] : []), ...webpackConfigs]; const compiler = webpack(configs); const devMiddleware = webpackDevMiddleware(compiler, { serverSideRender: true }); // @ts-expect-error outputFileSystem exists despite what the types say. const fs = devMiddleware.context.outputFileSystem; const koaDevMiddleware = expressToKoa(devMiddleware); return compose([ (ctx, next) => { ctx.fs = fs; return next(); }, (ctx, next) => { const { app, hostname } = ctx; if (!skipRoute.test(ctx.path)) { return koaDevMiddleware(ctx, next); } if ((new URL(argv.host).hostname === hostname && app.env !== 'development') || isIP(hostname)) { return next(); } return koaDevMiddleware(ctx, next); }, ]); } //# sourceMappingURL=frontend.js.map