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

gltf-loader

v1.0.0

Published

A Webpack loader for glTF files.

Downloads

693

Readme

gltf-loader

A comprehensive Webpack loader for glTF files.

v5 v2.0

Rationale

TL;DR: I wanted my glTF assets loaded the same as all my other image assets. :upside_down_face:

By design, glTF files are comprised of multiple assets representing the various components of a 3D scene. Typically, these files contain external references to binary files for geometries and animations, and image files for textures.

When using a module bundler such as Webpack, image assets can be optimized, versioned, and then referenced in JavaScript by importing them directly. However, if you're using something like Three.js to import glTF files, its assets are requested during runtime and thus won't have any of the optimizations or versioning applied to them in the way they would if they were handled by Webpack.

This loader fixes that problem by iterating through glTF JSON data and loading its assets automatically, replacing any uri references with the asset's final output URI.

You can read more about the glTF 2.0 specification here.

Installation

npm install gltf-loader

Configuration

All loader options are typed, documented, and available in the declaration file here.

webpack.config.js

module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /\.(gltf)$/,
        loader: "gltf-loader",
        /**
         * @type {import("gltf-loader").GLTFLoaderOptions}
         */
        options: {
          // ...
        },
      },
      {
        test: /\.(bin|png|jpe?g)$/,
        type: "asset/resource",
      },
    ],
  },
};

As a path

By default, the loader injects the glTF file path during import. This is especially useful when using Three.js:

import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
import myModel from "../assets/my-model.gltf"; // e.g. /dist/static/media/my-model.a1b2c3d4.gltf

const loader = new GLTFLoader();
loader.load(myModel, (gltf) => {
  // ...
});

As JSON data

Alternatively, you can set the loader option inline: true if you wish to import the raw JSON data instead:

import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
import myModel from "../assets/my-model.gltf"; // JSON data

const loader = new GLTFLoader();
loader.parse(JSON.stringify(myModel), window.location.origin + "/", (gltf) => {
  // ...
});

TypeScript

For TypeScript users, adding the following module declaration will fix any "cannot find module" errors:

modules.d.ts

declare module "*.gltf" {
  const content: string;
  export default content;
}

Note that if you supply the inline: true config option, you would want to change the above declaration to something like the following:

declare module "*.gltf" {
  const content: Record<string, unknown>; // Or a glTF interface if you have/need one
  export default content;
}

tsconfig.json

  {
    "compilerOptions": {
      // ...
    }.
    "include": [
+     "modules.d.ts",
      "foo.d.ts",
      "bar.d.ts
    ],
    "exclude": []
  }