Svgr: Generate index.js when `--out-dir` option is used

Created on 26 Sep 2018  Â·  6Comments  Â·  Source: gregberge/svgr

As suggested in #161, we could generate an index.js containing all icons when we use --out-dir option.

Be careful to be compatible with #182.

enhancement ✨ v5

Most helpful comment

All 6 comments

@sis @gregberge the package above doesn't have options to generate index.ts. Rest all good.

Open issue here - https://github.com/gajus/create-index/issues/48

Here, I have written a tiny nodejs script to achieve the same after create-index generates an index.js ( I have a Typescript project ).
Basically I had to replace .tsx from all import statements in index.js. Second, I had to rename it to index.ts.

const path = require("path");
const fs = require("fs");

const filePath = path.join("src", "icons");
const fileName = "index.js";

fs.readFile(`${filePath}/${fileName}`, "utf8", (err, data) => {
    if (err) throw err;
    const newStr = data.replace(/.tsx/g, "");
    fs.writeFile(`${filePath}/${fileName}`, newStr, writeErr => {
        if (writeErr) throw err;
        fs.rename(`${filePath}/${fileName}`, `${filePath}/index.ts`, renameErr => {
            if (renameErr) throw renameErr;
            console.log("rename completed");
        });
    });
});

package.json scripts as follows:

"build-icons": "rm -rf src/icons && npx @svgr/cli -d src/icons svg-icons --ext tsx",
"create-index": "npx create-index src/icons --extensions tsx",
"recreate-index": "node scripts/regenerate-icon-index.js",
"lint:icons": "eslint './src/icons/*.{ts,tsx}' --fix",
"generate-icons": "yarn run build-icons && yarn run create-index && yarn run recreate-index && yarn run lint:icons"

could we have semicolons at the end of the default template or an example of how to customize it?

Custom templates are now supported. A next step would be to run prettier on the index file. Do you want to submit a PR to do it?

Figured it out in a different issue, thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lucasjs picture lucasjs  Â·  3Comments

7ynk3r picture 7ynk3r  Â·  4Comments

alamothe picture alamothe  Â·  6Comments

rosenfeld picture rosenfeld  Â·  6Comments

SilencerWeb picture SilencerWeb  Â·  4Comments