Skip to main content

Creating Beauty with UglifyJS

For the past five years, we have been using a Notes-client base tool called iPhora Application Designer, that we created to build our iPhora applications written for our Dojo-based iPhora-MVC platform. This tool is written entirely in LotusScript and @Formula language.

With this tool we compile the  XML/JS view code into pure JavaScript onto a targeted application server all without opening Domino Designer.  Rarely do we venture into using Domino Designer. We only use Domino Designer to enhance our tool or fix a bug. Since this tool is Notes-client based, a key advantage of this tool is the ability to replicate every day our code to a backup server and when we are at home we can replicate to our computer at home.

Currently, our JavaScript MVC platform has over a 1,000,000 lines of Dojo/Javascript code and it grows every day as we add new widgets and modules. Majority of the code is automatically generated by our iPhora Application Designer. Can not imagine generating the code manually.

As part of the compilation process, we created a code HTML/JS minimifier in LotusScript to compress the code.  However, it never really worked the way that we wanted it to and it could not obfuscate the code. So we decide to look elsewhere for help.

There are many JS minimifier out there, but we wanted the ability to integrate it with our development process and also our iPhora Application Designer.  So we looked into integrating tools that we can run using the LotusScript Shell command to process the JavaScript.

After researching different alternatives, we decided to tried a node-based solution called UglifyJS which has a command line interface which we can invoke using the LotusScript Shell command.

First, you need to download and install the latest version of Node.  Make sure that you include the NPM manager during the installation process.

https://nodejs.org/en/download/

After installing node and npm, open a command line window and install uglifyJS from the NPM depository using the following command line.


npm install uglify-js -g

Now that you have installed UglifyJS, you need to confirm that it is working correctly. Create a simple JavaScript file called test.js and run UglifyJS using the following command.

uglifyjs test.js -c -m -o test.min.js

-c : compress the code
-m : mangle the code
-o : declare that there is output file with the following name

If the resulting test.min.js is a compressed mangled version of test.js then it is working.  

We could manually create files and then uglify them one at a time and then uploaded them to the server, but that is time consuming and laborious. Automation is key to reducing the time that you need to spend coding. So we needed to figure out a way to automate the process.

Our XML/JS code are stored in Notes Rich-text fields. When the code is compiled, the generated compiled code is pushed into the targeted Domino server using good old DXL. 

In order to minimify the code using UglifyJS we needed to change our process. Rather than compressing the code using the LotusScript and then pushing it up to the server, we needed a way to get it into the format that the Node-based uglifyJS can use, compress and mangle the code, read the code and push it into the server all in one single operation.  Show below is the process and code that we created to achieve our goal.

Read JavaScript Code from Rich Text

To get the code from the rich-text field using "GetUnformattedText()" Lotusscript rich text item method.

Set ritem = doc.GetFirstItem(fieldName)
If Not(ritem Is Nothing) Then
code = ritem.GetUnformattedText()
else 
code = ""
end if
fileName = tempGetNotesDirectory() & |\| & |input.js|
Dim fn%
fn = FreeFile
Open fileName For Output As fn
Print #fn,code
Close fn

Create Batch File to Process the JavaScript

cmdLine = |uglifyjs input.js -c -m -o output.js|

Create a batch file that has the command to run UglifyJS, UglifyJS.bat


cmdLine = |uglifyjs input.js -c -m -o output.js|

fileName = tempGetNotesDirectory() & |\| & |UglifyJS.bat|

Dim fn%

fn = FreeFile
Open fileName For Output As fn
Print #fn,cmdLine
Close fn

Run the batch using the LotusScript Shell command to create the file "output.js"

batchInstance = Shell("UglifyJS.bat",0);

The problem that you have running a batch file using Shell is that there is no way to know if the process has been completed using LotusScript. However, an easy way to check if the process is done is determine if the file output.js has been created.  If not, wait and check again. The following code allows you to query the file system for completion. The uglifying process is very fast, but a large JavaScript file may take longer. The code waits for 1 second and checks the file system. It tried this 10 times and if it can't find it the entire process is killed off.

Function runBatch() as Boolean
Dim tempdir As String
Dim i As Integer
i = 0
On Error GoTo ProcessError
batchInstance = Shell(tempdir &"\node_batch.bat", 6)
Do While i < 10
Sleep(1)
If Dir$("output.js",0) <> "" Then
Exit Do
End If
i = i + 1
Loop
If i > 10 Then
runBatch = false ' process timeout
Else
runBatch = true  ' process ran successful
End if

