Web Server gzip compression batch file

Topics for the Eclipse Environment
Post Reply
ephogy
Posts: 30
Joined: Fri Aug 29, 2008 12:53 pm

Web Server gzip compression batch file

Post by ephogy »

I'm not sure if anyone else uses static pages, but a while back, I built this small batch file to compress all the .html, .css and .js files so that they don't take up a ridiculous amount of space, and because the built in Huffman encoding is... not the best.

I apologize to the Mac/Linux users, this uses a good ol' MS-DOS batch file. I'm sure that those so-inclined can create their own scripts.

1. Install node.js, then uglify, uglifycss and html-minifier -- follow the instructions listed in the batch file. I don't think the version of nodejs used is all that important.

3. Create http_compress.bat in your main project folder:

Code: Select all

@echo off
REM install node.js:
REM https://nodejs.org/dist/v8.9.1/node-v8.9.1-x64.msi
REM 
REM Next, from a command prompt:
REM install uglify
REM npm install uglify-js -g
REM 
REM install uglifycss
REM npm install uglifycss -g
REM 
REM install html-minifier
REM npm install html-minifier -g

setlocal enabledelayedexpansion

:START
IF NOT EXIST %2 (mkdir %2)

set rtn=0
for %%f in (%1\*) do (
  if /I "%%~xf" == ".html" (
    CALL :COMPARE %1/%%~nf.html %2/%%~nf.html
    if "!rtn!"=="0" (
      echo %%~nf.html
      REM call %AppData%\npm\html-minifier --output %2\%%~nf %1/%%~nf.html
      REM call %AppData%\npm\html-minifier --minify-css --minify-js --collapse-boolean-attributes --collapse-inline-tag-whitespace --collapse-whitespace --decode-entities --html5 --include-auto-generated-tags --keep-closing-slash --remove-attribute-quotes --remove-comments --remove-empty-attributes --remove-empty-elements --remove-optional-tags --remove-redundant-attributes --remove-script-type-attributes --remove-style-link-type-attributes --remove-tag-whitespace --use-short-doctype --output %2\%%~nf %1/%%~nf.html
      cp %1/%%~nf.html %2/%%~nf
      gzip -9 -n -f -S .html %2\%%~nf
    )
  ) else if /I "%%~xf" == ".js" (
    CALL :COMPARE %1/%%~nf.js %2/%%~nf.js
    if "!rtn!"=="0" (
      echo %%~nf.js
      call %AppData%\npm\uglifyjs -c -m -o %2\%%~nf %1\%%~nf.js
      gzip -9 -f -S .js %2\%%~nf
    )
  ) else if /I "%%~xf" == ".css" (
    CALL :COMPARE %1/%%~nf.css %2/%%~nf.css
    if "!rtn!"=="0" (
      echo %%~nf.css
      call %AppData%\npm\uglifycss --output %2\%%~nf %1\%%~nf.css
      gzip -9 -f -S .css %2\%%~nf
    )
  ) else (
    CALL :COMPARE %1/%%~nf%%~xf %2/%%~nf%%~xf
    if "!rtn!"=="0" (
      echo copy %%~nf%%~xf
      cp %1/%%~nf%%~xf %2/%%~nf%%~xf>NUL
      touch %2\%%~nf%%~xf
    )
  )
)

exit /B 0

:COMPARE
  set /a x=0
  for /F %%i in ('ls -St -1 %1 %2 2^>NUL') do (
    set oldest=%%i
    set /a x+=1
  )

  IF NOT "%x%"=="2" (set rtn=0) ELSE IF NOT "%1"=="%oldest%" (set rtn=0) ELSE (set rtn=1)

  exit /B %rtn%
4. Update \nburn\pcbin\MIME_magic.txt and set .css, .html and .js to Process type 4 or, depending on your usage, you might want to modify the batch file so that it produces static files with some other extension. In which case, add the extension to your MIME_magic.txt file instead, and modify the compression batch accordingly. -- I kept them the same because it allows me to edit/run the files locally and not have to worry about renaming them for the build.

5. Modify some build steps:
Properties of your project, go to C/C++ Build->Settings
Under Tool Settings->NetBurner Comphtml, I've changed the command line pattern so that it uses "html_process" instead of "..\html"
Under Tool Settings->Build Steps, I've added a Pre-build step, ..\html_compress.bat ..\html html_process;

This will execute the above compression code, work on the \html folder of your project and place the compressed version of your files into Release\html_process, allowing comphtml do it's thing.

I believe this is everything you'll need, hopefully I haven't left out any steps.
I periodically check the forum for updates, but I likely won't be the fastest with responses.

Cheers.
User avatar
Jon
Posts: 79
Joined: Mon Feb 05, 2018 10:54 am

Re: Web Server gzip compression batch file

Post by Jon »

Thanks for passing this along! Next time I'm building an app with web content I will be sure to give it a try. =)
Post Reply