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

@kitschpatrol/vite-plugin-tldraw

v2.3.8

Published

Vite plugin enabling module-like import of local tldraw .tldr files with automatic conversion to SVG or PNG.

Downloads

42

Readme

@kitschpatrol/vite-plugin-tldraw

NPM Package @kitschpatrol/vite-plugin-tldraw License: MIT

Vite plugin enabling module-like import of local tldraw .tldr files with automatic conversion to SVG or PNG.

Overview

A Vite plugin to automate the import and conversion of local tldraw .tldr files into SVG or PNG image assets.

This allows .tldr files to be imported just like regular .webp, .jpeg etc. files in Vite-powered projects:

// main.ts
import tldrImage from './assets/test-sketch.tldr'

const body = document.querySelector<HTMLDivElement>('body')
if (body) body.innerHTML = `<img src="${tldrImage}" />`

The above transforms ./assets/test-sketch.tldr into ./assets/test-sketch-{hash}.svg, caches the output file, and then returns an SVG URL ready to be passed to an img element's src attribute.

The plugin provides a global configuration object to customize of several aspects of the conversion process, and also allows overrides on a per-import basis via query parameters on the asset import path, e.g.:

import tldrImage from './assets/test-sketch.tldr?format=png&tldr'

For lower-level processing of .tldr files in Node projects or via the command line, please see @kitschpatrol/tldraw-cli.

Installation

1. Install the plugin package

Assuming you're starting with a Vite project of some flavor:

npm install --save-dev @kitschpatrol/vite-plugin-tldraw

2. Add the plugin to your vite.config file

// vite.config.ts
import tldraw from '@kitschpatrol/vite-plugin-tldraw'
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [tldraw()],
})

3. Configure TypeScript

Skip this step if you're using plain JavaScript.

Add the extension declarations to your types in tsconfig.json:

{
  "compilerOptions": {
    "types": ["@kitschpatrol/vite-plugin-tldraw/ext"]
  }
}

Alternately, you can add a triple-slash package dependency directive to your global types file (e.g. env.d.ts or similar):

/// <reference types="@kitschpatrol/vite-plugin-tldraw/ext" />

This step should take care of errors like:

Cannot find module './assets/test-sketch.tldr' or its corresponding type declarations.ts(2307)

Usage

Save your tldraw project to a .tldr file.

Add it to your project, most likely in an assets folder.

Then simply import the .tldr file to get a working asset URL:

// example.ts
import tldrImage from './assets/test-sketch.tldr'

// Logs a working SVG URL
console.log(tldrImage)

See the sections below for additional conversion options.

Plugin Options

vite-plugin-tldraw inherits most of the configuration flags available in @kitschpatrol/tldraw-cli.

TldrawPluginOptions

| Key | Type | Description | Default | | --------------------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------- | | defaultImageOptions | TldrawImageOptions | Default options object for all the image conversion process. See section below for more detail. | See section below | | cacheEnabled | boolean | Caches generated image files. Hashes based on the source .tldr content and any TldrawImageOptions or import query parameters ensure the cache regenerates as needed. Cached files are stored in Vite's config.cacheDir (usually /node_modules/.vite). | true | | verbose | boolean | Log information about the conversion process to the console. | false |

TldrawImageOptions

| Key | Type | Description | Default | | ------------- | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ | ------- | | format | "png" \| "svg" | Output image format. | "svg" | | transparent | boolean | Output image with a transparent background. | false | | dark | boolean | Output a dark theme version of the image. | false | | stripStyle | boolean | Remove <style> elements from SVG output, useful to lighten the load of embedded fonts or if you are providing your own stylesheet for the SVG. | false | | padding | number | Set a specific padding amount around the exported image. | 32 | | scale | number | Set a sampling factor for raster image exports. | 1 |

Plugin options example

Configure the plugin to always generate PNGs with a transparent background, and to log conversion details:

// vite.config.ts
import tldraw from '@kitschpatrol/vite-plugin-tldraw'
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [
    tldraw({
      verbose: true,
      format: 'png',
      transparent: true,
    }),
  ],
})

The @kitschpatrol/vite-plugin-tldraw also exports TldrawPluginOptions and TldrawImageOptions types for your convenience.

Import path options

Import directives may include query parameters to set image conversion options on a per-import basis.

Query parameters take precedence over TldrawPluginOptions set at plugin instantiation in your vite.config.ts.

Note: Due to constraints in TypeScript's module declaration wildcards, the import path must be suffixed with &tldr or &tldraw when query parameters are used.

Additional query parameter options

In addition to all TldrawImageOptions, query parameters also accept additional options for selecting specific parts of a sketch:

| Key | Type | Description | Default | | ------- | --------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------- | | frame | string \| undefined | When defined, outputs only a specific frame from the .tldr file. Provide either the frame name or its shape ID, e.g. Frame 1. Slugified frame names will also match, e.g. frame-1. | undefined | | page | string \| undefined | When defined, outputs only a specific page from the .tldr file. Provide either the frame name or its page ID, e.g. Page 1. Slugified frame names will also match, e.g. page-1. | undefined |

Import path query parameter examples

// example.ts
import tldrImagePng from './assets/test-sketch.tldr?format=png&tldr'
import tldrImageTransparentPng from './assets/test-sketch.tldr?format=png&transparent=true&tldr'
import tldrImageFrame from './assets/test-sketch-three-frames.tldr?frame=frame-1&tldr'

// Logs a PNG URL
console.log(tldrImagePng)

// Logs a transparent-background PNG URL
console.log(tldrImageTransparentPng)

// Logs an SVG URL for "Frame 1" in the source `.tldr`
console.log(tldrImageFrame)

Implementation notes

This tool is not a part of the official tldraw project, and it is currently only tested and known to be compatible with tldraw 2.0.0-beta.2.

Behind the scenes, the plugin calls @kitschpatrol/tldraw-cli's Node API to generate image files from .tldr files, and then passes the resulting URL as the value of the module import.

Because tldraw-cli relies on the browser automation tool Puppeteer for its output, conversion can be a bit slow (on the order of a second or two), so by default generated image assets are cached to expedite subsequent builds.

During development, images are served from the cache, and when Vite builds for production the image files are bundled into the output with a hashed filename to simplify cache busting.

The future

Possible paths for future improvements include the following:

  • Rollup cross-compatibility
  • Support importing tldraw.com URLs
  • SVG compression, PNG resizing / optimization (or test integration with other asset pipeline plugins)

Any other suggestions are welcome.

I'm consciously releasing this tool under the @kitschpatrol namespace on NPM to leave the vite-plugin-tldraw package name available to the core tldraw project.

References

Some links and issues from development are retained for my own reference below:

TypeScript module query parameter compatibility:

Vite asset plugin approach:

Vite asset Path issues:

Maintainers

@kitschpatrol

Contributing

Issues and pull requests are welcome.

License

MIT © Eric Mika