Search code examples
rubyjekylljekyll-extensions

Reload plugin when using `jekyll serve`


I'm developing a Jekyll plugin. When I run the jekyll serve command, site files are regenerated when I change any markdown, html, or plugin files, as expected. The problem I've found is that while markdown/HTML files are regenerated, the plugins themselves are not reloaded. I have to terminate jekyll serve and issue the command again for the plugin changes to go into effect. Is there a way to make it so that the plugins get reloaded automatically when changed?

This is for Jekyll 3.1.2.


Solution

  • Based on the suggestion from @DavidJacquel and the gist I found here, I used Gulp with this gulpfile

    'use strict';
    
    var gulp = require('gulp'),
        express = require('express'),
        spawn = require('child_process').spawn;
    
    var jekyll_file = process.platform === 'win32' ? 'jekyll.bat' : 'jekyll';
    
    gulp.task('jekyll', () => {
      var jekyll = spawn(jekyll_file, ['build', '--incremental']);
    
      var output = '';
      jekyll.stdout.on('data', (t) => { output += t; });
      jekyll.stderr.on('data', (t) => { output += t; });
    
      jekyll.on('exit', (code) => {
        if (code)
          console.log(`Jekyll exited with code: ${code}\n${output}`);
        else
          console.log("Finished Jekyll build");
      });
    });
    
    
    gulp.task('serve', () => {
      var server = express();
      server.use(express.static('_site/'));
      server.listen(4000);
    });
    
    gulp.task('watch', () => {
      gulp.watch(['**/*.html', '**/*.md', '_plugins/*.rb', '!_site/**/*'], ['jekyll']);
    });
    
    gulp.task('default', ['jekyll', 'serve', 'watch']);
    

    to get the desired effect. Also created issue here.