Ext.data.JsonP.command_compiler({"title":"Sencha Compiler Reference","guide":"

Sencha Compiler Reference

\n
\n

Contents

\n
    \n
  1. Sets And The Current Set
  2. \n
  3. Generating Output with concat
  4. \n
  5. Saving And Restoring Sets
  6. \n
  7. Set Operations
  8. \n
  9. Compiler Directives
  10. \n
  11. Conditional Compilation
  12. \n
\n
\n\n

One of the major components new to Sencha Cmd with version 3 is the compiler. In a nutshell,\nthe compiler is a JavaScript-to-JavaScript, framework-aware optimizer. It is designed to\n\"understand\" your high-level Ext JS and Sencha Touch code and produce the smallest, most\nefficient code possible to support these high-level abstractions.

\n\n

\"\"

\n\n

Before using the compiler, you should understand the basics of Sencha Cmd by reading the\nfollowing guides:

\n\n\n\n\n

Sets And The Current Set

\n\n

Under the covers, the compiler manages a set of source files and analyzes these files to\ndetermine their dependencies. The set of all files is determined by the classpath:

\n\n
sencha compile -classpath=sdk/src,app ...\n
\n\n

In this example, the compiler recursively loads \"*.js\" from the specified list of folders.\nThis set of all files defines the basis for all operations to follow (that is, it defines\nthe \"universe\").

\n\n

The default classpath used by the compiler comes from these configuration properties:

\n\n
${framework.classpath},${workspace.classpath},${app.classpath}\n
\n\n

The compiler's output commands (for example, concat and metadata) operate on the set\nof files called the \"current set\". The current set starts out equal to the universe of all\nfiles, but this can be manipulated using the many commands provided to perform set\noperations.

\n\n

Note. With the compiler, you will often see rather long command lines using the command\nchaining mechanism and. Also, in practical use cases, for long command lines you should\nconsider using Ant or a \"response file\". See\nAdvanced Sencha Cmd. In this guide, all command lines will be\ncomplete (and potentially long) to keep the examples as clear as possible.

\n\n

Generating Output with concat

\n\n

A compiler ultimately is all about writing useful output given some number of inputs. The\nconcat command is designed to concatenate the source for the current set of files in the\nappropriate dependency order.

\n\n

The one required parameter is -out, which indicates the name of the output file. There\nare other options, however, that effect the generated file. You can pick one of the\nfollowing options for compression:

\n\n\n\n\n

The following command illustrates how to produce three flavors of output given a single\nread of the source.

\n\n
sencha compile -classpath=sdk/src \\\n    exclude -namespace Ext.chart and \\\n    concat ext-all-nocharts-debug-w-comments.js and \\\n    -debug=true \\\n    concat -strip ext-all-nocharts-debug.js and \\\n    -debug=false \\\n    concat -yui ext-all-nocharts.js\n
\n\n

Generating Metadata

\n\n

The compiler can also generate metadata in many useful ways, for example, the names of\nall source files, the set of files in dependency order, etc. To see what is available,\nsee the Generating Metadata guide.

\n\n

Saving And Restoring Sets

\n\n

When you need to produce multiple output files, it can be very helpful to save the\ncurrent set for later use, which you do like this:

\n\n
sencha compile -classpath=sdk/src \\\n    exclude -namespace Ext.chart and \\\n    save nocharts and \\\n    ...\n    restore nocharts and \\\n    ...\n
\n\n

Thesavecommand takes a snapshot of the current set and stores it under the given name\n(nocharts` in the above).

\n\n

The simplest use of a saved set is the restore command. This does the reverse and\nrestores the current set to its state at the time of the save.

\n\n

Set Operations

\n\n

Many of the commands provided by the compiler are classified as set operations, which are\noperations that work on and produce sets. In the case of the compiler, this means sets of\nfiles or classes. Let's first take a look at set terminology.

\n\n

A Little Set Theory

\n\n

There are three classic set operations:

\n\n\n\n\n

Set include and exclude

\n\n

These two set operations are probably the most common (and flexible) set operations. Both\nsupport these basic switches:

\n\n\n\n\n

In all of these cases, the next command line argument is a list of match criteria\nseparated by commas. Also, a single exclude or include can have as many switch/value\npairs as needed.

\n\n

So, let's start with a simple example and build an \"ext-all-no-charts-debug-w-comments.js\".

\n\n
sencha compile -classpath=sdk/src \\\n    exclude -namespace Ext.chart and \\\n    ...\n
\n\n

What is happening here is that we started with only the Ext JS sources (in \"sdk/src\") and\nthey were all part of the \"current set\". We then performed a set difference by excluding\nall files in the Ext.chart namespace. The current set was then equivalent to \"ext-all.js\"\nbut without any of the Chart package.

\n\n

Negating include and exclude with -not

\n\n

Both include and exclude support a rich set of matching criteria. This is rounded out\nby the -not switch, which negates the matching criteria that follows it. This means that\nthe files included or excluded are all those that do not match the criteria.

\n\n

For example:

\n\n
sencha compile -classpath=sdk/src,js \\\n    ... \\\n    exclude -not -namespace Ext and \\\n    ...\n
\n\n

The above exclude command will exclude from the current set any classes that are not in\nthe Ext namespace.

\n\n

The all Set

\n\n

In some cases, it is very handy to restore the current set to all files or to the empty\nset. To do this, you simply use include or exclude with the -all switch. To build\non the previous example:

\n\n
sencha compile -classpath=sdk/src \\\n    ... \\\n    include -all and \\\n    ... \\\n    exclude -all and \\\n    ...\n
\n\n

After the include -all, the current set is all files. After exclude -all it is the\nempty set.

\n\n

Union

\n\n

As shown already, the include command is a form of set union: it performs a union of\nthe current set with the set of matching files. Sometimes it is desirable to not include\nthe current set in the union and only those file matching the desired criteria. This is\nwhat the union command does.

\n\n

The union command has all of the options of include. Consider this union command:

\n\n
sencha compile -classpath=sdk/src ... and \\\n    union -namespace Ext.grid,Ext.chart and \\\n    ...\n
\n\n

It is exactly equivalent to this pair of exclude and include commands:

\n\n
sencha compile -classpath=sdk/src ... and \\\n    exclude -all and \\\n    include -namespace Ext.grid,Ext.chart and \\\n    ...\n
\n\n

Transitivity/Recursive Union

\n\n

One of the most important set operations is the union of all files explicitly specified\nand all of the files they require. This is the core of a build process, since this is\nhow you select only the set of files you need. So, if you have a small set of top-level\nfiles to start the process, say the class MyApp.App, you can do something like this:

\n\n
sencha compile -classpath=sdk/src,app \\\n    union -r -class MyApp.App and \\\n    ...\n
\n\n

The union command starts with no current set, includes only the class MyApp.App and\nthen proceeds to include all the things it needs recursively. The resulting current set\nis all files needed by the application.

\n\n

Intersect (Strict)

\n\n

The intersect command is a bit less flexible in the criteria it supports: it only\naccepts named sets (using -set).

\n\n
sencha compile -classpath=sdk/src,common,page1/src,page2/src \\\n    ... \\\n    intersect -set page1,page2 and \\\n    ... \\\n
\n\n

This command above intersects the two page sets and produces their intersection as the\ncurrent set.

\n\n

Intersect (Fuzzy)

\n\n

When dealing with more than two sets, intersect has an option called -min that sets\nthe threshold for membership in the current set. This option is discussed in more detail\nin Multi-Page Ext JS Apps.

\n\n

For example,

\n\n
sencha compile ... \\\n    intersect -min=2 -set page1,page2,page3 and \\\n    ...\n
\n\n

This use of intersect produces in the current set all files that are found in two of\nthe three sets specified.

\n\n

Compiler Directives

\n\n

In many situations, it is helpful to embed metadata in files that only the compiler will\npick up. To do this, the compiler recognizes special line comments as directives.

\n\n

The list of directives is:

\n\n\n\n\n

Character Encoding

\n\n

There is no standard way to specify the character encoding of a particular JS file. The\nSencha Cmd compiler, therefore, understands the following directive:

\n\n
//@charset ISO-9959-1\n
\n\n

This must be the first line of the JS file. The value to the right of charset can be any\nvalid Java charset\nname. The default is \"UTF-8\".

\n\n

The charset directive is used to describe the encoding of an input file to the compiler.\nThis does not effect the encoding of the output file. The content of the input file is\nconverted to Unicode internally.

\n\n

Tagging

\n\n

In an ideal world, a namespace would be sufficient to define a set of interest. Sometimes,\nhowever, a set can be quite arbitrary and even cross namespace boundaries. Rather than\nmove this issue to the command-line level, the compiler can track arbitrary tags in files.

\n\n

Consider the example:

\n\n
//@tag foo,bar\n
\n\n

This assigns the tags foo and bar to the file. These tags can be used in the include,\nexclude and union commands with their -tag option.

\n\n

Dealing with \"Other\" JavaScript Files

\n\n

In some cases, JavaScript files define classes or objects and require classes or objects\nthat are not expressed in terms of Ext.define and requires or Ext.require. Using\nExt.define you can still say that a class requires such things and the dynamic loader\nwill not complain so long as those things exist (if they do not exist, the loader will\ntry to load them, which will most likely fail).

\n\n

To support arbitrary JavaScript approaches to defining and requiring types, the compiler\nalso provides these directives:

\n\n
//@define Foo.bar.Thing\n//@requires Bar.foo.Stuff\n
\n\n

These directives set up the same basic metadata in the compiler that tracks what file\ndefines a type and what types that file requires. In most ways, then, these directives\naccomplish the same thing as an Ext.define with a requires property.

\n\n

You can use either of these directives in a file without using the other.

\n\n

Conditional Compilation

\n\n

The compiler supports conditional compilation directives, such as the one illustrated here:

\n\n
foo: function () {\n    //<debug>\n    if (sometest) {\n        Ext.log.warn(\"Something is wrong...\");\n    }\n    //</debug>\n\n    this.bar();\n}\n
\n\n

This may be the most useful of the conditional directives, which you'd use for code that\nyou want to run in a development environment but not in production.

\n\n

Important. When you use conditional compilation, remember that unless you always run\ncompiled code, the directives are just comments and the conditional code will be \"live\"\nduring development.

\n\n

The debug directive

\n\n

When compiling, by default, none of the preprocessor statements are examined. So in this\ncase, the result is development mode. If we switch on -debug we get a very similar\nresult, but with the preprocessor active. In fact, the only difference is that the\npreprocessor directives are removed.

\n\n

For example, this command:

\n\n
sencha compile -classpath=... \\\n    -debug \\\n    ...\n
\n\n

generates code like this:

\n\n
foo: function () {\n    if (sometest) {\n        Ext.log.warn(\"Something is wrong...\");\n    }\n\n    this.bar();\n}\n
\n\n

However, this command:

\n\n
sencha compile -classpath=... \\\n    -debug=false \\\n    ...\n
\n\n

generates code like this:

\n\n
foo: function () {\n    this.bar();\n}\n
\n\n

You can see that the if test and the log statement are both removed.

\n\n

The if directive

\n\n

The most general directive is if. The if directive tests one or more configured\noptions against the attributes of the directive and removes the code in the block\nif any are false.

\n\n

For example:

\n\n
//<if debug>\n//</if>\n
\n\n

This is equivalent to the <debug> directive.

\n"});