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

vite-plugin-excalibur-resources

v0.6.0

Published

Automatically loads your resources for [Excalibur](https://excaliburjs.com/) games with full Typescript support.

Downloads

16

Readme

vite-plugin-excalibur-resources

Automatically loads your resources for Excalibur games with full Typescript support.

// equivalent to ex.ImageSource('/res/player.png')
const sprite = $res('player.png').toSprite()
actor.graphics.use(sprite)

// resource can safely be used from $res inline, no need to store in a variable
$res('jump.mp3').play()

It even has full typescript support for your files

autocomplete

Installation

npm install vite-plugin-excalibur-resources

Add the plugin to your Vite config

// vite.config.js
import { defineConfig } from 'vite'
import resources from 'vite-plugin-excalibur-resources'

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

Usage

Create a public/res folder. All of your resources must be under this folder.

public
└── res
    ├── myimage.png
    └── mysound.mp3

Use $res to load resources

const sprite = new ex.Sprite({
  image: $res('myimage.png'),
})

Add the resources to your loader.

import { resources } from 'vite-plugin-excalibur-resources/runtime'

const loader = new Loader(resources)

game.start(loader)

Pro Tip: if you dynamically import a file resources will be populated for those files if they use $res. This means you can load resources incrementally (and automatically!) as you load scenes, for example.

async function goToLevel2() {
  await import('./scenes/level2')

  game.start(loader).then(() => {
    game.goToScene('level2')
  })
}

Typescript

You can get proper typing and autocompletion for your resources by updating your tsconfig.json like so:

{
  "compilerOptions": {
    "types": ["vite-plugin-excalibur-resources/types"]
  }
}

I'd also recommend adding the following to your package.json so types are correctly generated after npm installing

"scripts": {
  "prepare": "excalibur-resources generate"
}

It can also be ran manually with npx excalibur-resources generate.

If you notice types not updating while developing, try restarting the dev server. If you're using VS Code, try 'Restart TS Server' via the command palette.

Overriding resource type

By default a resource is determined by its file extension. However, extensions are not always 1-to-1 to a resource (for example, JSON could be used for many different resources). You can override the resource type by using the as option.

(The Aseprite loader will actually require you to provide as to work because it uses JSON)

$res('/player.json', { as: 'aseprite' })
$res('/tileset.json', { as: 'tiled' })

Custom resource loader

You can add your own resource types by providing a loaders option - a path to a file that adds additional resource loaders.

import { defineConfig } from 'vite'
import resources from 'vite-plugin-excalibur-resources'

export default defineConfig({
  plugins: [
    resources({
      loaders: '/src/loaders.ts',
    }),
  ],
})

Here's an example that adds a custom resource type that loads a .ctm file.

// src/loaders.ts
export default {
  // name of your resource. this is used for `as`
  custom: {
    load: (path, options) => {
      // your custom resource class, see https://excaliburjs.com/docs/api/edge/classes/Resource.html
      // this is what will be passed to the Excalibur Loader
      return new CustomResource(path, options)
    },
    // optional - extensions to match for this loader when `as` is not specified
    extension: ['ctm'],
  },
}

// if you're using typescript, this will update $res for your custom resource type
declare module 'vite-plugin-excalibur-resources/types' {
  interface Resources {
    custom: {
      type: CustomResource

      // optional
      options: {
        foo?: string
      }

      // optional
      extensions: 'ctm' // | 'other' | 'extensions'
    }
  }
}

// usage
const custom = $res('/model.ctm')

// force a .json file to use your custom loader
const custom = $res('/model.json', { as: 'custom' })

Aseprite / Tiled

I've provided optional loaders for Aseprite and Tiled. You can import and add them to a custom loader.

// src/loaders.ts
import aseprite from 'vite-plugin-excalibur-resources/loaders/aseprite'
import tiled from 'vite-plugin-excalibur-resources/loaders/tiled'

export default {
  aseprite,
  tiled,
}