How to stop Middleman removing files in the build directory?

I’m working on a static site that has a large number of image assets. I have a gulp script that reads a directory of source images, and generates different-sized images from the source images. The gulp script writes all the assets into ‘build/photos/’

When I then run Middleman, it immediately removes all the newly-built images – because they have no counterpart in the source directory, and it therefore assumes that they are old/unwanted.

As far as I can tell, the ‘ignore’ command only tells Middleman not to process files in the ‘source’ directory. Even when I add:

ignore 'photos'
ignore 'photos/**/*.jpg'

the files in ‘build/photos’ are deleted.

I know that there is a ‘–no-clean’ command-line argument that stops Middleman deleting unmatched files in the build directory, but what I’m really looking for is something that could apply to just one location in the build tree.

I could, of course, write my images to ‘source/photos’ and have Middleman copy them across, but this wastes large amounts of disk space.

Is there any way to persuade Middleman not to touch certain subdirectories in the ‘build’ directory?

Write them into Source > Build > then remove them from source post build.

You can write the hooks directly into the Config.rb for.

You say you have a directory of images anyway - so unsure why this would take more disk space - simply move that directory in its entirety to your source middleman folder and work from there.

Its also nice to have the real images within development also?

The directory of resized images comes to about 2GB in size (and will continue to grow). It takes the gulp script something like 20 minutes to generate all the images from the high-resolution sources, on a high-end laptop (and will likely take longer on the server that will eventually host the site).

The site is going to be deployed to a VPS that has limited disk space. Rather than simply building it offline and then uploading just the ‘build’ directory to the VPS, I’ll be uploading the ‘source’ directory to the VPS so that I can make changes and rebuild on the VPS. (I won’t always have access to my laptop when I need to update the site).

Regenerating the images for every build isn’t an option, because of the time required. And because of the limited amount of disk space available on the VPS, I’d prefer not to have two copies of the image directory (one in ‘source’, one in ‘build’) if I can avoid it.

Probably what I need to do is some symlink magic so that the source and build directories can share the same set of images. Failing that, I’ll just need to move them around, depending on which ‘mode’ I’m working in. Thanks for the suggestion about hooks; that’s probably the way to go.

Hiya

Just build the latest ones - set a dotenv file that records the last run and then only manipulate images added since that last build. Probably what you want to do on the VPS also.

Yes I use Symlinks all the time with my CodeBlender.net project - just symlink the compiled images into the source. Keeping the high res way our of sight.

This sounds like a problem I’ll be facing at a certain point.
Can you elaborate more on what you did, please?
How a sym link from source to build worked if build is cleaned on build?

I haven’t tested this, but what I was thinking of doing was something like this.

  1. Have the stuff that I want to share with ‘build’ in a subdirectory of ‘source’.
  2. Tell Middleman to ignore that directory during the build process
  3. Have a post-build hook that creates the symlink back to that shared directory inside the ‘build’ directory.

As I say, I haven’t tested this, but this seems as if it should work. It will also be necessary to configure the webserver that’s going to serve the built files to follow symlinks; by default, most webservers don’t, for security reasons.

I also would like to know if there is a way to more granularly control the behaviour of the --no-clean flag. If that is not possible with the current version, I would try this strategy:

  1. Make gulp build the images directly to build/photos/.
  2. For development, use files.watch to make the photos available to middleman server, in a configure :development block.
  3. Before running middleman build or in a configure :build block, move build/photos/ to a temporary folder outside of build/, so Middleman leaves it alone.
  4. In an after_build block, move the folder back.

To make this robust, the script will need to check for the existence of build/photos/, but the basic idea is here.