Compile wasm into plugin
This commit is contained in:
parent
39bb81c553
commit
e712164e89
4 changed files with 106 additions and 41 deletions
|
|
@ -1,8 +1,9 @@
|
|||
import esbuild from "esbuild";
|
||||
import process from "process";
|
||||
import builtins from "builtin-modules";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import path from "node:path";
|
||||
import fs from "node:fs";
|
||||
import { wasmPack } from "esbuild-plugin-wasm-pack";
|
||||
|
||||
const banner = `/*
|
||||
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
|
||||
|
|
@ -12,64 +13,6 @@ if you want to view the source, please visit the github repository of this plugi
|
|||
|
||||
const prod = process.argv[2] === "production";
|
||||
|
||||
const context = await esbuild.context({
|
||||
banner: {
|
||||
js: banner,
|
||||
},
|
||||
entryPoints: ["main.ts"],
|
||||
bundle: true,
|
||||
external: [
|
||||
"obsidian",
|
||||
"electron",
|
||||
"@codemirror/autocomplete",
|
||||
"@codemirror/collab",
|
||||
"@codemirror/commands",
|
||||
"@codemirror/language",
|
||||
"@codemirror/lint",
|
||||
"@codemirror/search",
|
||||
"@codemirror/state",
|
||||
"@codemirror/view",
|
||||
"@lezer/common",
|
||||
"@lezer/highlight",
|
||||
"@lezer/lr",
|
||||
...builtins,
|
||||
],
|
||||
format: "cjs",
|
||||
target: "es2018",
|
||||
logLevel: "info",
|
||||
sourcemap: prod ? false : "inline",
|
||||
treeShaking: true,
|
||||
outfile: "build/main.js",
|
||||
minify: prod,
|
||||
plugins: [
|
||||
{
|
||||
name: "post-compile",
|
||||
setup(build) {
|
||||
build.onEnd((result) => {
|
||||
if (result.errors.length === 0) {
|
||||
copyFiles(
|
||||
["manifest.json", "versions.json", ".hotreload"],
|
||||
"/mnt/c/Users/Andras/Desktop/test/test/.obsidian/plugins/my-plugin"
|
||||
);
|
||||
|
||||
copyFiles(
|
||||
"build",
|
||||
"/mnt/c/Users/Andras/Desktop/test/test/.obsidian/plugins/my-plugin"
|
||||
);
|
||||
}
|
||||
});
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
if (prod) {
|
||||
await context.rebuild();
|
||||
process.exit(0);
|
||||
} else {
|
||||
await context.watch();
|
||||
}
|
||||
|
||||
async function copyFiles(sourceDir, destinationDir) {
|
||||
try {
|
||||
await fs.promises.mkdir(destinationDir, { recursive: true });
|
||||
|
|
@ -102,3 +45,95 @@ async function copyFiles(sourceDir, destinationDir) {
|
|||
console.error("Error copying files:", err);
|
||||
}
|
||||
}
|
||||
|
||||
let wasmPlugin = {
|
||||
name: "wasm",
|
||||
setup(build) {
|
||||
// Resolve ".wasm" files to a path with a namespace
|
||||
build.onResolve({ filter: /\.wasm$/ }, (args) => {
|
||||
if (args.resolveDir === "") {
|
||||
return; // Ignore unresolvable paths
|
||||
}
|
||||
return {
|
||||
path: path.isAbsolute(args.path)
|
||||
? args.path
|
||||
: path.join(args.resolveDir, args.path),
|
||||
namespace: "wasm-binary",
|
||||
};
|
||||
});
|
||||
|
||||
// Virtual modules in the "wasm-binary" namespace contain the
|
||||
// actual bytes of the WebAssembly file. This uses esbuild's
|
||||
// built-in "binary" loader instead of manually embedding the
|
||||
// binary data inside JavaScript code ourselves.
|
||||
build.onLoad(
|
||||
{ filter: /.*/, namespace: "wasm-binary" },
|
||||
async (args) => ({
|
||||
contents: await fs.promises.readFile(args.path),
|
||||
loader: "binary",
|
||||
})
|
||||
);
|
||||
},
|
||||
};
|
||||
const context = await esbuild.context({
|
||||
banner: {
|
||||
js: banner,
|
||||
},
|
||||
entryPoints: ["main.ts"],
|
||||
bundle: true,
|
||||
external: [
|
||||
"obsidian",
|
||||
"electron",
|
||||
"@codemirror/autocomplete",
|
||||
"@codemirror/collab",
|
||||
"@codemirror/commands",
|
||||
"@codemirror/language",
|
||||
"@codemirror/lint",
|
||||
"@codemirror/search",
|
||||
"@codemirror/state",
|
||||
"@codemirror/view",
|
||||
"@lezer/common",
|
||||
"@lezer/highlight",
|
||||
"@lezer/lr",
|
||||
...builtins,
|
||||
],
|
||||
format: "cjs",
|
||||
target: "es2020",
|
||||
logLevel: "info",
|
||||
sourcemap: prod ? false : "inline",
|
||||
treeShaking: false,
|
||||
outfile: "build/main.js",
|
||||
minify: prod,
|
||||
plugins: [
|
||||
wasmPlugin,
|
||||
wasmPack({
|
||||
target: "web",
|
||||
path: "../backend/sync_wasm",
|
||||
}),
|
||||
{
|
||||
name: "post-compile",
|
||||
setup(build) {
|
||||
build.onEnd((result) => {
|
||||
if (result.errors.length === 0) {
|
||||
copyFiles(
|
||||
["manifest.json", "versions.json", ".hotreload"],
|
||||
"/mnt/c/Users/Andras/Desktop/test/test/.obsidian/plugins/my-plugin"
|
||||
);
|
||||
|
||||
copyFiles(
|
||||
"build",
|
||||
"/mnt/c/Users/Andras/Desktop/test/test/.obsidian/plugins/my-plugin"
|
||||
);
|
||||
}
|
||||
});
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
if (prod) {
|
||||
await context.rebuild();
|
||||
process.exit(0);
|
||||
} else {
|
||||
await context.watch();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue