wesl-plugin
Version:
[](https://www.npmjs.com/package/wesl-plugin) [](https://wesl-lang.dev/)
75 lines (72 loc) • 2.66 kB
JavaScript
import { t as resolve } from "./import-meta-resolve-CUFqnZwT.js";
import path from "node:path";
import { link, noSuffix } from "wesl";
import url from "node:url";
//#region src/extensions/LinkExtension.ts
const linkBuildExtension = {
extensionName: "link",
emitFn: emitLinkJs
};
/** Emit a JavaScript LinkParams structure, ready for linking at runtime. */
async function emitLinkJs(baseId, api) {
const { resolvedRoot, tomlDir } = await api.weslToml();
const weslSrc = await api.weslSrc();
const rootModuleName = noSuffix(await api.weslMain(baseId));
const debugWeslRoot = path.relative(tomlDir, resolvedRoot).replaceAll(path.sep, "/");
const autoDeps = await api.weslDependencies();
const sanitizedDeps = autoDeps.map((dep) => dep.replaceAll("/", "_"));
const bundleImports = autoDeps.map((p, i) => `import ${sanitizedDeps[i]} from "${p}";`).join("\n");
const paramsName = `link${path.basename(rootModuleName).replace(/\W/g, "_")}Config`;
const linkParams = {
rootModuleName,
weslSrc,
debugWeslRoot
};
const libsStr = `libs: [${sanitizedDeps.join(", ")}]`;
return `
${bundleImports}
export const ${paramsName} = ${`{
${serializeFields(linkParams)},
${libsStr},
}`};
export default ${paramsName};
`;
}
function serializeFields(record) {
return Object.entries(record).map(([k, v]) => ` ${k}: ${JSON.stringify(v, null, 2)}`).join(",\n");
}
//#endregion
//#region src/extensions/StaticExtension.ts
/**
* a wesl-js ?static build extension that statically links from the root file
* and emits a JavaScript file containing the linked wgsl string.
*
* use it like this:
* import wgsl from "./shaders/app.wesl?static";
*
* or with conditions, like this:
* import wgsl from "../shaders/foo/app.wesl MOBILE=true FUN SAFE=false ?static";
*/
const staticBuildExtension = {
extensionName: "static",
emitFn: emitStaticJs
};
/** Emit a JavaScript file containing the wgsl string */
async function emitStaticJs(baseId, api, conditions) {
const { resolvedRoot, tomlDir } = await api.weslToml();
const parentModule = url.pathToFileURL(path.join(tomlDir, "wesl.toml")).toString();
const futureLibs = (await api.weslDependencies()).map((d) => resolve(d, parentModule)).map((f) => import(f));
const libs = (await Promise.all(futureLibs)).map((m) => m.default);
return `
export const wgsl = \`${(await link({
weslSrc: await api.weslSrc(),
rootModuleName: noSuffix(await api.weslMain(baseId)),
debugWeslRoot: path.relative(tomlDir, resolvedRoot).replaceAll(path.sep, "/"),
libs,
conditions
})).dest}\`;
export default wgsl;
`;
}
//#endregion
export { linkBuildExtension, staticBuildExtension };