PDA

View Full Version : [CLOSED] Minification Attempt



michaeld
Jul 20, 2013, 10:08 AM
I've started to integrate YUI minification into my application. I've been able to minify all my inline scripts and styles that lay outside of ext.net controls but I'm wondering how I'd go about minifying the stream output of an XScript sections.

Vladimir
Jul 20, 2013, 3:06 PM
Sorry, the question is not clear for me. What exactly problems do you have. Script of XScript widget is located in ScriptBlock string property
Just modify that property as you wish. Please note that you have to modify it before PreRender stage

Or create own inheritor of XScript control with OnPreRender override


protected override void OnPreRender(EventArgs e)
{
if (!this.DesignMode)
{
// minify script here
this.Controls.Add(new LiteralControl(TokenUtils.ReplaceRawToken(TokenUti ls.ParseTokens(this.ScriptBlock, this))));
this.ScriptBlock = "";
}


base.OnPreRender(e);
}

michaeld
Jul 23, 2013, 6:18 AM
Wouldn't this occur before Ext.Net processed the #{controlID} references?

I would need to process the text after.

Please correct me if I'm wrong.

michaeld
Jul 23, 2013, 7:31 AM
The other problem with your solution is that I have to do this for each and every XScript. I'm going to continue looking for another way. If I can't find one at the adapter or filter level, I'll branch.

Vladimir
Jul 23, 2013, 1:31 PM
If you need to minify after #{} procesinng then I can suggest inheritor of XScript control with the following override


protected override void OnPreRender(EventArgs e)
{
if (!this.DesignMode)
{
string script = TokenUtils.ReplaceRawToken(TokenUtils.ParseTokens( this.ScriptBlock, this));
// minify script here
this.Controls.Add(new LiteralControl(script));
this.ScriptBlock = "";
}


base.OnPreRender(e);
}


After that you need to use own inheritor XScript class instead Ext.Net XScript

I am not sure why do you need to minimify scripts inside XScript control (big inline scripts is not good idea imho). If script so big then move it to script files and use IIS compression.
All required controls references can be passed to special init function

For example,

Script file


Ext.ns("MyNamespace");
MyNamespace.Block1 = {
init : function (control1, control2) {
this.control1 = control1;
this.control2 = control2;
},

myVeryUsefulFn1 : function () {
this.control1.show();
},

myVeryUsefulFn2 : function () {
this.control2.hide();
}
};


In the page use


<ext:XScript runat="server">
<script src="linkToMyFile.js"></script>
<script>
MyNamespace.Block1.init(#{Window1}, #{Window2});
</script>
</ext:XScript>


Using function form that file


<ext:Button runat="server" Text="Show" Handler="MyNamespace.Block1.myVeryUsefulFn1" />

michaeld
Jul 24, 2013, 1:50 AM
Everything you posed is good advise. Though, most of the inlines are 10-20 lines; however, there are many. Most of the inlines were coded to relate to the controls that they represent for management cohesion.

Originally I had a lot of code similar to the way you described whereby I passed the control names to the javascript object. In cases where the clientside code is big, I do do something like your strategy. But once you started making XScripts translate IDs in 2.0, I revised much of the inefficient code that passed controls to the inlines during Render listeners (which was probably a bad model for clientside performance). I'll eventually use an alternate ID mode to eliminate the need for XScripts as well, but another day.

In any case, your strategy to create an inherited class and actually parse the tokens in the OnPreRender would work. The only concern I'd have to watch for is that you don't change the code inside OnPreRender in XScript in future revs.

Still, I took a scratch at a solution last night and I think I came up with a better option that actually allows me to catch script outside of XScripts as well as the XScripts. I wrote a ControlAdapter for LiteralControl that I can turn on and off in a single place. it also allows me an chance to minify html as well. Performance is reasonable. Overall, I cut 15% size at a 1% cpu cost. IIS compression is already on. The 15% savings is post-compression both times. Since bandwidth is our first-gen concern on launch, it's significant enough to call a win for now. There are a lot of things I want to get rid of from the original 1.0 implementation of my project like remove viewstate but I have to take the small wins where I can get them. And right now bandwidth is the present concern as I cannot cdn extnet until 2.3.

Thank you for your solution. I may still use it if current strategy becomes a concern.

Daniil
Jul 24, 2013, 12:11 PM
Do you mind we close the thread?

If you have some new information to share or ask, you could update this thread despite it is closed.

michaeld
Jul 25, 2013, 5:23 AM
For sure, yeah.