tag:help-archives.hannonhill.com,2010-02-09:/discussions/how-do-i/10607-minify-cssjs-during-publishCascade CMS: Discussion 2014-02-05T19:55:02Ztag:help-archives.hannonhill.com,2010-02-09:Comment/312089112014-01-20T14:32:36Z2014-01-20T14:32:36ZMinify CSS/JS during publish?<div><p>Hi Joel,</p>
<p>I believe you may be able to accomplish this using a <a href=
"http://www.hannonhill.com/kb/Publish-Triggers/">custom publish
trigger</a>. Off-hand, I think this plugin would need to:</p>
<ul>
<li>Check the file name for <code>.css</code> or
<code>.js</code></li>
<li>Read the asset and minify it's contents using a third party
library</li>
<li>Upload a minified version of the file using the (S)FTP
Destination information provided by the publish trigger.</li>
</ul>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/312089112014-01-20T16:45:30Z2014-01-20T17:30:42ZMinify CSS/JS during publish?<div><p>Is there any JavaDoc on the publish API available? I see from
the sample that there is a class
com.cms.publish.PublishTriggerInformation, but I don't see anything
to tell me what members that class contains.</p>
<p>I also don't see how to get an instance of
com.hannonhill.cascade.api.asset.home.File so that I can get the
contents, or how to get an instance of
com.hannonhill.cascade.api.asset.admin.Transport to send the
modified data along. I'm assuming those are the right classes I
should be looking for instances of?</p></div>Joel Goguentag:help-archives.hannonhill.com,2010-02-09:Comment/312089112014-01-20T20:34:52Z2014-01-20T20:34:52ZMinify CSS/JS during publish?<div><p>Hi Joel,</p>
<p>Are you using Eclipse by chance? When you import the project you
should be able to view these classes by opening up Referenced
Libraries and expanding the jars.</p>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/312089112014-01-20T21:31:06Z2014-01-20T21:31:06ZMinify CSS/JS during publish?<div><p>I am using Eclipse, but I didn't see anything useful. I found
how to get the name of the file and the transport ID, but I didn't
see how to get file contents or the actual Transport implementation
being used. For example, I was expecting something like a static
Transport.getInstanceById(String) or a static method to get a File
instance, but nothing jumped out at me from either JAR.</p></div>Joel Goguentag:help-archives.hannonhill.com,2010-02-09:Comment/312089112014-01-21T13:30:21Z2014-01-21T13:30:21ZMinify CSS/JS during publish?<div><p>Hi Joel,</p>
<p>My apologies for the inconvenience regarding documentation on
these public APIs.</p>
<p>Let me check with the developers to see if we have any
additional documentation or information that may be able to help
you out.</p>
<p>In the meantime, you may be able to follow <a href=
"https://github.com/hannonhill/Webservices-Java-Sample-Project">the
instructions on this sample Web Services Java project</a> to
generate the WSDL stubs, which may help shed some light on what
methods and properties are available for assets.</p>
<p>Also, <a href=
"https://github.com/hannonhill/Cascade-Web-Services-Examples/tree/master/java/hannonhill-test-cases">
these Web Services test examples</a> may be able to help point you
in the right direction in terms of accessing Cascade assets.
Specifically, check out <a href=
"https://github.com/hannonhill/Cascade-Web-Services-Examples/blob/master/java/hannonhill-test-cases/TestFile.java">
TestFile.java</a> which has an example of using the
<code>getData</code> and <code>getText</code> methods to obtain the
File's content. And <a href=
"https://github.com/hannonhill/Cascade-Web-Services-Examples/blob/master/java/hannonhill-test-cases/TestTransport.java">
TestTransport.java</a> has examples of the various getters and
setters for Transports.</p>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/312089112014-01-24T16:20:09Z2014-01-24T16:20:09ZMinify CSS/JS during publish?<div><p>Hi Ryan,</p>
<p>Have you heard anything from the developers? I don't see
anything in the JARs I can get at or in the projects you pointed at
that shows me how to take a file ID (which I think I fetch using
com.cms.publish.PublishTriggerInformation#getEntityId()) and get
the actual File implementation itself. Am I even looking for an
implementation of com.hannonhill.cascade.api.asset.home.File or
should I be looking for something else?</p>
<p>Similarly for a Transport, I can get the transport ID with
com.cms.publish.PublishTriggerInformation#getTransportId() but I
don't see how to get an implementation of
com.hannonhill.cascade.api.asset.admin.Transport (assuming that's
the right Interface to be looking for).</p></div>Joel Goguentag:help-archives.hannonhill.com,2010-02-09:Comment/312089112014-01-24T19:32:30Z2014-01-24T19:32:30ZMinify CSS/JS during publish?<div><p>Hi Joel,</p>
<p>Unfortunately, there really isn't any public documentation other
than the various samples we provide.</p>
<p>That being said, I did find a sample Publish Trigger in our
private repository that I believe would give you a very good
starting point. It obtains the Transport information as well as
reads a Page. In your case, you would want to read a File and use
<code>File.getText()</code> to obtain the contents of the file in
plain-text so you can pass it through a minifying library.</p>
<p>I don't see a public link to this sample trigger, to I went
ahead and <a href=
"https://gist.github.com/rgriffith/28ca51a9f24d6d285d21">created a
gist on Github</a> for you with the source code.</p>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/312089112014-01-24T19:59:34Z2014-01-24T19:59:34ZMinify CSS/JS during publish?<div><p>Thanks for the code! I'm out on training all next week, but I'll
give that a try as soon as I can. It looks like the readAsset()
method in there is the key to everything I was missing.</p></div>Joel Goguentag:help-archives.hannonhill.com,2010-02-09:Comment/312089112014-01-24T21:30:07Z2014-01-24T21:30:07ZMinify CSS/JS during publish?<div><p>Not a problem at all. Again, my apologies for not being able to
provide you with documentation, but I'm glad we were able to
provide you with an example.</p>
<p>I'll make a note to follow up the following week to see how
things are going if we don't hear from you.</p>
<p>Have a great day!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/312089112014-01-28T15:27:49Z2014-01-28T15:27:52ZMinify CSS/JS during publish?<div><p>So, I'm getting a Hibernate exception:<br></p>
<pre>
2014-01-28 11:12:57,621 ERROR [LazyInitializationException] : could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
at com.hannonhill.cascade.model.dom.BlobEntity$$EnhancerByCGLIB$$56ae2579.getData()
at com.hannonhill.cascade.api.adapters.FileAPIAdapter.getData(FileAPIAdapter.java:44)
at ca.unb.cascade.triggers.publish.MinifyOnPublish.invoke(MinifyOnPublish.java:67)
at com.hannonhill.cascade.model.publish.callback.TriggerCallbackImpl.execute(TriggerCallbackImpl.java:70)
at com.hannonhill.publish.Publisher.fireTriggers(Publisher.java:659)
at com.hannonhill.publish.Publisher.access$1500(Publisher.java:69)
at com.hannonhill.publish.Publisher$2.run(Publisher.java:684)
at com.hannonhill.publish.DelegateRunner$1.run(DelegateRunner.java:85)
at java.lang.Thread.run(Unknown Source)
</pre>
<p>I've implemented readAsset() and the required IdentifierImpl
class exactly as it is in the Gist you linked to. The line in my
code referenced in the stack trace is just trying to get the file
data:</p>
<pre>
File file = (File)readAsset(publishInfo.getEntityId(), EntityTypes.TYPE_FILE);
...
InputStreamReader in = new InputStreamReader(new StringBufferInputStream(new String(file.getData())), CHARSET); // Exception triggered here
</pre>
<p>It looks like the Exception is triggered by trying to get the
file data; if I set up something silly like this, the Exception is
triggered on this line instead:<br></p>
<pre>
int initialSize = file.getData().length;
</pre>
<p>A quick search for the string
"org.hibernate.LazyInitializationException: could not initialize
proxy - no Session" suggests that the Hibernate session is already
closed when I try to read the byte array from the 'file' object.
Any hints on how I can move forward and get the file data?</p>
<p>Also, the Gist you posted is reading a file from the destination
and posting a new file. I'm trying to modify the data of the file
before transit, but not update what Cascade has in its database. Do
I have to fetch the Transport instance and send the data over
myself, or do I just give the data back to the Cascade publish
process somehow?</p></div>jgoguentag:help-archives.hannonhill.com,2010-02-09:Comment/312089112014-01-28T16:08:13Z2014-01-28T16:08:13ZMinify CSS/JS during publish?<div><p>Hi Joel,</p>
<blockquote>
<p>It looks like the Exception is triggered by trying to get the
file data; if I set up something silly like this, the Exception is
triggered on this line instead:</p>
</blockquote>
<p>Have you tried using <code>file.getText()</code> by chance? I
believe the sample DocX trigger is a little dated, prior to the
<code>File.getText()</code> method. This method should return the
contents of the file as a String so you can pass it through your
minifier.</p>
<blockquote>
<p>Also, the Gist you posted is reading a file from the destination
and posting a new file. I'm trying to modify the data of the file
before transit, but not update what Cascade has in its database. Do
I have to fetch the Transport instance and send the data over
myself, or do I just give the data back to the Cascade publish
process somehow?</p>
</blockquote>
<p>What I was thinking is you obtain the Transport/Destination
information and the full source contents of the file that is being
published, minify the contents, and manually push a minified
version of the file to the server. So you would end up having the
full version within Cascade and on the server, and the minified
version is only on the server. One downside with this is the
minified file is static on the server and you wouldn't be able to
maintain it within Cascade Server, or internally link to it.</p>
<p>You could also create the minified file within Cascade (or
update it if it already exists). This way, when you update and
publish the full source, it would also update the minified source
and publish that as well. This would allow you to continue to
maintain one file (or both if you really wanted to) and link to the
minified file from your pages (rather than use a static link).</p>
<p>If you do go this route, you would probably want to add a check
to ensure you are not re-minifying an already minified file (maybe
check the name to see if it contains <code>.min.</code>). You may
also want to figure out a way to ensure if the original file is
moved/renamed/unpublished, so is the minified one, so you don't end
up with a bunch of outdated files.</p>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/312089112014-01-28T16:25:05Z2014-01-28T17:47:03ZMinify CSS/JS during publish?<div><p>I don't have access to File.getText() in the 7.6.x API JARs.
Unless I'm using the wrong class - I'm creating an instance of
com.hannonhill.cascade.api.asset.home.File, and trying to compile
calling getText() fails because getText() isn't defined on the
interface.</p>
<p>What I had in mind for publishing was, let's say I have my site
named Public and I have the file <em>resources/css/base.css. When I
publish out _resources/css/base.css, the server should receive only
the minified version - Cascade maintains the fully expanded CSS
file that I can easily edit, and I can link to it normally without
needing a static link because Cascade would generate a link to
<a href=
"http://www.example.com/">http://www.example.com/</a></em>resources/css/base.css
and the server would give me the minified file at that URL.</p>
<p>In short, the ideal implementation would see Cascade never
storing the minified file and the server receiving only a minified
file.</p></div>jgoguentag:help-archives.hannonhill.com,2010-02-09:Comment/312089112014-01-28T16:45:18Z2014-01-28T16:45:18ZMinify CSS/JS during publish?<div><blockquote>
<p>In short, the ideal implementation would see Cascade never
storing the minified file and the server receiving only a minified
file.</p>
</blockquote>
<p>That definitely makes sense now, and is a great solution.</p>
<blockquote>
<p>I don't have access to File.getText() in the 7.6.x API JARs.
Unless I'm using the wrong class - I'm creating an instance of
com.hannonhill.cascade.api.asset.home.File, and trying to compile
calling getText() fails because getText() isn't defined on the
interface.</p>
</blockquote>
<p>Ah, my apologies, the <code>File.getText()</code> method was
added in 7.8.2 (<a href=
"https://hannonhill.jira.com/browse/CSI-682">here is the
improvement</a> in our issue tracker).</p>
<p>Regarding the LIE error you are encountering, I was speaking to
the developers and this was actually a <a href=
"https://hannonhill.jira.com/browse/CSI-680">known defect</a> that
was resolved in 7.8.2 as well.</p>
<p>Upgrading to at least 7.8.2 would definitely help you out and
would be the recommended solution. If you are not able to upgrade,
you could do something similar to what the DocX trigger does: read
the contents of the published file on the server, minify the
content, and then update the file on the server.</p>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/312089112014-01-28T17:53:38Z2014-01-28T17:53:43ZMinify CSS/JS during publish?<div><p>Thanks for the update. I think I'll push for us to get upgraded
to 7.8.2 internally and wait for 7.8.2. The getText() method would
make things a little simpler.</p>
<p>Are there any tips for how I should be proceeding with the
solution once I'm on 7.8.2? I'm just not sure if I should somehow
give Cascade back the minified content (and how) or should I
manually fetch the Transport and the Destination and send the data
up myself and stop the publish (and how)?</p></div>jgoguentag:help-archives.hannonhill.com,2010-02-09:Comment/312089112014-01-28T18:21:05Z2014-01-28T18:21:07ZMinify CSS/JS during publish?<div><p>Oh, also, the API JARs and the publish trigger pages both
reference 7.6.x as the highest version supported. Could you please
ask to have API JARs for publish triggers with 7.8.x published?</p></div>jgoguentag:help-archives.hannonhill.com,2010-02-09:Comment/312089112014-01-28T18:23:58Z2014-01-28T18:23:58ZMinify CSS/JS during publish?<div><p>Not a problem at all, Joel. Upgrading would definitely make
getting the file's contents easier, since it would avoid the need
to read the content of the published file that is on the
server.</p>
<blockquote>
<p>Are there any tips for how I should be proceeding with the
solution once I'm on 7.8.2? I'm just not sure if I should somehow
give Cascade back the minified content (and how) or should I
manually fetch the Transport and the Destination and send the data
up myself and stop the publish (and how)?</p>
</blockquote>
<p>Once you are upgraded, I believe the steps would be:</p>
<ul>
<li>Get the File's asset's information from PublishInformation</li>
<li>Get the Transport/Destination information from
PublishInformation</li>
<li>Retrieve its content as a string using
<code>File.getText()</code></li>
<li>Minify the content string</li>
<li>Overwrite the published file that is on the server with the
newly minified content, using the Transport/Destination info</li>
</ul>
<p>Note: the full file will always be published to the server since
a Publish Trigger is executed after each individual publish job, so
you would need to overwrite what is on the server no matter what if
you only want one file out there. One side effect I can think of is
between the time the publish job completes and the trigger
executes, you could have a file that is the full source.</p>
<blockquote>
<p>Oh, also, the API JARs and the publish trigger pages both
reference 7.6.x as the highest version supported. Could you please
ask to have API JARs for publish triggers with 7.8.x published?</p>
</blockquote>
<p>Sure, not a problem at all. Let me speak with the developers to
see if we can get those updated.</p>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/312089112014-01-28T18:44:48Z2014-01-28T18:55:40ZMinify CSS/JS during publish?<div><p>Ahh! I didn't clue in that the triggers happen after the publish
is complete. Makes plenty of sense now.</p></div>jgoguentag:help-archives.hannonhill.com,2010-02-09:Comment/312089112014-01-28T18:54:06Z2014-01-28T18:54:06ZMinify CSS/JS during publish?<div><p>Joel, I also forgot to mention that there is also a <a href=
"http://ideas.hannonhill.com/forums/52559-ideas-for-cascade-server/suggestions/2887030-concatenate-and-minify-css-javascript">
related suggestion</a> on our Idea Exchange to add the ability to
minify JavaScript and CSS files.</p>
<p>I would highly recommend voting this up and/or commenting on it
if you would like to see a feature like this implemented in a
future release.</p>
<p>Let me know if you need access to the Idea Exchange and I will
send over an invitation.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/312089112014-01-28T19:10:24Z2014-01-28T19:10:27ZMinify CSS/JS during publish?<div><p>I've already seen and upvoted that idea. The concatenation isn't
necessarily interesting for us yet, but that's mostly because we
don't know yet which scripts/stylesheets we would actually
concatenate most commonly or if we would need some sort of script
to concatenate an arbitrary set of scripts/stylesheets.</p></div>jgoguentag:help-archives.hannonhill.com,2010-02-09:Comment/312089112014-01-29T14:15:24Z2014-01-29T14:15:24ZMinify CSS/JS during publish?<div><p>Excellent, thank you for contributing to our Idea Exchange.</p>
<blockquote>
<p>The concatenation isn't necessarily interesting for us yet, but
that's mostly because we don't know yet which scripts/stylesheets
we would actually concatenate most commonly or if we would need
some sort of script to concatenate an arbitrary set of
scripts/stylesheets.</p>
</blockquote>
<p>I see what you mean. I believe this suggestion would include the
ability to do one or the other (or both), so it should cover your
use case. Having an option on maybe the file's system tab to minify
on publish would be useful.</p>
<p>Please let me know if you have any additional questions with
your publish trigger.</p>
<p>Thanks!</p></div>Ryan Griffith