Skip to content

Instantly share code, notes, and snippets.

@webstrand
Last active January 25, 2022 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save webstrand/3c3454dec9dc4bf68a3e67c7eb9f0f22 to your computer and use it in GitHub Desktop.
Save webstrand/3c3454dec9dc4bf68a3e67c7eb9f0f22 to your computer and use it in GitHub Desktop.
import { fileURLToPath } from "url";
export async function load(url, context, defaultLoad) {
if(/\.yaml$/.test(url)) {
const path = fileURLToPath(new URL(url));
return {
format: "module",
source: `
import fs from "fs/promises";
import YAML from 'yaml';
const yamlSource = await fs.readFile(${JSON.stringify(path)}, "utf8");
export default YAML.parse(yamlSource);
`
}
}
// Defer to Node.js for all other URLs.
return defaultLoad(url, context, defaultLoad);
}
{
"name": "yaml-loader",
"exports": {
".": "./a-yaml-loader.mjs"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment