Mitrahsoft
  • SERVICES
    • OUR SERVICES
      • SOFTWARE DEVELOPMENT
      • MOBILE DEVELOPMENT
      • BLOCKCHAIN DEVELOPMENT
      • SOFTWARE TESTING
      • CLOUD & DEVOPS SERVICES
      • PRODUCT DEVELOPMENT
      • OFFSHORE DEVELOPMENT
      • SOCIAL MEDIA MARKETING
      TECHNOLOGIES
      • COLDFUSION DEVELOPMENT
      • REACTJS DEVELOPMENT
      • VUEJS DEVELOPMENT
      • ANGULAR DEVELOPMENT
      • NODEJS DEVELOPMENT
      • PYTHON DEVELOPMENT
      • GOLANG DEVELOPMENT
      • GOLANG CORPORATE TRAINING
      COLDFUSION EXPERTISE
      • MURACMS EXPERTISE
      • COLDFUSION APP MIGRATION
      • COLDFUSION HOSTING & SUPPORT
      • CF LEGACY APP MAINTENANCE
      • COLDFUSION REST API
      • ECOMMERCE / SHOPPING CART SOLUTIONS
      • PRESIDECMS EXPERTISE
      MOBILE DEVELOPMENT
      • REACT NATIVE DEVELOPMENT
      • FLUTTER DEVELOPMENT
      • IONIC DEVELOPMENT
      • PHONEGAP DEVELOPMENT
  • ABOUT US
    • ABOUT MITRAHSOFT
    • OUR CLIENTS
    • TESTIMONIALS
    • PORTFOLIO
    • CAREERS
  • CONTACT US
  • BLOG
Adobe Solution PartnerLucee Solution Partner
Adobe Solution PartnerLucee Solution Partner
Adobe Solution PartnerLucee Solution Partner
  • OUR SERVICESSOFTWARE DEVELOPMENTMOBILE DEVELOPMENTBLOCKCHAIN DEVELOPMENTSOFTWARE TESTINGCLOUD & DEVOPS SERVICESPRODUCT DEVELOPMENTOFFSHORE DEVELOPMENTSOCIAL MEDIA MARKETING
    TECHNOLOGIESCOLDFUSION DEVELOPMENTREACTJS DEVELOPMENTVUEJS DEVELOPMENTANGULAR DEVELOPMENTNODEJS DEVELOPMENTPYTHON DEVELOPMENTGOLANG DEVELOPMENTGOLANG CORPORATE TRAINING
    COLDFUSION EXPERTISEMURACMS EXPERTISECOLDFUSION APP MIGRATIONCOLDFUSION HOSTING & SUPPORTCF LEGACY APP MAINTENANCECOLDFUSION REST API ECOMMERCE / SHOPPING CART SOLUTIONSPRESIDECMS EXPERTISE
    MOBILE DEVELOPMENTREACT NATIVE DEVELOPMENTFLUTTER DEVELOPMENTIONIC DEVELOPMENTPHONEGAP DEVELOPMENT
  • ABOUT MITRAHSOFTOUR CLIENTSTESTIMONIALSPORTFOLIOCAREERS
  • CONTACT US
  • BLOG

Minify JS And CSS Files Using ColdFusion And YUI Library

HomeBlogMinify JS And CSS Files Using ColdFusion And YUI Library
YUI-library

Minify JS And CSS Files Using ColdFusion And YUI Library

M
Post by MitrahsoftPublished: Jun 19, 2019

All web applications has CSS and JS files. But, now a days most of the developers wants to include only minified JS and CSS files. Because, normal JS and CSS files having unnecessary characters in source codes. These unnecessary characters usually include white space characters, new line characters, comments, and sometimes block delimiters. These are very important only for code readability, but not needed for execution process. These unnecessary characters increase file size and it will be affect performance too. For this reason, the minification (also minimisation or minimization) is very important.

We have lot of JS and CSS minification online convertor and have lot of JAVA libraries available in internet. But, best one is, YUI Library ( Yahoo userInterface Library ). Because, it is very lightweight core library and also it is a free open source Library.
Minify JS CSS Files Using Coldfusion and Yui Library
The YUI - Yahoo userInterface Library Compressor is JavaScript minifier designed to be 100% safe and yield a higher compression ratio than most other tools.The YUI Compressor is also able to compress CSS files by using a port of Isaac Schlueter's regular-expression-based CSS minifier.

