32 lines
932 B
TypeScript
32 lines
932 B
TypeScript
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<string> {
|
|
let input = file;
|
|
const regex = /\{\&([\s\S]*?)\&\}/gms;
|
|
|
|
let m;
|
|
while ((m = regex.exec(input)) !== null) {
|
|
if (m.index === regex.lastIndex) {
|
|
regex.lastIndex++;
|
|
}
|
|
|
|
const before = input.slice(0, m.index);
|
|
const expr = m[1];
|
|
const after = input.slice(m.index + m[0].length);
|
|
|
|
const result = eval(expr); // You can replace this with Function() for sandboxing
|
|
input = before + result + after;
|
|
|
|
// Reset regex lastIndex since we changed the string
|
|
regex.lastIndex = 0;
|
|
}
|
|
|
|
return input;
|
|
}
|
|
}
|