jescalan / roots

a toolkit for rapid advanced front-end development
http://roots.netlify.com/
Other
1.45k stars 132 forks source link

Ability to swap/customize server more easily #658

Closed kristianmandrup closed 9 years ago

kristianmandrup commented 9 years ago

I need to use an existing Koa server configured with a bunch of extra features. How do I swap out charge server in roots config?

server.js seems to use connect server

connect = require 'connect'

exports.start = (serve_dir) ->
  port = process.env.PORT or 1111
  app = connect()
  ...
  app.use connect.static(serve_dir)
  app.use connect.logger("dev") if roots.project.conf("debug")
  server = exports.server = http.createServer(app).listen(port)

Easy to swap out for sure :) But could be made even easier...

I see the following in commands/serve.js, that I could also swap out:

var path = require('path'),
    server = require('../server'),
    roots = require('../index');

var _serve = function(){
  server.start(roots.project.rootDir);
};

module.exports = { execute: _serve };

Same for watch command:

roots.server = require('../server');
_.bindAll(roots.print, 'reload');

var _watch = function(args){
  roots.print.compiling();
  if (args.open == false) roots.project.open = false

  // compile once and run the local server when ready
  roots.project.mode = 'dev';
  roots.browserPrinter = new roots.printers.BrowserPrinter(); // @private
  roots.compile_project(roots.project.rootDir, function(){
    roots.print.reload();
    roots.server.start(roots.project.path('public'));
    roots.browserPrinter.start();
  });
jescalan commented 9 years ago

So the issue with swapping out roots' server is that charge includes websocket functionality that is used for the page refresh. And its API for handling the websocket piece would not work with any other random server, unless it purposefully exactly mimicked charge. This means errors would be thrown left and right in watch mode as nonexistant functions were called.

I assume that whatever server you are trying to replace charge with does not ship with websocket functionality for the purpose of using livereload, and even if it does it probably does not exactly match charge's API. If this is indeed the case, you must give up live reload through roots during development. If this is ok with you, you can simply run your server and set it to use /your_roots_project/public as its static root, and manually reload after changes have been compiled. Charge will still run in the background, but takes minimal CPU and you simply can ignore it. If you don't want the page to open automatically on watch, just include server: { no_open: true } in your app.coffee and nothing will open in the browser on watch, you can open it yourself to whatever port your custom server is running on.

Hope this helps!

kristianmandrup commented 9 years ago

Thanks for the info. Makes perfect sense. My setup includes browser sync, so works just fine with live reload. Pretty standard these days... On top of that, I will use Roots.cx to generate Marko templates on the fly, so I need to teach Jade to understand specific Marko and Lasso templates via Pencil :o All good :) super powerful combo!!!