Closed eliot-akira closed 3 months ago
I'm not 100% sure if this would fly on wp.org so it might have to be kept as a premium exclusive feature.
I think you're right. I wanted to write down the idea since I'd considered it before, but as I thought it through, I realized I wouldn't be comfortable with any plugin that did what I described. It's too powerful. If I do experiment with this, I'll make it a private repo and premium add-on.
A compiler that generates HTML/CSS/JS files would be fine, though it kind of defeats the purpose of dynamic HTML - which might be better cached in the database, as the Cache
tag does.
That leaves CSS and JS, which would benefit from being compiled/saved/uploaded as minifed files cachable by the browser. For CSS it could use CSS variables for dynamic values, instead of Sass variables.
Currently, the template engine works as an interpreter to render templates. It walks the abstract syntax tree of the parsed template, running it as a program (somewhat like Lisp). This template/program is what generates HTML, runs PHP functions for dynamic tags, and enqueues CSS and JavaScript files.
It may be possible to implement a new strategy for the engine, that works as a compiler.
Instead of interpreting each node at run time (on page load), the template can be converted to the target languages (HTML/CSS/JS/PHP) at compile time when it's saved in the admin edit screen.
An advantage of the compiler approach is that the generated files can be cached, both on the server (OpCache) and on the frontend (browser cache).
A disadvantage is the added complexity of the template life cycle, saving and loading files. There's also a question whether dynamically generating PHP files is seen as reasonable/acceptable behavior for a plugin; some hosting providers may forbid it as dark magic. It would be an opt-in and experimental feature, like "turbo mode".