1
0

preview.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { spawn } from 'child_process';
  2. import path from 'path';
  3. import { fileURLToPath } from 'url';
  4. const __dirname = path.dirname( fileURLToPath( import.meta.url ) );
  5. const rootDir = path.resolve( __dirname, '../..' );
  6. // Start rollup in watch mode
  7. const rollup = spawn( 'npx', [
  8. 'rollup',
  9. '-c', 'utils/build/rollup.config.js',
  10. '-w',
  11. '-m', 'inline'
  12. ], {
  13. cwd: rootDir,
  14. stdio: [ 'ignore', 'pipe', 'pipe' ],
  15. shell: true
  16. } );
  17. // Start server
  18. const server = spawn( 'node', [ 'utils/server.js', '-p', '8080' ], {
  19. cwd: rootDir,
  20. stdio: [ 'ignore', 'pipe', 'pipe' ],
  21. shell: false
  22. } );
  23. // Prefix output
  24. const prefix = ( name, color ) => {
  25. return ( data ) => {
  26. const lines = data.toString().split( '\n' ).filter( l => l.trim() );
  27. for ( const line of lines ) {
  28. console.log( `${color}[${name}]\x1b[0m ${line}` );
  29. }
  30. };
  31. };
  32. rollup.stdout.on( 'data', prefix( 'ROLLUP', '\x1b[44m\x1b[1m' ) );
  33. rollup.stderr.on( 'data', prefix( 'ROLLUP', '\x1b[44m\x1b[1m' ) );
  34. server.stdout.on( 'data', prefix( 'HTTP', '\x1b[42m\x1b[1m' ) );
  35. server.stderr.on( 'data', prefix( 'HTTP', '\x1b[42m\x1b[1m' ) );
  36. // Handle cleanup
  37. const cleanup = () => {
  38. rollup.kill();
  39. server.kill();
  40. process.exit( 0 );
  41. };
  42. process.on( 'SIGINT', cleanup );
  43. process.on( 'SIGTERM', cleanup );
  44. rollup.on( 'close', cleanup );
  45. server.on( 'close', cleanup );
粤ICP备19079148号