discourse/frontend/asset-processor/build.js
2026-03-30 13:13:58 +02:00

71 lines
1.7 KiB
JavaScript

// See: https://esbuild.github.io/plugins/#webassembly-plugin
const esbuild = require("esbuild");
const path = require("node:path");
const fs = require("node:fs");
let wasmPlugin = {
name: "wasm",
setup(build) {
build.onResolve({ filter: /\.wasm$/ }, (args) => {
if (args.namespace === "wasm-stub") {
return {
path: args.path,
namespace: "wasm-binary",
};
}
if (args.resolveDir === "") {
return;
}
return {
path: path.isAbsolute(args.path)
? args.path
: path.join(args.resolveDir, args.path),
namespace: "wasm-stub",
};
});
build.onLoad({ filter: /.*/, namespace: "wasm-stub" }, async (args) => {
return {
contents: `export { default } from ${JSON.stringify(args.path)};`,
};
});
build.onLoad({ filter: /.*/, namespace: "wasm-binary" }, async (args) => {
return {
contents: await fs.promises.readFile(args.path),
loader: "binary",
};
});
},
};
esbuild
.build({
logLevel: "warning",
bundle: true,
minify: false,
alias: {
path: "path-browserify",
url: "./url-polyfill",
"source-map-js": "source-map-js",
assert: "./noop",
fs: "./noop",
stream: "readable-stream",
"abort-controller": "abort-controller/dist/abort-controller",
},
banner: {
js: `var process = { "env": { "EMBER_ENV": "production" }, "cwd": () => "/" };`,
},
define: {
"import.meta.url": "'http://example.com'",
},
supported: { "from-base64": false },
external: [],
entryPoints: ["./transpiler.js"],
plugins: [wasmPlugin],
})
.then(() => {});