lumeland / lume

🔥 Static site generator for Deno 🦕
https://lume.land
MIT License
1.83k stars 83 forks source link

An option for certain generated artifacts to be moved to a folder other than the SiteOptions.dest (The path of the built destination) #372

Closed dnk8n closed 1 year ago

dnk8n commented 1 year ago

Enter your suggestions in details:

I am integrating Lume with Fresh. In my example Lume is responsible for generating styles.css and html. I manually commit files to static/ used both in Fresh and Lume parts of the site.

Here is a diagram to explain fresh-lume

Fresh uses middleware to route to the Lume generated html like this, routes/_middleware.ts:

import { MiddlewareHandlerContext } from "$fresh/server.ts";

interface State {
  data: string;
}

export async function handler(
  req: Request,
  ctx: MiddlewareHandlerContext<State>,
) {
  const filePath = "_site" + new URL(req.url).pathname + "/index.html";
  let fileSize;
  try {
    fileSize = (await Deno.stat(filePath)).size;
  } catch (e) {
    if (e instanceof Deno.errors.NotFound) {
      return await ctx.next();
    }
    return new Response(null, { status: 500 });
  }
  const body = (await Deno.open(filePath)).readable;
  const resp = new Response(body);
  resp.headers.set("content-length", fileSize.toString());
  resp.headers.set("content-type", "text/html; charset=utf-8");
  return resp;
}

I would prefer to only route HTML this way. It is a fallback, where contents of static/ directory take priority over html files intercepted by this middleware, all other fresh routes follow.

I would like a way for lume to have html destination be _site (or whatever is configured in Site options) and for CSS (using sass and postcss) to be generated and copied directly to the static/ directory.

I think it would be great for the SiteOptions.dest to be used as a default, but for there to be an override option to allow specific generated content to be placed in directories (relative to SiteOptions.cwd maybe)

dnk8n commented 1 year ago

Is there some post-process functionality I am maybe missing? Because then it could be as simple as:

await Deno.rename("_site/styles.css", "static/styles.css");
dnk8n commented 1 year ago

I just found the relevant piece:

site.addEventListener("afterBuild", (event) => {
  Deno.renameSync("_site/styles.css", "static/styles.css");
});

and it appears to be working properly. Apologies. I did go on a bit of a hunt, but failed to make the connection earlier on.

oscarotero commented 1 year ago

Yes, for your use case the afterBuild event, to move manually the generated files is the best choice.