UNPKG

@farmfe/core

Version:

Farm is a extremely fast web build tool written in Rust. Farm can start a project in milliseconds and perform HMR within 10ms, making it much faster than similar tools like webpack and vite.

49 lines 1.9 kB
import fs from 'fs'; import path, { relative } from 'path'; import serve from 'koa-static'; export function staticMiddleware(devServerContext) { const { config } = devServerContext; const staticMiddleware = serve(config.distDir, { // multiple page maybe receive "about", should auto try extension extensions: ['html'] }); // Fallback const fallbackMiddleware = async (ctx, next) => { await next(); // If staticMiddleware doesn't find the file, try to serve index.html if (ctx.status === 404 && !ctx.body) { ctx.type = 'html'; ctx.body = fs.createReadStream(path.join(config.distDir, 'index.html')); } }; return async (ctx, next) => { if (ctx.status !== 404 || ctx.body) { await next(); return; } const requestPath = ctx.request?.path; let modifiedPath = requestPath; if (requestPath) { if (config.output.publicPath.startsWith('/')) { modifiedPath = requestPath.substring(config.output.publicPath.length); } else { const publicPath = relative(path.join(config.distDir, config.output.publicPath), config.distDir); modifiedPath = requestPath.substring(publicPath.length + 1); } } ctx.request.path = `/${modifiedPath}`; try { // Serve middleware for static files await staticMiddleware(ctx, async () => { // If staticMiddleware doesn't find the file or refresh current page router, execute fallbackMiddleware await fallbackMiddleware(ctx, next); }); } catch (error) { devServerContext.logger.error('Static file handling error:', error); ctx.status = 500; } }; } //# sourceMappingURL=static.js.map