34 lines
915 B
JavaScript
34 lines
915 B
JavaScript
import {promises as fsp} from "fs";
|
|
|
|
const prefix = "json:";
|
|
|
|
export default function ejsAssetPlugin() {
|
|
return {
|
|
name: "json-plugin",
|
|
async resolveId(id, importer) {
|
|
if (!id.startsWith(prefix)) return;
|
|
const realId = id.slice(prefix.length);
|
|
const resolveResult = await this.resolve(realId, importer);
|
|
|
|
if (!resolveResult) {
|
|
throw Error(`Cannot find ${realId}`);
|
|
}
|
|
// Add an additional .js to the end so it ends up with .js at the end in the _virtual folder.
|
|
return prefix + resolveResult.id;
|
|
},
|
|
async load(id) {
|
|
if (!id.startsWith(prefix)) return;
|
|
const realId = id.slice(prefix.length);
|
|
const source = await fsp.readFile(realId, "utf8");
|
|
|
|
let code = "";
|
|
for(const [key, value] of Object.entries(JSON.parse(source))) {
|
|
code += `
|
|
export const ${key} = ${JSON.stringify(value)};
|
|
`
|
|
}
|
|
return code;
|
|
},
|
|
};
|
|
}
|