Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to skip bundling for a faster restart? #32

Closed
rasteiner opened this issue Mar 24, 2015 · 5 comments
Closed

Is there a way to skip bundling for a faster restart? #32

rasteiner opened this issue Mar 24, 2015 · 5 comments

Comments

@rasteiner
Copy link

It would be really nice if the browserify / bundling step could be skipped, for example when the server crashes and forever or pm2 or whatever restarts the app. The bundling can take quite a lot of time, especially when in NODE_ENV=production mode.

I'm talking about simply reusing the latest bundled .js file.

@minicuper
Copy link
Contributor

You need to update your server.js file (if you use generator-derby) to something like this (may contain some misspells, I've not tested it):

var async = require('async');
var derby = require('derby');

var http  = require('http');
var chalk = require('chalk');

var publicDir = process.cwd() + '/public';

derby.run(function(){
  require('coffee-script/register');

  require('./server/config');

  var apps = [
    require('./apps/app')
    // <end of app list> - don't remove this comment
  ];

  var express = require('./server/express');
  var store = require('./server/store')(derby, publicDir);

  var error = require('./server/error');

  express(store, apps, error, publicDir, function(expressApp, upgrade){
    var server = http.createServer(expressApp);

    server.on('upgrade', upgrade);

    function listenServer(){
      server.listen(process.env.PORT, function() {
        console.log('%d listening. Go to: http://localhost:%d/',
            process.pid, process.env.PORT);
      });
    }

    function bundleApp (app, cb) {
      app.writeScripts(store, publicDir, {extensions: ['.coffee']}, function(err){
        if (process.env.MODE === 'save') app.serialize();

        if (err) {
          console.log("Bundle don't created:", chalk.red(app.name), ', error:', err);
        } else {
          console.log('Bundle created:', chalk.blue(app.name));
        }
        cb();
      });
    }

    switch (process.env.MODE) {
      case ('use'): return listenServer();
      case ('save'): return async.each(apps, bundleApp, process.exit.bind(process));
      default: async.each(apps, bundleApp, listenServer);
    } 
  });
});

After that you will be able to start your project three ways:

  1. node server.js (usual way)
  2. MODE=save node server.js (to save bundles and so on)
  3. MODE=use node server.js (to start you project using just created bundles)

@minicuper
Copy link
Contributor

We use MODE=save in the deployment stage (while creating docker image) and MODE=use inside our docker container. Our 16 heavy apps start on 8 second.

@rasteiner
Copy link
Author

Thanks 👍

from 2 minutes to less than 3 seconds

@rasteiner
Copy link
Author

For anyone who wants to try this out on his development machine:
Once you have done a "MODE=save node server", derby creates a "derby-serialized" folder in each app folder. You need to delete those if you want to return to developing.

@minicuper
Copy link
Contributor

Yeah, for the purpose we use npm-script in our package.json:

"clean": "find . -name 'derby-serialized' -type d | xargs /bin/rm -rf"

So, npm run clean does the work for us

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants