Run Gulp AFTER build

I’ve got a gulp workflow set up in my app, and so far it’s working pretty well. However, I’d like to be able to use the NPM Critical package in Gulp to add inline CSS rules to my webpages. I looked into some of the existing Middleman plugins for this, but I couldn’t get any of them to work.

The process looks pretty straight forward in Gulp, but it requires having already built HTML pages. Is there a way that I can get Middleman to run a Gulp task after the build has completed and view/manipulate the /build files directly?

AFAIK there is no event emitted when Middleman has finished compiling.
The only thing I can think is to run another task after middleman in terminal.

e.g. middleman build && npm run inline-css

Where inline-css is an NPM script calling a Gulp task which is targeted at the /build directory rather than /source.

I actually just found an after_build method that works perfectly! I’m using that with the Ruby system command to execute the Gulp task:

after_build do
  system './node_modules/gulp/bin/gulp.js critical'
end

(this is in config.rb, by the way)

1 Like

Another alternative is to use a Rakefile for your build process.

Here’s an article of mine that describes the build process for my portfolio site, and you may be able to tweak it to your needs: https://hellojason.net/blog/remove-unused-css-from-middleman-before-deploying/