Skip to content

Build your personal knowledge base with Trilium Notes

refactor(share): remove experiment to precompile EJS

Elian Doran54ffeeaed9c9
diff --git a/packages/share-theme/scripts/build.ts b/packages/share-theme/scripts/build.ts
index 39d24c9623..8e822f1d43 100644
--- a/packages/share-theme/scripts/build.ts
+++ b/packages/share-theme/scripts/build.ts
@@ -1,6 +1,5 @@
import fs from "node:fs";
import path from "node:path";
-import ejsPlugin from "./esbuild-ejs-plugin.js";
// import {fileURLToPath} from "node:url";
import dotenv from "dotenv";
@@ -58,9 +57,6 @@ async function runBuild() {
outdir: path.join(rootDir, "dist"),
format: "cjs",
target: ["chrome96"],
- plugins: [
- ejsPlugin()
- ],
loader: {
".png": "dataurl",
".gif": "dataurl",
diff --git a/packages/share-theme/scripts/esbuild-ejs-plugin.ts b/packages/share-theme/scripts/esbuild-ejs-plugin.ts
deleted file mode 100644
index 56ba351e25..0000000000
--- a/packages/share-theme/scripts/esbuild-ejs-plugin.ts
+++ /dev/null
@@ -1,85 +0,0 @@
-import { readFile } from 'fs/promises';
-import { compile, type Options } from 'ejs';
-import { resolve, dirname, basename } from 'path';
-
-export default function esbuildPluginEjs(options = {}) {
- return {
- name: 'ejs',
- setup(build) {
- build.onLoad({ filter: /\.ejs$/ }, async args => {
- const ejsOptions: Options = {
- ...options,
- client: true,
- strict: true,
- compileDebug: false
- }
-
- const contents: string[] = [];
- contents.push(`const includeMap = {}`);
- contents.push(`
- function __include(name, ...args) {
- return includeMap[name](...args);
- }
- `);
-
- let main;
-
- // Compile the subtemplates.
- const subtemplates = await collectTemplateTree(args.path);
- for (const [ path, subtemplate ] of Object.entries(subtemplates)) {
- const functionName = basename(path).split(".")[0];
- const isMain = (path === args.path);
-
- const generator = compile(subtemplate, ejsOptions);
- const functionOutput = generator.toString().split("\n");
-
- if (isMain) {
- functionOutput[0] = functionOutput[0].replace(/^function anonymous/, `module.exports = function`);
- } else {
- functionOutput[0] = functionOutput[0].replace(/^function anonymous/, `includeMap["${functionName}"] = function`);
- }
-
- // Inject include function.
- functionOutput[2] = `include = __include;\n${functionOutput[2]}`;
-
- if (isMain) {
- main = functionOutput.join("\n");
- } else {
- contents.push(functionOutput.join("\n"));
- }
- }
-
- // Compile the rest.
- if (!main) {
- throw new Error("Missing main entry point");
- }
- contents.push(main);
-
- return { contents: contents.join("\n"), loader: 'js' }
- })
- }
- }
-}
-
-
-const includeRegex = /<%-?\s*include\((['"`])(.+?)\1\s*(?:,[^)]+)?\)\s*-?%>/g;
-
-async function collectTemplateTree(filePath, seen: Record<string, string> = {}) {
- if (seen[filePath]) return;
-
- const source = await readFile(filePath, 'utf8');
- seen[filePath] = source;
-
- const dir = dirname(filePath);
-
- const matches = [...source.matchAll(includeRegex)];
- for (const match of matches) {
- const includePath = match[2];
-
- // Add .ejs extension if needed
- const resolvedPath = resolve(dir, includePath.endsWith('.ejs') ? includePath : includePath + '.ejs');
- await collectTemplateTree(resolvedPath, seen);
- }
-
- return seen;
-}
diff --git a/packages/share-theme/src/templates/page.ejs b/packages/share-theme/src/templates/page.ejs
index 641f0dc584..de3e2941c2 100644
--- a/packages/share-theme/src/templates/page.ejs
+++ b/packages/share-theme/src/templates/page.ejs
@@ -1,9 +1,5 @@
<!DOCTYPE html>
<html lang="en">
-<%
- const { note, subRoot, assetPath, appPath } = locals;
- let content = locals.content;
-%>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">