-
-
Save cspotcode/c76948412b5c8575020b4b4f239cdd26 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "yaml-loader", | |
"exports": { | |
".": "./yaml-loader.mjs" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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