Skip to content

Commit 581ad1b

Browse files
committed
create plugin
1 parent 35d3490 commit 581ad1b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

lib/src/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { Plugin, PluginBuild } from "esbuild";
2+
import fs from "node:fs";
3+
import path from "node:path";
4+
5+
/** Plugin to load `.glsl` files as minified strings */
6+
export const raw: () => Plugin = () => ({
7+
/** generate randmo name to avoid collision among the plugins */
8+
name: `raw-${(Date.now() * Math.random()).toString(36).slice(0, 8)}`,
9+
setup(build: PluginBuild) {
10+
build.onResolve({ filter: /\?raw$/ }, args => {
11+
const filePath = args.path;
12+
return {
13+
path: filePath,
14+
pluginData: path.resolve(args.resolveDir, filePath).replace(/\?raw$/, ""),
15+
namespace: "raw",
16+
};
17+
});
18+
build.onLoad({ filter: /\?raw$/, namespace: "raw" }, async args => {
19+
return {
20+
contents: fs.readFileSync(args.pluginData, "utf8"),
21+
loader: "text",
22+
};
23+
});
24+
},
25+
});

0 commit comments

Comments
 (0)