UNPKG

@maravilla-labs/functions

Version:

Maravilla Edge Functions bundler and development tools

70 lines • 2.07 kB
import { watch } from 'chokidar'; import { buildFunctions } from './index.js'; export async function developmentServer(options = {}) { const { functionsDir = 'functions', outputDir = '.maravilla', watch: watchFiles = true, onRebuild, ...buildOptions } = options; console.log('šŸš€ Starting functions development server...'); // Initial build await buildFunctions({ functionsDir, outputDir, ...buildOptions, production: false, }); if (!watchFiles) { return; } // Watch for changes const watcher = watch(functionsDir, { ignored: [ '**/node_modules/**', '**/.git/**', '**/dist/**', '**/.maravilla/**' ], persistent: true, ignoreInitial: true, }); let buildTimeout = null; const rebuild = async () => { if (buildTimeout) { clearTimeout(buildTimeout); } buildTimeout = setTimeout(async () => { console.log('\nšŸ”„ Rebuilding functions...'); try { await buildFunctions({ functionsDir, outputDir, ...buildOptions, production: false, }); if (onRebuild) { onRebuild(); } console.log('āœ… Functions rebuilt successfully'); } catch (error) { console.error('āŒ Failed to rebuild functions:', error); } }, 100); }; watcher .on('add', path => { console.log(` Added: ${path}`); rebuild(); }) .on('change', path => { console.log(` Changed: ${path}`); rebuild(); }) .on('unlink', path => { console.log(` Removed: ${path}`); rebuild(); }); console.log(`šŸ‘€ Watching for changes in ${functionsDir}...`); // Return cleanup function return () => { watcher.close(); }; } //# sourceMappingURL=dev.js.map