Movatterモバイル変換


[0]ホーム

URL:


RSS

Hakyll Pt. 4 – Copying Static Files For Your Build

Info

SummaryThis short hakyll tutorial will show you a simple way to copy static files over to your build folder.
Shared2019-01-27
Revised2023-02-11 @ 16:00 UTC

This is part 4 of a multipart series where we will look at getting a website / blog set up withhakyll and customized a fair bit.

Overview

You will inevitably need to copy static files over to your build folder at some point in a hakyll project, and this short tutorial will show you a simple way to do so.

  1. Copying Files the Long Way
  2. Simplify File Copying With a List
  3. Simplify File Copying With Pattern Composition Operators
  4. GitHub Pages Tip for Dotfiles and Dotfolders

Copying Files the Long Way

As of the time of this writing, thedefault hakyll example for copying files looks like this:

match"images/*"$dorouteidRoutecompilecopyFileCompiler

This is great and gets the job done! When I first looked at copying more files, I went down this path:

match"CNAME"$dorouteidRoutecompilecopyFileCompilermatch"robots.txt"$dorouteidRoutecompilecopyFileCompilermatch"images/*"$dorouteidRoutecompilecopyFileCompilermatch"fonts/*"$dorouteidRoutecompilecopyFileCompiler-- ...and so on

Obviously, there is some code duplication here; there must be a better way!

Simplify File Copying With a List

Here are all the items I need copied over:

CNAMErobots.txt_config.ymlimages/*fonts/*.well-known/*

As it turns out, thislist of file identifiers to copy can be used in conjunction withforM_ to take some foldable structure (for us, a list), map each element to a monadic action that uses hakyll’smatch function, ignore the results and ultimately simplify our code.

The type signature forforM_ is as follows:

forM_::(Foldablet,Monadm)=>ta->(a->mb)->m()

And here is the implementation:

forM_["CNAME","robots.txt","_config.yml","images/*","fonts/*",".well-known/*"]$\f->matchf$dorouteidRoutecompilecopyFileCompiler

Nice! While this technique is not mentioned in the documentation, it is present in thehakyll website’ssite.hs file, so we know we’re in good company ifjaspervdj is already using it.

If you want to read more about the possible patterns that can be matched, check out the commentary in the source here:https://github.com/jaspervdj/hakyll/blob/1abdeee743d65d96c6f469213ca6e7ea823340a7/lib/Hakyll/Core/Identifier/Pattern.hs.

Simplify File Copying With Pattern Composition Operators

In this /r/haskell reddit thread byGAumala, they point out thathakyll’s pattern composition operators can also be used to accomplish the same goal. Here is how we would could convert ourforM_ above to instead use.||.:

match("CNAME".||."favicon.ico".||."robots.txt".||."_config.yml".||."images/*".||."fonts/*".||.".well-known/*")$dorouteidRoutecompilecopyFileCompiler

While I understand theforM_ better, this does seem to be more attractive!

GitHub Pages Tip for Dotfiles and Dotfolders

If you’re using GitHub pages and have any dotfiles or dotfolders to copy over, make sure you pay attention here.

Let’s say you have signed up forBrave Payments and need to verify your site by placing a file at:

https://mysite.com/.well-known/brave-payments-verification.txt

Unfortunately,GitHub Pages, which usesjekyll under the hood, will ignore your dotfiles and dotfolders by default and will therefore not deploy them.

We can fix this by adding a_config.yml file to our project (you can see it included in the list in the previous section) and telling it to include what it is ignoring:

# _config.ymlinclude:[".well-known"]

Once you’ve done this, you can commit this file, push it up to GitHub and view it on your published site.

You can read more about jekyll’s configuration options here:https://jekyllrb.com/docs/configuration/options/.

Wrapping Up

Today we learned a simple way to list what files we want to be copied over in our hakyll projects, got exposed toforM_ and uncovered a potential issue with dotfiles and dotfolders not getting published on GitHub Pages.

Next up:


Thank you for reading!
Robert


[8]ページ先頭

©2009-2025 Movatter.jp