From 55a699c7fd34c3b4b954a44d2557ef04f5a72f88 Mon Sep 17 00:00:00 2001 From: sophie Date: Fri, 2 Aug 2024 05:13:09 +0300 Subject: [PATCH] add compiletimejs --- src/plugins/compile-time-js.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/plugins/compile-time-js.ts diff --git a/src/plugins/compile-time-js.ts b/src/plugins/compile-time-js.ts new file mode 100644 index 0000000..778b1be --- /dev/null +++ b/src/plugins/compile-time-js.ts @@ -0,0 +1,25 @@ +import { Plugin } from ".."; + +export default class CompileTimeJS extends Plugin { + name = "compile-time-js"; + rewriteTriggers = ["html", "*"] + longLasting = false; + build = undefined; + + async rewriteFile(file: string, filePath: string): Promise { + let input = file; + const regex = /{&(.+)&}/gm; + + let m; + + while ((m = regex.exec(file)) !== null) { + if (m.index === regex.lastIndex) { + regex.lastIndex++; + } + + input = file.slice(0, m.index) + eval(m[1]) + file.slice(m.index + m[0].length); + } + + return input; + } +} \ No newline at end of file