Instead of relying on JSON.parse() at runtime, this plugin converts your 16x16 sprite data into static TypeScript files. This allows your IDE to provide autocomplete for your assets and ensures your data structure is validated at compile time.
// plugins/TypeScriptExporter.js
export const TypeScriptExporter = {
id: 'ts-exporter',
// Transforms internal pixel grid to a TypeScript source string
generateExport(spriteName, pixelData) {
const typeDefinition = `export interface SpriteData { name: string; pixels: number[]; }`;
const dataConstant = `export const ${spriteName}: SpriteData = {
name: "${spriteName}",
pixels: ${JSON.stringify(pixelData)}
};`;
return `${typeDefinition}\n\n${dataConstant}`;
}
};
TypeScriptExporter.generateExport('hero_walk', pixels).src/assets/hero_walk.ts.import { hero_walk } from './assets/hero_walk';
console.log(hero_walk.pixels); // Type-safe and immediately available
You might want to keep the raw JSON for the editor to load/save while working, and use the TypeScript exporter only for the final build pipeline. This keeps your editor fast and iterative while making your runtime lean and performant.