import chokidar from 'chokidar'; import { OrisonServer } from './server'; import { compileTypeScriptFiles } from './build'; import path from 'path'; export function watchPages(server: OrisonServer) { console.log('Starting file watcher...'); const watcher = chokidar.watch('src/pages/**/*.tsx'); watcher.on('change', async filePath => { filePath = filePath.replace(/\\/g, '/'); console.log('Recompiling relevant files...'); const result = await compileTypeScriptFiles(server.getSettings()!, [filePath]); const page = result.pages[0]; const modulePath = require.resolve(path.relative(__dirname, path.join(process.cwd(), '.orison', 'pages', page.serverScriptPath + '_server'))); delete require.cache[modulePath]; server.setModuleDefinition(page.path, require(modulePath).default); if (page.clientScriptPath !== null) { server.setModuleDependencies(page.clientScriptPath, result.pageDependencies[page.clientScriptPath]); } console.log('Compiled.'); }); }