Prerequisites

  1. YUI Compressor requires 1.5 and above Java version.
  2. Version of YUI compressor library 2.4.8
  3. Download 2.4.8 version from this link ( https://github.com/yui/yuicompressor/releases )

Application.cfc

In this file, First need to include YUI jar file. In the below sample code, I have paste my downloaded YUI jar file inside of the lib folder.

cfml
<cfcomponent>
    <cfset this.name = "yuicompress3">

    <cfset THIS.javaSettings = {
        LoadPaths = ["./lib"],
        loadColdFusionClassPath = true,
        reloadOnChange = true,
        watchInterval = 10
    }>

    <cffunction name="onRequestStart">
        <cfset application.wrapperObj = createObject("component","cfc.YUICompressor")>
        <cfset application.wrapperObj.jsCompressor()>
        <cfset application.wrapperObj.cssCompressor()>
    </cffunction>

</cfcomponent>

So, using this.settings I have included my jar file into my application. And As usaual, I have created an object for 2 CFC files. Inside of those files, I wrote a code for minified process.

YUICompressor.cfc

In this file, I wrote two functions such as jsCompressor(), cssCompressor(). In the cssCompressor() function, I wrote CSS file minification process code and in the another function wrote a JS file minification process code.

cfml
<cfcomponent>
     <cffunction name="jsCompressor" access="public" output="false" returntype="void">
        <cfset stReturn = structNew()>
        <cfset oErrRep = createObject("cfc.ErrorReporter")>
        <cfset jErrRep = createDynamicProxy(oErrRep, ["org.mozilla.javascript.ErrorReporter"])>
        <cfset listOfJs = directoryList(expandPath("./assets/js"))>
        <cfloop array="#listOfJs#" index="name">
            <cffile action="read" variable="inputString" file="#name#">
            <cfset joInput = createObject("java","java.io.StringReader").init(inputString)>
            <cfset joOutput = createObject('java','java.io.StringWriter').init() />
            <cfset compr = createobject("java","com.yahoo.platform.yui.compressor.JavaScriptCompressor").init(joInput,jErrRep)>
            <cfset compr.compress(joOutput,javaCast('int',-1),<br>                              javaCast('boolean',false), <br>                              javaCast('boolean',false),<br>                              javaCast('boolean',false),<br>                              javaCast('boolean',false))>
            <cfset stReturn.results = joOutput.toString() />
            <cfset joOutput.close() />
            <cfset joInput.close() />
            <cfset stReturn.uncompressed = len(inputString) />
            <cfset stReturn.compressed = len(stReturn.results) />
            <cfset stReturn.compression = (1 - (stReturn.compressed / stReturn.uncompressed)) * 100 />
            <cfset extension = listLast(name,'.') />
            <cfset filename = listLast(replaceNoCase(name,'.js','.min.js'),'\') />
            <cfset folderName = "minifiedAssets" />
            <cfif NOT directoryExists(expandPath('./#folderName#'))>
               <cfset directoryCreate(expandPath('./#folderName#')) />
            </cfif>
            <cfif NOT directoryExists(expandPath('./#folderName#/#extension#'))>
               <cfset directoryCreate(expandPath('./#folderName#/#extension#')) />
            </cfif>
            <cffile action="write" file="#expandPath('./#folderName#/#extension#/#filename#')#" output="#stReturn.results#">
        </cfloop>
    </cffunction>

    <cffunction name="cssCompressor" access="public" output="false" returntype="void">
         <cfset stReturn = structNew()>
         <cfset listOfCss = directoryList(expandPath("./assets/css"))>
         <cfloop array="#listOfCss#" index="name">
             <cffile action="read" variable="inputString" file="#name#">
             <cfset joInput = createObject("java","java.io.StringReader").init(inputString)>
             <cfset joOutput = createObject('java','java.io.StringWriter').init() />
             <cfset compr = createobject("java","com.yahoo.platform.yui.compressor.CssCompressor").init(joInput)>
             <cfset compr.compress(joOutput,javaCast('int',-1))>
             <cfset stReturn.results = joOutput.toString() />
             <cfset joOutput.close() />
             <cfset joInput.close() />
             <cfset stReturn.uncompressed = len(inputString) />
             <cfset stReturn.compressed = len(stReturn.results) />
             <cfset stReturn.compression = (1 - (stReturn.compressed / stReturn.uncompressed)) * 100 />
             <cfset extension = listLast(name,'.') />
             <cfset filename = listLast(replaceNoCase(name,'.css','.min.css'),'\') />
             <cfset folderName = "minifiedAssets" />
             <cfif NOT directoryExists(expandPath('./#folderName#'))>
                  <cfset directoryCreate(expandPath('./#folderName#')) />
             </cfif>
             <cfif NOT directoryExists(expandPath('./#folderName#/#extension#'))>
                  <cfset directoryCreate(expandPath('./#folderName#/#extension#')) />
             </cfif>
             <cffile action="write" file="#expandPath('./#folderName#/#extension#/#filename#')#" output="#stReturn.results#">
          </cfloop>
     </cffunction>
</cfcomponent>

In side of this function, I created an object for ErrorReporter.cfc file. This is a required core file. So, Please don't forgot to include this CFC file on the particular directory. I just read all the JS and CSS files from /assets/js & /assets/css directory and then created some wonderful Java objects like StringBuilder,StringReader,StringWriter. These all are used for minified the CSS & JS files.

For compressing JS files need to call the JavaScriptCompressor, com.yahoo.platform.yui.compressor.JavaScriptCompressor For compressing CSS files need to call the CssCompressor com.yahoo.platform.yui.compressor.CssCompressor

cfml
<cfcomponent>

    <cffunction name="warning" returntype="void">
        <cfargument name="message" type="java.lang.String" required="false">
        <cfargument name="sourceName" type="java.lang.String" required="false">
        <cfargument name="line" type="int" required="false">
        <cfargument name="lineSource" type="java.lang.String" required="false">
        <cfargument name="lineOffset" type="int" required="false">
    </cffunction>

    <cffunction name="error" returntype="void">
        <cfargument name="message" type="java.lang.String" required="false">
        <cfargument name="sourceName" type="java.lang.String" required="false">
        <cfargument name="line" type="int" required="false">
        <cfargument name="lineSource" type="java.lang.String" required="false">
        <cfargument name="lineOffset" type="int" required="false">
    </cffunction>

    <cffunction name="runtimeError" returntype="org.mozilla.javascript.EvaluatorException">
        <cfargument name="message" type="java.lang.String" required="false">
        <cfargument name="sourceName" type="java.lang.String" required="false">
        <cfargument name="line" type="int" required="false">
        <cfargument name="lineSource" type="java.lang.String" required="false">
        <cfargument name="lineOffset" type="int" required="false">

        <cfset local.exc = createObject(
            "java",
            "org.mozilla.javascript.EvaluatorException"
        ).init(arguments.message)>

        <cfreturn local.exc>
    </cffunction>

</cfcomponent>

Tags

ADYENAMAZON-SESANDROIDAPACHEAPACHE-JMETERAWSBARCODE-SCANNERCOLDBOXCOLDFUSIONCOLDFUSION-API-INTEGRATIONCOLDFUSIONBUILDERECHARTSEVENT-GATEWAYFFMPEGFW1JQUERYJSOUPLUCEEMURACMSMURACMS-THEMENODEJSONESIGNALOSSPAYFLOW-PROPAYMENT-GATEWAYPAYPALPRESIDECMSPUPPETEERRAILORAZUNAREACTJSREACTNATIVERESTSASS-COMPILATIONSEMANTIC-UISUBLIMETEXTTINYMCETWILIOYUI-LIBRARY

Archives

JAN 20DEC 19AUG 19JUL 19JUN 19MAY 19APR 19MAR 19FEB 19JAN 19DEC 18NOV 18OCT 18MAR 16NOV 15SEP 15MAY 15MAY 14OCT 13JUN 13MAY 13APR 13MAR 13FEB 13JAN 13

Follow us

Mitrahsoft
United StatesUnited States

Hurst, Texas, United States

+1 (817) 606-8684usa-sales@mitrahsoft.com

IndiaIndia

Head Office

126G2/5, Thiru malai nagaram,
Kovilpatti - 628 501

Madurai

1/B, 2nd & 3rd floor, GV Complex,

Bye Pass Road, SS Colony,

Madurai - 625 016

Coimbatore

2nd Floor, Sri Narmadha Towers,

Murugan Nagar Rd, Thoppampatti Pirivu,

 K. Vadamadurai, Coimbatore - 641 017

+91 9092480924

business@mitrahsoft.com

ColdFusion
  • eCommerce / Shopping Cart Solutions
  • Payment Gateway Integrations
  • Shipping API Integrations
  • ColdFusion Development Services
  • REST API Development
  • MuraCMS Plugin Creation & Personalization
Technology Services
  • ReactJS Development
  • VueJS Development
  • AngularJS Development
  • NodeJS API Development
  • Python Development
  • Golang Development
  • React Native Development
  • D3.JS data Visualization
Other Services
  • Software Development
  • Mobile Development
  • Blockchain Development
  • Cloud & Devops Services

Connect with MitrahSoft

Please fill out this field
Please fill out this field
Please fill out this field
Please fill out this field
Please fill out this field
Please fill out this field

© 2026 All Rights Reserved. MitrahSoft Solutions Pvt Ltd

Home|About Us|Careers