Skip to content

Instantly share code, notes, and snippets.

@cspotcode
Forked from webstrand/a-yaml-loader.mjs
Last active January 25, 2022 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cspotcode/c76948412b5c8575020b4b4f239cdd26 to your computer and use it in GitHub Desktop.
Save cspotcode/c76948412b5c8575020b4b4f239cdd26 to your computer and use it in GitHub Desktop.
{
"name": "yaml-loader",
"exports": {
".": "./yaml-loader.mjs"
}
}
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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment