UNPKG

@pizca/esbuild-webgl

Version:

An esbuild plugin to import WebGL shader files as strings, with optional comment stripping for cleaner bundles.

31 lines (30 loc) 1.12 kB
import fs from 'fs'; /** * An esbuild plugin to load WebGL shader files (`.vert`, `.frag`, `.glsl`) as string modules. * * This plugin can optionally remove comments from the shader source code before exporting. * * @param {WebGLPluginOptions} [options] - Plugin options. * @returns {import('esbuild').Plugin} The configured esbuild plugin instance. */ export default function webglPlugin(options = {}) { const { stripComments = false } = options; return { name: 'webgl', setup(build) { build.onLoad({ filter: /\.(vert|frag|glsl)$/ }, async (args) => { return fs.promises.readFile(args.path, 'utf-8').then((value) => { if (stripComments) { value = value .replace(/\/\/.*$/gm, '') .replace(/\/\*[\s\S]*?\*\//g, ''); } return { contents: `export default ${JSON.stringify(value)};`, loader: 'js', }; }); }); } }; }