Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. const PORT = process.env.PORT || 7000;
  2.  
  3. const Koa = require('koa');
  4. const router = require('koa-router')();
  5. const MemoryFS = require('memory-fs');
  6. const Promise = require('bluebird');
  7. const webpack = require('webpack');
  8.  
  9.  
  10. const app = new Koa();
  11. const fs = new MemoryFS();
  12.  
  13. // webpack compiler. once compiled, client.js
  14. // will be available at fs.readFileSync('/client-bundle.js')
  15. const compiler = webpack({
  16. context: __dirname,
  17. entry: {
  18. client: './client'
  19. },
  20. output: {
  21. filename: '[name]-bundle.js',
  22. path: '/'
  23. }
  24. });
  25. compiler.outputFileSystem = fs;
  26.  
  27. // start compiler.watch()
  28. // p will resolve when the first compile finishes
  29. // we should wait to start the server until p resolves,
  30. // but it's more readable not to
  31. const p = Promise.fromCallback(cb => compiler.watch({}, cb));
  32. p.tap(() => console.log('Client assets compiled'));
  33.  
  34. router.get('/client.js', ctx => {
  35. ctx.type = 'application/javascript; charset=utf-8';
  36. ctx.body = fs.readFileSync('/client-bundle.js');
  37. });
  38.  
  39. app.use(router.routes());
  40. app.listen(PORT);
  41. console.log('Prototype server listening on', PORT);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement