Sha256: 261440c882e0314447e6e500ac7fe19c151859267507b2a0a87faec0448fc56e

Contents?: true

Size: 1.73 KB

Versions: 36

Compression:

Stored size: 1.73 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 = prettier.format(
            JSON.stringify(iconNames.sort()),
            {
              ...options,
              parser: "json",
            }
          );
          await fs.writeFile(iconsDataFilePath, formattedJSON);
        });
      });
    },
  };
}

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
coveragebook_components-0.8.7 lib/scripts/esbuild-plugin-build-icons.js
coveragebook_components-0.8.6 lib/scripts/esbuild-plugin-build-icons.js
coveragebook_components-0.8.5 lib/scripts/esbuild-plugin-build-icons.js
coveragebook_components-0.8.4 lib/scripts/esbuild-plugin-build-icons.js
coveragebook_components-0.8.3 lib/scripts/esbuild-plugin-build-icons.js
coveragebook_components-0.8.2 lib/scripts/esbuild-plugin-build-icons.js
coveragebook_components-0.8.1 lib/scripts/esbuild-plugin-build-icons.js
coveragebook_components-0.8.0 lib/scripts/esbuild-plugin-build-icons.js
coveragebook_components-0.8.0.beta.3 lib/scripts/esbuild-plugin-build-icons.js
coveragebook_components-0.8.0.beta.2 lib/scripts/esbuild-plugin-build-icons.js
coveragebook_components-0.8.0.beta.1 lib/scripts/esbuild-plugin-build-icons.js
coveragebook_components-0.7.10 lib/scripts/esbuild-plugin-build-icons.js
coveragebook_components-0.7.9 lib/scripts/esbuild-plugin-build-icons.js
coveragebook_components-0.7.8 lib/scripts/esbuild-plugin-build-icons.js
coveragebook_components-0.7.7 lib/scripts/esbuild-plugin-build-icons.js
coveragebook_components-0.7.6 lib/scripts/esbuild-plugin-build-icons.js
coveragebook_components-0.7.5 lib/scripts/esbuild-plugin-build-icons.js
coveragebook_components-0.7.4 lib/scripts/esbuild-plugin-build-icons.js
coveragebook_components-0.7.3 lib/scripts/esbuild-plugin-build-icons.js
coveragebook_components-0.7.2 lib/scripts/esbuild-plugin-build-icons.js