Rollup for proper CLI

This commit is contained in:
Surma
2020-09-09 18:22:50 +01:00
parent 3b07862efb
commit 47fb7f9f71
8 changed files with 657 additions and 29 deletions

33
cli/lib/json-plugin.js Normal file
View File

@@ -0,0 +1,33 @@
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;
},
};
}