End Function

After the process completes we need to read the output.js file so that we can push it up to the Domino server.

Function readJSFile() As String
Dim tempdir As String
Dim fileName As String
Dim uglifiedCode As String
On Error GoTo ProcessError
tempdir = tempGetNotesDirectory()
fileName = tempdir & |\output.js|
Dim fn%
fn = FreeFile
Open fileName For Input As fn
uglifiedCode = Input(LOF(fn), fn)
Close fn
readJSFile = uglifiedCode
End Function

Using our DXL-based methods we stream the compressed and mangled file up to the Domino server.

There is one more step that is required. We need to delete all the files that we created in the temporary directory so that we can process another file. If not, the process can not be repeated since there will always be a output.js file.

Function removeTempFiles() as Boolean
tempdir = tempGetNotesDirectory()
kill tempdir & |\uglifyjs.bat|
kill tempdir & |\input.js|
kill tempdir & |\output.js|
End Function

tempGetNotesDirectory(), function to find the Notes temporary directory

The inclusion of UglifyJS to our iPhora Application Designer has been so successful that we have added other Node-based developer tools to it using the same method to connect our Notes-based tool to new and modern development tools. So far we have added CSSLint and Minifier to cleanup and compress our CSS file. We are looking for other Node modules that we can include to help us automatic the development and testing process. There are thousands of npm modules that run on Node. There are many to choose from.

If you are using the Domino Designer, you should look at this Node-based tools to add new capabilities in your developer's toolbox. How that can be done is beyond my skills.




Comments

NotesSensei said…
You might go a step further. Have a look at webpack. Besides minifying it also can combine all JS and CSS into a single file, so you need less https calls.
When your JS is ess6 compatible you might even use tree shaking to minimise it further

Popular posts from this blog

Creating Twitter Bootstrap Widgets - Part II - Let's Assemble

Creating Twitter Bootstrap Widgets - Part I - Anatomy of a Widget Creating Twitter Bootstrap Widgets - Part II - Let's Assemble Creating Twitter Bootstrap Widgets - Part IIIA - Using Dojo To Bring It Together This is two part of my five part series "Creating Twitter Bootstrap Widgets".   As I mentioned in part one of this series, Twitter Bootstrap widgets are built from a collection standard HTML elements, styled, and programmed to function as a single unit. The goal of this series is to teach you how to create a Bootstrap widget that utilizes the Bootstrap CSS and Dojo. The use of Dojo with Bootstrap is very limited with the exception of Kevin Armstrong who did an incredible job with his Dojo Bootstrap, http://dojobootstrap.com. Our example is a combo box that we are building to replace the standard Bootstrap combo box. In part one, we built a widget that looks like a combo box but did not have a drop down menu associated with it to allow the user to make a select

The iPhora Journey - Part 8 - Flow-based Programming

After my last post in this series -- way back in September 2022, several things happened that prevented any further installments. First came CollabSphere 2022 and then CollabSphere 2023, and organizing international conferences can easily consume all of one's spare time. Throughout this same time period, our product development efforts continued at full speed and are just now coming to fruition, which means it is finally time to continue our blog series. So let's get started... As developers, most of us create applications through the conscious act of programming, either procedural, as many of us old-timers grew up with, or object-oriented, which we grudgingly had to admit was better. This is true whether we are using Java, LotusScript, C++ or Rust on Domino. (By the way, does anyone remember Pascal? When I was in school, I remember being told it was the language of the future, but for some reason it didn't seem to survive past the MTV era).  But in the last decade, there a

The iPhora Journey - Part 4 - JSON is King - The How

  The iPhora Journey - Part 1 - Reimagining Domino The iPhora Journey - Part 2 - Domino, the Little Engine that Could The iPhora Journey - Part 3 - Creating an Integrated UI Framework The iPhora Journey - Part 4 - JSON is King - The Why The iPhora Journey - Part 4 - JSON is King - The How As we mentioned yesterday, in reimagining Domino, we wanted Domino to be a modern web application server, one that utilized a JSON-based NoSQL database and be more secure compared to other JSON-based NoSQL platforms. A Domino document existing within a Domino database is the foundational data record used in iPhora, just as it is with traditional Domino applications. But instead of just storing data into individual fields, we wanted to store and process the JSON in a Domino document.  However, text fields (AKA summary fields) in Domino documents are limited to only 64 KBytes, and that is a serious limitation. 64 KBytes of JSON data does not even touch what the real world typically transfers back and fo