diff --git a/packages/share-theme/scripts/build.ts b/packages/share-theme/scripts/build.ts index 39d24c962..8e822f1d4 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 56ba351e2..000000000 --- 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 = {}) { - 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 641f0dc58..de3e2941c 100644 --- a/packages/share-theme/src/templates/page.ejs +++ b/packages/share-theme/src/templates/page.ejs @@ -1,9 +1,5 @@ -<% - const { note, subRoot, assetPath, appPath } = locals; - let content = locals.content; -%>