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

@mszu/pixi-ssr-shim

v1.0.2

Published

A shim that allows PixiJS to be imported (but not run) in a server-side-rendering context

Downloads

74

Readme

@mszu/pixi-ssr-shim

Screenshot showing SvelteKit app with PixiJS

What is this?

A basic shim that stubs out enough browser context (window, document, etc.) to allow the PixiJS library to be imported in Node without errors.

Note that this does not let you USE Pixi from Node (e.g. to render images on the server), just to import it.

Why would I want this?

You're trying to use PixiJS with a framework like SvelteKit that does server-side rendering, but you're getting errors like ReferenceError: self is not defined during the build or starting the preview server.

How do I use this?

See https://github.com/mszu/svelte-kit-pixi-sample for a complete example.

  1. Add this library as a dependency (npm i @mszu/pixi-ssr-shim)
  2. Import it before your first Pixi import
// Sample SvelteKit script block
//
<script>
  import '@mszu/pixi-ssr-shim'; // <----------------- IMPORTANT
  import { Application, Sprite, Texture } from 'pixi.js';
  import { onMount } from 'svelte';
  
  let app;
  
  onMount(async () => {
    app = new Application({
      width: window.innerWidth,
      height: window.innerHeight,
      resolution: 1,
      backgroundColor: 0x1099bb,
    });

    document.body.appendChild(app.view);

    const sprite = new Sprite(Texture.WHITE);
    sprite.tint = 0xff0000;
    sprite.width = sprite.height = 100;
    sprite.x = sprite.y = 100;

    app.stage.addChild(sprite);
  });
</script>

Important Notes

This is kind of a hacky work-around since it depends on the framework (like SvelteKit) generating output code which imports the shim before pixi.js or any of the @pixi/... modules. Putting it before the Pixi imports in your own code is important, but even then the SvelteKit/Vite/rollup/esbuild configuration might decide to hoist things around, or inline them -- it's a bit of a black box.

For SvelteKit specifically:

  • Make sure not to call or instantiate anything from Pixi outside of an onMount(...) handler.
  • Don't import this shim (or the Pixi modules) in a <script context="module"> but then use Pixi in a separate <script> tag -- this reliably outputs imports in the wrong order.
  • To debug, look at the top of the file .svelte-kit/output/server/app.js after running svelte-kit build. The imports are near the top -- make sure the shim comes before anything Pixi.