0

I have been battling to create a plugin that can create some HTML files from JSON input.

I plan to parse the JSON to get each page and then create a HTML asset for each, and the hope is for it to be then parsed by vite-plugin-handlebars before outputting to dist

I cannot for the life of me get it to work, I have used this.emitFile to get an asset created, but it doesn't get picked up by the following plugin (handlebars)

"use strict";

import fs from "fs";

export default function ExperimentalPlugin(myOptions = {}) {
  const htmlContent = fs.readFileSync(myOptions.htmlTemplate, "utf-8");

  return {
    name: "poster",
    enforce: "pre",
    apply: "build",
    async buildEnd(error) {
      if (error) throw error;
    },
    buildStart: {
      order: "pre",
      handler(o) {
        for (const page of myOptions.pages) {
          this.emitFile({
            type: "asset",
            name: `index`,
            source: htmlContent,
            fileName: `${page.slug}/index.html`,
          });
        }
      },
    },
  };
}
Phill Duffy
  • 2,805
  • 3
  • 33
  • 45

0 Answers0