Moving Your Application to Production

0
151
9 min read

(For more resources related to this topic, see here.)

We will start by examining the Sencha Cmd compiler.

Compiling with Sencha Cmd

This section will focus on using Sencha Cmd to compile our Ext JS 4 application for deployment within a Web Archive (WAR) file. The goal of the compilation process is to create a single JavaScript file that contains all of the code needed for the application, including all the Ext JS 4 dependencies.

The index.html file that was created during the application skeleton generation is structured as follows:

<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>TTT</title> <!-- <x-compile> --> <!-- <x-bootstrap> --> <link rel="stylesheet" href="bootstrap.css"> <script src = "ext/ext-dev.js"></script> <script src = "bootstrap.js"></script> <!-- </x-bootstrap> --> <script src = "app.js"></script> <!-- </x-compile> --> </head> <body></body> </html>

The open and close tags of the x-compile directive enclose the part of the index.html file where the Sencha Cmd compiler will operate. The only declarations that should be contained in this block are the script tags. The compiler will process all of the scripts within the x-compile directive, searching for dependencies based on the Ext.define, requires, or uses directives.

An exception to this is the ext-dev.js file. This file is considered to be a “bootstrap” file for the framework and will not be processed in the same way. The compiler ignores the files in the x-bootstrap block and the declarations are removed from the final compiler-generated page.

The first step in the compilation process is to examine and parse all the JavaScript source code and analyze any dependencies. To do this the compiler needs to identify all the source folders in the application. Our application has two source folders: Ext JS 4 sources in webapp/ext/src and 3T application sources in webapp/app. These folder locations are specified using the -sdk and -classpath arguments in the compile command:

sencha –sdk {path-to-sdk} compile -classpath={app-sources-folder} page -yui -in
{index-page-to-compile}
-out {output-file-location}

For our 3T application the compile command is as follows:

sencha –sdk ext compile -classpath=app page -yui -in index.html
-out build/index.html

This command performs the following actions:

  • The Sencha Cmd compiler examines all the folders specified by the -classpath argument. The -sdk directory is automatically included for scanning.
  • The page command then includes all of the script tags in index.html that are contained in the x-compile block.
  • After identifying the content of the app directory and the index.html page, the compiler analyzes the JavaScript code and determines what is ultimately needed for inclusion in a single JavaScript file representing the application.
  • A modified version of the original index.html file is written to build/index.html.
  • All of the JavaScript files needed by the new index.html file are concatenated and compressed using the YUI Compressor, and written to the build/all-classes.js file.

The sencha compile command must be executed from within the webapp directory, which is the root of the application and is the directory containing the index.html file. All the arguments supplied to the sencha compile command can then be relative to the webapp directory.

Open a command prompt (or terminal window in Mac) and navigate to the webapp directory of the 3T project. Executing the sencha compile command as shown earlier in this section will result in the following output:

Opening the webapp/build folder in NetBeans should now show the two newly generated files: index.html and all-classes.js. The all-classes.js file will contain all the required Ext JS 4 classes in addition to all the 3T application classes. Attempting to open this file in NetBeans will result in the following warning: “The file seems to be too large to open safely…“, but you can open the file in a text editor to see the following concatenated and minified content:

Opening the build/index.html page in NetBeans will display the following screenshot:

You can now open the build/index.html file in the browser after running the application, but the result may surprise you:

The layout that is presented will depend on the browser, but regardless, you will see that the CSS styling is missing. The CSS files required by our application need to be moved outside the <!– <x-compile> –> directive. But where are the styles coming from? It is now time to briefly delve into Ext JS 4 themes and the bootstrap.css file.

Ext JS 4 theming

