Files
obsidian-typst-pdf-export/esbuild.config.mjs
T
AlexKucera 18c0e1736c Removes unused sharp dependency
Removes the `sharp` dependency from the esbuild configuration.

This dependency was not being used, and its removal reduces the bundle size and improves build performance.
2025-09-21 16:02:32 +02:00

105 lines
2.4 KiB
JavaScript

import esbuild from "esbuild";
import process from "process";
import builtins from "builtin-modules";
import fs from "fs";
import path from "path";
const banner =
`/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
`;
const prod = (process.argv[2] === "production");
// Plugin to embed template files as base64 strings
const templateEmbedPlugin = {
name: 'template-embed',
setup(build) {
build.onResolve({ filter: /^template-embed:/ }, (args) => ({
path: args.path,
namespace: 'template-embed',
}));
build.onLoad({ filter: /.*/, namespace: 'template-embed' }, (args) => {
const templatesDir = './templates';
const templates = {};
try {
const files = fs.readdirSync(templatesDir);
for (const file of files) {
if (file.endsWith('.typ')) {
const filePath = path.join(templatesDir, file);
const content = fs.readFileSync(filePath, 'utf8');
templates[file] = Buffer.from(content, 'utf8').toString('base64');
}
}
} catch (error) {
console.warn('Warning: Could not read templates directory:', error.message);
}
const contents = `
export const embeddedTemplates = ${JSON.stringify(templates, null, 2)};
export function extractTemplate(templateName, targetPath) {
const templateContent = embeddedTemplates[templateName];
if (!templateContent) {
throw new Error(\`Template \${templateName} not found in embedded templates\`);
}
const decoded = Buffer.from(templateContent, 'base64').toString('utf8');
return decoded;
}
export function getAllTemplateNames() {
return Object.keys(embeddedTemplates);
}
`;
return {
contents,
loader: 'js',
};
});
},
};
const context = await esbuild.context({
banner: {
js: banner,
},
entryPoints: ["main.ts"],
bundle: true,
plugins: [templateEmbedPlugin],
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: "main.js",
minify: prod,
});
if (prod) {
await context.rebuild();
process.exit(0);
} else {
await context.watch();
}