babel-plugin-svg-react
Version:
Transpile SVG documents into React component modules with only Babel.
39 lines (27 loc) • 1.04 kB
JavaScript
const path = require("path");
const fs = require("fs-extra");
const output =
'export { default } from "babel-plugin-svg-react/lib/SvgComponent";\n';
module.exports = async function writeDeclarations({ svgDir, outDir, dryRun }) {
if (dryRun) {
console.log("Dry run enabled. Changes will not be persisted.");
}
const cwd = process.cwd();
const filenames = await fs.readdir(svgDir);
const svgFilenames = filenames.filter(filename => {
const fileExt = path.extname(filename);
return fileExt === ".svg";
});
console.log(`Found ${svgFilenames.length} SVG files.`);
const jobs = svgFilenames.map(async filename => {
const fileExt = path.extname(filename);
if (fileExt !== ".svg") return;
const outFilePath = path.resolve(outDir, `${filename}.d.ts`);
const relativeOutFilePath = path.relative(cwd, outFilePath);
if (!dryRun) {
await fs.writeFile(outFilePath, output);
}
console.log(`Wrote declaration to: ${relativeOutFilePath}.`);
});
return Promise.all(jobs);
};