minify interpreted templates

save bytes in init.js
This commit is contained in:
Azareal 2020-03-23 11:18:10 +10:00
parent a668cd296b
commit a3d6f1c844
3 changed files with 47 additions and 23 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"html/template" "html/template"
"io" "io"
"io/ioutil"
"log" "log"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -903,9 +904,31 @@ func loadTemplates(t *template.Template, themeName string) error {
} }
} }
template.Must(t.ParseFiles(tFiles...)) // TODO: Minify these
template.Must(t.ParseGlob("pages/*")) /*err = t.ParseFiles(tFiles...)
return nil if err != nil {
return err
}*/
for _, fname := range tFiles {
b, err := ioutil.ReadFile(fname)
if err != nil {
return err
}
s := tmpl.Minify(string(b))
name := filepath.Base(fname)
var tmpl *template.Template
if name == t.Name() {
tmpl = t
} else {
tmpl = t.New(name)
}
_, err = tmpl.Parse(s)
if err != nil {
return err
}
}
_, err = t.ParseGlob("pages/*")
return err
} }
func InitTemplates() error { func InitTemplates() error {

View File

@ -118,6 +118,7 @@ func (t *Theme) LoadStaticFiles() error {
return out return out
} }
t.ResourceTemplates.Funcs(fmap) t.ResourceTemplates.Funcs(fmap)
// TODO: Minify these
template.Must(t.ResourceTemplates.ParseGlob("./themes/" + t.Name + "/public/*.css")) template.Must(t.ResourceTemplates.ParseGlob("./themes/" + t.Name + "/public/*.css"))
// It should be safe for us to load the files for all the themes in memory, as-long as the admin hasn't setup a ridiculous number of themes // It should be safe for us to load the files for all the themes in memory, as-long as the admin hasn't setup a ridiculous number of themes

View File

@ -107,7 +107,7 @@ function notifyOnScriptW(name,complete,success) {
complete(); complete();
if(success!==undefined) success(); if(success!==undefined) success();
}).catch(e => { }).catch(e => {
console.log("Unable to get script name '"+name+"'",e); console.log("Unable to get '"+name+"'",e);
console.trace(); console.trace();
complete(e); complete(e);
}); });
@ -125,12 +125,12 @@ function loadScript(name,callback,fail) {
asyncGetScript(url) asyncGetScript(url)
.then(callback) .then(callback)
.catch(e => { .catch(e => {
console.log("Unable to get script '"+url+"'"); console.log("Unable to get '"+url+"'");
if(fname!=name) { if(fname!=name) {
asyncGetScript(iurl) asyncGetScript(iurl)
.then(callback) .then(callback)
.catch(e => { .catch(e => {
console.log("Unable to get script '"+iurl+"'",e); console.log("Unable to get '"+iurl+"'",e);
console.trace(); console.trace();
}); });
} }
@ -140,20 +140,20 @@ function loadScript(name,callback,fail) {
}); });
} }
function RelativeTime(date) {return date;} function RelativeTime(date) {return date}
function initPhrases(loggedIn, panel=false) { function initPhrases(member, acp=false) {
console.log("in initPhrases") console.log("initPhrases")
console.log("tmlInits",tmplInits) console.log("tmlInits",tmplInits)
let e = ""; let e = "";
if(loggedIn && !panel) e = ",status,topic_list,topic"; if(member && !acp) e = ",status,topic_list,topic";
else if(panel) e = ",analytics,panel"; // TODO: Request phrases for just one section of the control panel? else if(acp) e = ",analytics,panel"; // TODO: Request phrases for just one section of the cp?
else e = ",status,topic_list"; else e = ",status,topic_list";
fetchPhrases("alerts,paginator"+e) // TODO: Break this up? fetchPhrases("alerts,paginator"+e) // TODO: Break this up?
} }
function fetchPhrases(plist) { function fetchPhrases(plist) {
fetch("/api/phrases/?q="+plist, {cache:"no-cache"}) fetch("/api/phrases/?q="+plist,{cache:"no-cache"})
.then(resp => resp.json()) .then(resp => resp.json())
.then(data => { .then(data => {
console.log("loaded phrase endpoint data"); console.log("loaded phrase endpoint data");
@ -185,20 +185,20 @@ function fetchPhrases(plist) {
(() => { (() => {
runInitHook("pre_iife"); runInitHook("pre_iife");
let loggedIn = document.head.querySelector("[property='x-mem']")!=null; let member = document.head.querySelector("[property='x-mem']")!=null;
let panel = window.location.pathname.startsWith("/panel/"); let acp = window.location.pathname.startsWith("/panel/");
let toLoad = 1; let toLoad = 1;
// TODO: Shunt this into loggedIn if there aren't any search and filter widgets? // TODO: Shunt this into member if there aren't any search and filter widgets?
let q = (f) => { let q = (f) => {
toLoad--; toLoad--;
if(toLoad===0) initPhrases(loggedIn,panel); if(toLoad===0) initPhrases(member,acp);
if(f) throw("template function not found"); if(f) throw("tmpl func not found");
}; };
if(!panel) { if(!acp) {
toLoad += 2; toLoad += 2;
if(loggedIn) { if(member) {
toLoad += 3; toLoad += 3;
notifyOnScriptW("tmpl_topic_c_edit_post", () => q(!Tmpl_topic_c_edit_post)); notifyOnScriptW("tmpl_topic_c_edit_post", () => q(!Tmpl_topic_c_edit_post));
notifyOnScriptW("tmpl_topic_c_attach_item", () => q(!Tmpl_topic_c_attach_item)); notifyOnScriptW("tmpl_topic_c_attach_item", () => q(!Tmpl_topic_c_attach_item));
@ -209,16 +209,16 @@ function fetchPhrases(plist) {
} }
notifyOnScriptW("tmpl_notice", () => q(!Tmpl_notice)); notifyOnScriptW("tmpl_notice", () => q(!Tmpl_notice));
if(loggedIn) { if(member) {
fetch("/api/me/") fetch("/api/me/")
.then(resp => resp.json()) .then(resp => resp.json())
.then(data => { .then(data => {
console.log("loaded me endpoint data",data); console.log("me data",data);
me = data; me=data;
runInitHook("pre_init"); runInitHook("pre_init");
}); });
} else { } else {
me = {User:{ID:0,S:""},Site:{"MaxRequestSize":0}}; me={User:{ID:0,S:""},Site:{"MaxRequestSize":0}};
runInitHook("pre_init"); runInitHook("pre_init");
} }
})() })()