@baybreezy/file-extension-icon
v0.0.7
Published
Modern, lightweight package for folder and file extension specific Material icons and VSCode icons
Maintainers
Readme
@baybreezy/file-extension-icon

Modern, TypeScript-first package for file and folder extension-specific Material and VSCode icons
A complete rewrite of file-extension-icon-js using modern TypeScript, ESM-first approach, and the UnJS ecosystem for optimal performance and developer experience.
✨ Features
- 🎨 Two icon themes: Material Icon Theme & VSCode Icons
- 🔷 TypeScript: Full type safety with comprehensive type definitions
- 📦 Zero dependencies: No runtime dependencies
- 🌳 Tree-shakeable: Per-theme entry points, so you only load the icons you use
- 🔄 Dual format: ESM and CommonJS support
- 🧾 Output formats: base64 data URI, URL-encoded data URI or raw SVG
- 🛣️ Paths welcome:
src/app/package.jsonresolves likepackage.json - 🧩 Customizable: add your own file/folder associations and SVG icons
- ⚛️ React & Vue components:
<FileIcon name="index.ts" /> - 🚀 Modern build: Built with tsdown
- ✅ Well-tested: Comprehensive test coverage
- 📝 Well-documented: Full JSDoc comments and examples
- 🖥️ CLI included: Interactive and scriptable terminal tool for browsing and exporting icons
📦 Installation
# @antfu/ni
npx ni @baybreezy/file-extension-icon🚀 Usage
Basic Usage
import {
getMaterialFileIcon,
getMaterialFolderIcon,
getVSIFileIcon,
getVSIFolderIcon,
} from "@baybreezy/file-extension-icon";
// Get Material Design file icon
const jsIcon = getMaterialFileIcon("index.js");
// Returns: "data:image/svg+xml;base64,..."
// Get Material Design folder icon (closed)
const folderIcon = getMaterialFolderIcon("components");
// Get Material Design folder icon (open)
const openFolderIcon = getMaterialFolderIcon("components", true);
// Get VSCode file icon
const tsIcon = getVSIFileIcon("app.ts");
// Get VSCode folder icon
const vsiFolderIcon = getVSIFolderIcon("src", true);File and folder names can also be paths (/ or \); the last segment is used. Matching is case-insensitive.
getMaterialFileIcon("src/app/package.json"); // same as "package.json"
getMaterialFolderIcon("project/src/", true); // same as "src"Load Only One Theme
If you only use one icon theme, import it from its own entry point. This loads just that theme's icon data, which keeps your bundle (and server start-up) much smaller:
// Material Icon Theme only (~0.9 MB of icon data instead of ~4.2 MB)
import {
getMaterialFileIcon,
getMaterialFolderIcon,
} from "@baybreezy/file-extension-icon/material";
// VSCode Icons only
import { getVSIFileIcon, getVSIFolderIcon } from "@baybreezy/file-extension-icon/vsi";The root import (@baybreezy/file-extension-icon) still exports everything, as before.
Output Formats
Every icon function takes an optional format:
| Format | Returns | Use it for |
| ------------------ | ------------------------------- | ------------------------------------- |
| "base64" default | data:image/svg+xml;base64,... | <img src>, CSS url(), anywhere |
| "utf8" | data:image/svg+xml,%3Csvg ... | Same places, ~23% smaller than base64 |
| "svg" | <svg ...>...</svg> | Inline SVG, writing .svg files |
getMaterialFileIcon("index.ts", { format: "svg" });
getMaterialFolderIcon("src", { open: true, format: "utf8" });Encoded results are cached per icon, so repeated lookups are cheap.
One API for Both Themes
import { getFileIcon, getFolderIcon } from "@baybreezy/file-extension-icon";
getFileIcon("index.ts"); // Material (default)
getFileIcon("index.ts", { theme: "vsi", format: "svg" }); // VSCode Icons
getFolderIcon("src", { theme: "vsi", open: true });Icon Details
Find out which icon was picked and why:
import { getFileIconInfo } from "@baybreezy/file-extension-icon";
getFileIconInfo("Dockerfile.dev");
// {
// iconName: "docker",
// matchedBy: "fileName", // "fileName" | "extension" | "folderName" | "default"
// matchedKey: "dockerfile.dev",
// svg: "<svg ...>",
// dataUri: "data:image/svg+xml;base64,..."
// }Icons by Name
import { getAssociations, getIconByName, getIconNames } from "@baybreezy/file-extension-icon";
getIconByName("typescript"); // Material file icon by icon name
getIconByName("folder-src", { type: "folder", open: true, format: "svg" });
getIconByName("nope"); // undefined
getIconNames(); // { file: [...], folder: [...] } every icon in the theme
getAssociations("vsi"); // { fileNames, extensions, folderNames } every name with a specific iconCustom Associations and Icons
Layer your own names and SVGs on top of a theme. The built-in functions are not affected.
import { createIconResolver } from "@baybreezy/file-extension-icon";
const icons = createIconResolver({
theme: "material",
extensions: { tpl: "html" }, // map to an existing icon
fileNames: { "acme.config": "acme" }, // map to a custom icon
fileIcons: { acme: "<svg ...>...</svg>" },
folderNames: { infra: "folder-server" },
folderIcons: { "my-folder": { closed: "<svg ...>", open: "<svg ...>" } },
});
icons.getFileIcon("page.tpl");
icons.getFolderIcon("infra", { open: true });Per-theme versions are available too: createMaterialIconResolver() and createVSIIconResolver().
React and Vue Components
react (17+) and vue (3.3+) are optional peer dependencies; install whichever you use.
import { FileIcon, FolderIcon } from "@baybreezy/file-extension-icon/react";
<FileIcon name="index.ts" />
<FileIcon name="package.json" theme="vsi" size={24} alt="package.json" className="icon" />
<FolderIcon name="src" open={isExpanded} /><script setup lang="ts">
import { FileIcon, FolderIcon } from "@baybreezy/file-extension-icon/vue";
</script>
<template>
<FileIcon name="index.ts" />
<FolderIcon name="src" :open="isExpanded" theme="vsi" :size="24" />
</template>Both render an <img> (with alt="" by default, since the icon is usually decorative). Other attributes pass through to it.
Other Frameworks / HTML
<script type="module">
import { getMaterialFileIcon } from "@baybreezy/file-extension-icon";
const icon = getMaterialFileIcon("index.html");
document.getElementById("icon").src = icon;
</script>
<img id="icon" alt="html" width="24" height="24" />Advanced Usage
import { getMaterialFileIcon } from "@baybreezy/file-extension-icon";
// The package intelligently matches file names, most specific first
getMaterialFileIcon("app.component.ts"); // Matches Angular component
getMaterialFileIcon("test.spec.ts"); // Matches test file
getMaterialFileIcon("package.json"); // Matches package.json
getMaterialFileIcon("Dockerfile"); // Matches Docker
// Works with any file extension
getMaterialFileIcon("script.py");
getMaterialFileIcon("style.scss");
getMaterialFileIcon("README.md");📚 API Reference
All icon functions return a base64 data URI unless you pass a format.
Theme-agnostic (root import only)
theme is "material" (default) or "vsi".
| Function | Returns |
| ------------------------------------------------------------ | ---------------------------------------- |
| getFileIcon(fileName, { theme?, format? }) | string |
| getFolderIcon(folderName, { theme?, open?, format? }) | string |
| getFileIconInfo(fileName, { theme? }) | IconInfo |
| getFolderIconInfo(folderName, { theme?, open? }) | IconInfo |
| getIconByName(iconName, { theme?, type?, open?, format? }) | string \| undefined |
| getIconNames(theme?) | { file: string[]; folder: string[] } |
| getAssociations(theme?) | { fileNames; extensions; folderNames } |
| createIconResolver({ theme?, ...customizations }) | IconResolver |
Material Icons (/material or root)
| Function | Returns |
| ------------------------------------------------------------- | --------------------- |
| getMaterialFileIcon(fileName, { format? }?) | string |
| getMaterialFolderIcon(folderName, open?) | string |
| getMaterialFolderIcon(folderName, { open?, format? }) | string |
| getMaterialFileIconInfo(fileName) | IconInfo |
| getMaterialFolderIconInfo(folderName, open?) | IconInfo |
| getMaterialIconByName(iconName, { type?, open?, format? }?) | string \| undefined |
| getMaterialIconNames() | IconNames |
| getMaterialAssociations() | IconAssociations |
| createMaterialIconResolver(customizations?) | IconResolver |
VSCode Icons (/vsi or root)
The same functions with a VSI prefix: getVSIFileIcon, getVSIFolderIcon, getVSIFileIconInfo, getVSIFolderIconInfo, getVSIIconByName, getVSIIconNames, getVSIAssociations, createVSIIconResolver.
IconResolver
Returned by the create*IconResolver functions. Same behaviour as above, bound to one theme plus your customizations: getFileIcon, getFolderIcon, getFileIconInfo, getFolderIconInfo, getIconByName, getIconNames, getAssociations.
Types
IconTheme, IconFormat, IconOptions, FolderIconOptions, IconByNameOptions, IconInfo, IconNames, IconAssociations, IconCustomizations, IconResolver, plus the key types MaterialFileExtensionKeys, MaterialFileNameKeys, MaterialFolderNameKeys, VSIFileExtensionKeys, VSIFileNameKeys, VSIFolderNameKeys.
Utility Functions
import {
convertToBase64,
createDataUri, // createDataUri(svg, "base64" | "utf8")
findIconInCollection,
getIconFromMap,
} from "@baybreezy/file-extension-icon";🖥️ CLI Tool
This package includes a CLI for browsing and exporting icons, interactively or from scripts.
# Run interactively
npx file-icon
# Or with flags for quick export
npx file-icon -t material -y file -n "index.js" --copy
# Save a raw SVG
npx file-icon -n package.json -o package.svg
# Print to stdout for piping (no prompts)
npx file-icon -y folder -n src --open -p -f utf8
# JSON with match details
npx file-icon -n Dockerfile --jsonCLI Features
- 🎨 Browse Material and VSCode icon themes
- 🔍 Enter any file/folder name or path, or autocomplete over every known name
- 📋 Copy to clipboard, save to file (
.svgor.txt), or display in terminal - 🧾 base64, URL-encoded or raw SVG output
- 🤖
--printand--jsonfor scripts - ⚡ Fast and beautiful UI powered by Clack
📊 Bundle Size
This package contains comprehensive icon data for both Material and VSCode icon themes.
- Package size (compressed): ~2.7 MB
- Unpacked size: ~8.9 MB (ESM and CommonJS builds)
- Includes: Thousands of file and folder icons
| Import | Icon data loaded |
| ----------------------------------------- | ---------------- |
| @baybreezy/file-extension-icon | ~4.2 MB (both) |
| @baybreezy/file-extension-icon/material | ~0.9 MB |
| @baybreezy/file-extension-icon/vsi | ~3.2 MB |
Optimization Tips
Import only the theme you use from
/materialor/vsi(see Load Only One Theme).Import only what you need:
// ✅ Good - Tree-shakeable import { getMaterialFileIcon } from "@baybreezy/file-extension-icon"; // ❌ Avoid - Imports everything import * as Icons from "@baybreezy/file-extension-icon";Use modern bundlers with tree-shaking support (Vite, Rollup, esbuild, webpack 5+). The package is marked
"sideEffects": false.
🛠️ Development
# Install dependencies
npm install
# Build the package
npm run build
# Run tests
npm test
# Run tests with coverage
npm run test:coverage
# Run tests in UI mode
npm run test:ui
# Type check
npm run typecheck
# Lint code
npm run lint
# Regenerate the icon data from the latest upstream themes
npm run sync:iconsUpdating the Icon Data
npm run sync:icons downloads the latest material-icon-theme package and vscode-icons release and regenerates src/data. By default it merges: existing names are never removed, so the exported key types never lose members. Options: --material <version>, --vsi <version>, --only material|vsi, --replace (upstream data only, may remove names), --dry-run, --summary <file.md>.
The Sync Icon Data GitHub workflow runs this monthly (or on demand) and opens a pull request with a summary for review.
🧪 Testing
This package includes comprehensive test coverage:
# Run tests in watch mode
npm test
# Generate coverage report
npm run test:coverage
# Open test UI
npm run test:ui🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📄 License
MIT © Behon Baker
🙏 Credits & Acknowledgments
This package is a complete TypeScript rewrite of file-extension-icon-js by MD. Minhazul Islam.
Original Package
- Original Author: MD. Minhazul Islam
- Original Package: file-extension-icon-js
- License: MIT
Icon Sources
- Material Icon Theme by Philipp Kief
- VSCode Icons by VSCode Icons Team
Inspiration
- vscode-icons-js by Dominik Dereǰanik (the language-to-extension table in
scripts/language-extensions.jsoncomes from its data, MIT) - file-icons-js by Web Semantics
