mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-12 16:57:26 +00:00
Rollup for proper CLI
This commit is contained in:
33
cli/lib/json-plugin.js
Normal file
33
cli/lib/json-plugin.js
Normal 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;
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user