npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

figma-export-assets

v1.0.4

Published

Script to generate and download assets from a Figma file

Downloads

168

Readme

figma-export-assets

Install

npm install figma-export-assets

Description

A highly customizable package for exporting assets from Figma API in any supported format.

🙏🏻 Based on concepts by https://github.com/tsimenis/figma-export-icons and https://github.com/nate-summercook/figma-assets-exporter but with focus on customizability. Thanks to both of them for their work!

Features

  • 📄 Multiple Figma Pages/Frames: Configure to process assets from various Figma pages or specific frames.
  • 🔄 Batch Exporting: Supports batch exporting out of the box to overcame Figma API export limits.
  • 📁 Customizable Asset Paths/Names: Set unique saving paths or names for each asset.
  • 🌈 Customizable Asset Format: Chose any Figma export format for each asset.
  • 🚫 Asset Exclusion: Easily exclude specific assets from export based on their names.
  • ⚙️ Axios Integration: Extend or modify Axios configurations for advanced HTTP request handling.
  • 🌟 Variant Exporting: Overridable option to export components with variants as separate assets.

Example: Basic Usage

// get figmaPersonalToken and fileId from .env
require("dotenv").config({ path: ".env" });

const { FigmaExporter } = require("figma-export-assets");

const config = {
	figmaPersonalToken: process.env.figma_token,
	fileId: process.env.figma_file_id,
	page: "📎 assets",
	assetsPath: "src",
};

async function main() {
	// 0. Initialize exporter
	const figma = new FigmaExporter(config);

	// 1. Get an array of all assets in the Figma file
	let assets = await figma.getAssets();

	// 2. Create SVGs (Figma API)
	svgs = await figma.exportAssets(assets, "svg");

	const svgDownloads = svgs.map(async (asset) => {
		// 3. Download each exported asset
		await figma.saveAsset(asset);
		console.log(`Downloaded ${asset.name} as svg`);
	});

	await Promise.all(svgDownloads);
}

// Run everything
main();

Example: Individual Asset Handling

// get figmaPersonalToken and fileId from .env
require("dotenv").config({ path: ".env" });

const { FigmaExporter } = require("figma-export-assets");

const config = {
	figmaPersonalToken: process.env.figma_token,
	fileId: process.env.figma_file_id,
	page: "📎 assets",
	assetsPath: "src",
};

async function main() {
	// 0. Initialize exporter
	const figma = new FigmaExporter(config);

	// Helper function to optimize the path coming from Figma
	const optimizePath = (path) =>
		path.replace("assets/", "").replace("name=", "").replace(".png", "");

	// 1. Get an array of all assets in the Figma file
	let assets = await figma.getAssets();

	// 2. Create PNGs

	// 2a. Select all assets which have `.png` in their name in Figma
	let pngs = assets.filter((asset) => asset.name.includes(".png"));
	// 2b. Let Figma export the assets as PNGs with a scale of 4
	pngs = await figma.exportAssets(pngs, "png", 4);
	const pngDownloads = pngs.map(async (asset) => {
		// 2c. Download each exported asset
		await figma.saveAsset(asset, {
			// 2d. Optimize the path coming from Figma
			path: optimizePath(asset.name),
		});
		console.log(`Downloaded ${asset.name} as png`);
	});
	await Promise.all(pngDownloads);

	// 3. Create SVGs

	// 3a. Select all assets which do NOT have `.png` in their name in Figma
	let svgs = assets.filter((asset) => !asset.name.includes(".png"));
	// 3b. Let Figma export the assets as SVGs
	svgs = await figma.exportAssets(svgs, "svg");
	const svgDownloads = svgs.map(async (asset) => {
		// 3c. Download each exported asset
		await figma.saveAsset(asset, {
			// 3d. Optimize the path coming from Figma
			name: optimizePath(asset.name),
		});
		console.log(`Downloaded ${asset.name} as svg`);
	});
	await Promise.all(svgDownloads);
}

// Run everything
main();

Constructor

constructor(config)

Creates a new instance of the FigmaExporter.

Parameters

  • config: An object containing configuration settings.
    • baseURL (string, optional): The base URL for the Figma API. Defaults to 'https://api.figma.com/v1'.
    • format (string, optional): The format of the exported assets. Defaults to 'svg'.
    • assetsPath (string, required): The path to save the exported assets to.
    • scale (number, optional): The scale at which to export assets. Defaults to 1.
    • axiosConfig (Object, optional): Additional Axios configuration settings.
    • exportVariants (boolean, optional): Whether to export variants of the assets. Defaults to true.
    • figmaPersonalToken (string, required): Personal access token for the Figma API.
    • fileId (string, required): The ID of the Figma file to export assets from.
    • page (string, required): The name of the page to export assets from.
    • frame (string, optional): The name of the frame to export assets from.

Methods

getAssets()

Fetches assets from Figma using the configured settings.

Returns

  • Promise<Array>: A promise that resolves to an array of assets.

exportAssets(assets, format, scale, batchSize)

Exports assets from Figma in batches.

Parameters

  • assets (Array): The assets to export.
  • format (string, optional): The format to export the assets in. Defaults to 'svg'.
  • scale (number, optional): The scale at which to export the assets. Defaults to 1.
  • batchSize (number, optional): The number of assets to export in each batch. Defaults to 100.

Returns

  • Promise<Array>: A promise that resolves to an array of exported assets.

saveAsset(asset, overrideConfig)

Saves assets to the configured assets path.

Parameters

  • asset (Object): The asset to save.
  • overrideConfig (Object, optional): Overrides for the exporter configuration.
    • name (string, optional): Overrides the name of the asset.
    • format (string, optional): The format of the exported assets. Defaults to 'svg'.
    • assetsPath (string, optional): The path to save the exported assets to.
    • scale (number, optional): The scale at which to export assets. Defaults to 1.

Returns

  • Promise: A promise that resolves when all assets have been saved.