Ext JS 4 themes leverage Syntactically Awesome StyleSheets (SASS) and Compass (http://compass-style.org/) to enable the use of variables and mixins in stylesheets. Almost all of the styles for Ext JS 4 components can be customized, including colors, fonts, borders, and backgrounds, by simply changing the SASS variables. SASS is an extension of CSS that allows you to keep large stylesheets well-organized; a very good overview and reference can be found at http://sass-lang.com/documentation/file.SASS_REFERENCE.html.

Theming an Ext JS 4 application using Compass and SASS is beyond the scope of this book. Sencha Cmd allows easy integration with these technologies to build SASS projects; however, the SASS language and syntax is a steep learning curve in its own right. Ext JS 4 theming is very powerful and minor changes to the existing themes can quickly change the appearance of your application. You can find out more about Ext JS 4 theming at http://docs.sencha.com/extjs/4.2.2/#!/guide/theming.

The bootstrap.css file was created with the default theme definition during the generation of the application skeleton. The content of the bootstrap.css file is as follows:

@import 'ext/packages/ext-theme-classic/build/resources/
ext-theme-classic-all.css';

This file imports the ext-theme-classic-all.css stylesheet, which is the default “classic” Ext JS theme. All of the available themes can be found in the ext/packages directory of the Ext JS 4 SDK:

Changing to a different theme is as simple as changing the bootstrap.css import. Switching to the neptune theme would require the following bootstrap.css definition:

@import 'ext/packages/ext-theme-neptune/build/resources/
ext-theme-neptune-all.css';

This modification will change the appearance of the application to the Ext JS “neptune” theme as shown in the following screenshot:

We will change the bootstrap.css file definition to use the gray theme:

@import 'ext /packages/ext-theme-gray/build/resources/ext-theme-gray-all.css';

This will result in the following appearance:

You may experiment with different themes but should note that not all of the themes may be as complete as the classic theme; minor changes may be required to fully utilize the styling for some components.

We will keep the gray theme for our index.html page. This will allow us to differentiate the (original) index.html page from the new ones that will be created in the following section using the classic theme.

Compiling for production use

Until now we have only worked with the Sencha Cmd-generated index.html file. We will now create a new index-dev.html file for our development environment. The development file will be a copy of the index.html file without the bootstrap.css file. We will reference the default classic theme in the index-dev.html file as follows:

<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>TTT</title> <link rel="stylesh eet" href="ext/packages/ext-theme-classic/build/resources/
ext-theme-classic-all.
css"> <link rel="stylesheet" href="resources/styles.css"> <!-- <x-compile> --> <!-- <x-bootstrap> --> <script src = "ext/ext-dev.js"></script> <script src = "bootstrap.js"></script> <!-- </x-bootstrap> --> <script src = "app.js"></script> <!-- </x-compile> --> </head> <body></body> </html>

Note that we have moved the stylesheet definition out of the <!– <x-compile> –> directive.

If you are using the downloaded source code for the book, you will have the resources/styles.css file and the resources directory structure available. The stylesheet and associated images in the resources directory contain the 3T logos and icons. We recommend you download the full source code now for completeness.

We can now modify the Sencha Cmd compile command to use the index-dev.html file and output the generated compile file to index-prod.html in the webapp directory:

sencha –sdk ext compile -classpath=app page -yui -in index-dev.html
-out index-prod.html

This command will generate the index-prod.html file and the all-classes.js files in the webapp directory as shown in the following screenshot:

The index-prod.html file references the stylesheets directly and uses the single compiled and minified all-classes.js file. You can now run the application and browse the index-prod.html file as shown in the following screenshot:

You should notice a significant increase in the speed with which the logon window is displayed as all the JavaScript classes are loaded from the single all-classes.js file.

The index-prod.html file will be used by developers to test the compiled all-classes.js file.

Accessing the individual pages will now allow us to differentiate between environments:

The logon window as displayed in the browser

Page description

The index.html page was generated by Sencha Cmd and has been configured to use the gray theme in bootstrap.css. This page is no longer needed for development; use index-dev.html instead.

You can access this page at

http://localhost:8080/index.html

The index-dev.html page uses the classic theme stylesheet included outside the <!– <x-compile> –> directive. Use this file for application development. Ext JS 4 will dynamically load source files as required.

You can access this page at

http://localhost:8080/index-dev.html

The index-prod.html file is dynamically generated by the Sencha Cmd compile command. This page uses the all-classes.js all-in-one compiled JavaScript file with the classic theme stylesheet.

You can access this page at http://localhost:8080/index-prod.html

LEAVE A REPLY

Please enter your comment!
Please enter your name here