gulp-structify
TypeScript icon, indicating that this package has built-in type declarations

1.3.0 • Public • Published

gulp-structify

Generates WebGL-compatible structs and struct buffers from a template file.

NPM

Install

npm install gulp-structify --save

Create a template file:

Suppose we want to create a Vec2 struct backed by a Float32Array. We start with a minimal template file called Vec2.template.ts:

import {Template} from "gulp-structify/template";
 
/**
 * A two-dimensional vector with (x,y) components.
 */
class Vec2 extends Template<Float32Array> {
    /**
     * The X component of this Vec2.
     */
    x: number;
    /**
     * The Y component of this Vec2.
     */
    y: number;
}

Note: the template should not include a constructor or any static methods.

Add methods to the template

Now suppose we want to add a dot method to our Vec2 struct. We do this by adding the method to our template:

// ...
class Vec2 extends Template<Float32Array> {
    // ... 
 
    /**
     * Computes the dot product of this Vec2 with the other Vec2.
     */
    dot(other: Vec2): number {
        return this.x * other.x + this.y * other.y;
    }
}
 

Note: gulp-structify automatically generates the following methods:

  • set(other: this)
  • setScalar(k: number)
  • add(other: this)
  • subtract(other: this)
  • mulScalar(k: number)
  • divScalar(k: number)
  • equals(other: this)
  • equalsScalar(k: number)
  • epsilonEquals(other: this, e: number)
  • epsilonEqualsScalar(k: number, e: number)
  • toString()

Create gulp task

var gulp = require("gulp");
var rename = require("gulp-rename");
var structify = require("gulp-structify");
 
// Directory where template file is located
var directory = "./"; 
 
gulp.task("structify", function () {
    // Search for files ending in .template.ts
    return gulp.src(directory + "*.template.ts")
        // Generate struct file
        .pipe(structify())
        // Remove ".template" from filename
        .pipe(rename(function(p) {
            let base = p.basename;
            let length = base.length - ".template".length;
            p.basename = base.substr(0, length);
        })) 
        // Output to same directory to preserve imports
        .pipe(gulp.dest(directory));
});

Run gulp task

gulp structify

Examples

Template Output Usage
point.template.ts point.ts
vec2.template.ts vec2.ts
color.template.ts color.ts

Package Sidebar

Install

npm i gulp-structify

Weekly Downloads

12

Version

1.3.0

License

ISC

Last publish

Collaborators

  • wjheesen0