Sha256: 3bce5733bdaa81b720c751c4f8f77dbc70790643725b3d1572e4ce526e317995

Contents?: true

Size: 1.74 KB

Versions: 37

Compression:

Stored size: 1.74 KB

Contents

import { optimize } from "svgo";
import fs from "fs/promises";
import { existsSync, mkdirSync } from "fs";
import prettier from "prettier";

export default function (sourcePath, destPath, configPath) {
  return {
    name: "optimize-icons",
    setup(build) {
      build.onEnd(async (result) => {
        const files = await fs.readdir(sourcePath);
        const iconNames = [];

        if (!existsSync(destPath)) {
          mkdirSync(destPath);
        }

        await Promise.all(
          files
            .filter((fileName) => fileName.endsWith(".svg"))
            .map(async (fileName) => {
              const filePath = `${sourcePath}/${fileName}`;
              const svgText = await fs.readFile(filePath, {
                encoding: "utf8",
              });

              const optimizedSvg = optimize(svgText, {
                path: filePath,
                plugins: [
                  {
                    name: "removeAttrs",
                    params: {
                      attrs:
                        "(fill|stroke|stroke-width|stroke-linecap|stroke-linejoin)",
                    },
                  },
                ],
              });
              await fs.writeFile(`${destPath}/${fileName}`, optimizedSvg.data);

              iconNames.push(fileName.replace(".svg", ""));
            })
        );

        const iconsDataFilePath = `${configPath}/icons.json`;

        prettier.resolveConfig(iconsDataFilePath).then(async (options) => {
          const formattedJSON = await prettier.format(
            JSON.stringify(iconNames.sort()),
            {
              ...options,
              parser: "json",
            }
          );
          await fs.writeFile(iconsDataFilePath, formattedJSON);
        });
      });
    },
  };
}

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
coveragebook_components-0.19.8 lib/scripts/esbuild-plugin-build-icons.mjs
coveragebook_components-0.19.7 lib/scripts/esbuild-plugin-build-icons.mjs
coveragebook_components-0.19.6 lib/scripts/esbuild-plugin-build-icons.mjs
coveragebook_components-0.19.5 lib/scripts/esbuild-plugin-build-icons.mjs
coveragebook_components-0.19.4 lib/scripts/esbuild-plugin-build-icons.mjs
coveragebook_components-0.19.3 lib/scripts/esbuild-plugin-build-icons.mjs
coveragebook_components-0.19.2 lib/scripts/esbuild-plugin-build-icons.mjs
coveragebook_components-0.19.1 lib/scripts/esbuild-plugin-build-icons.mjs
coveragebook_components-0.19.0 lib/scripts/esbuild-plugin-build-icons.mjs
coveragebook_components-0.18.8 lib/scripts/esbuild-plugin-build-icons.mjs
coveragebook_components-0.18.7 lib/scripts/esbuild-plugin-build-icons.mjs
coveragebook_components-0.18.0 lib/scripts/esbuild-plugin-build-icons.mjs
coveragebook_components-0.17.7 lib/scripts/esbuild-plugin-build-icons.mjs
coveragebook_components-0.17.6 lib/scripts/esbuild-plugin-build-icons.mjs
coveragebook_components-0.17.5 lib/scripts/esbuild-plugin-build-icons.mjs
coveragebook_components-0.17.4 lib/scripts/esbuild-plugin-build-icons.mjs
coveragebook_components-0.17.3 lib/scripts/esbuild-plugin-build-icons.mjs
coveragebook_components-0.17.2 lib/scripts/esbuild-plugin-build-icons.mjs
coveragebook_components-0.17.1 lib/scripts/esbuild-plugin-build-icons.mjs
coveragebook_components-0.17.0 lib/scripts/esbuild-plugin-build-icons.mjs