Ext.data.JsonP.command_compiler({"title":"Sencha Compiler Reference","guide":"
Contents
\n\nOne 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\nBefore using the compiler, you should understand the basics of Sencha Cmd by reading the\nfollowing guides:
\n\n\n\n\nUnder 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
:
sencha compile -classpath=sdk/src,app ...\n
\n\nIn 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\").
The default classpath used by the compiler comes from these configuration properties:
\n\n${framework.classpath},${workspace.classpath},${app.classpath}\n
\n\nThe 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.
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.
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.
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:
-compress
- Compress the generated file using the default compressor. Currently this\nis the same as -yui
.-max
- Compress the generated file using all compressors and keep the smallest.-closure
- Compress the generated file using Google Closure Compiler.-uglify
- Compress the generated file using UglifyJS.-yui
- Compress the source file using YUI Compressor.-strip
- Strip comments from the output file, but preserve whitespace. This is the\noption used to convert \"ext-all-debug-w-comments.js\" into \"ext-all-debug.js\".The following command illustrates how to produce three flavors of output given a single\nread of the source.
\n\nsencha 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\nThe 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\nWhen 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\nsencha compile -classpath=sdk/src \\\n exclude -namespace Ext.chart and \\\n save nocharts and \\\n ...\n restore nocharts and \\\n ...\n
\n\nThe
savecommand takes a snapshot of the current set and stores it under the given name\n(
nocharts` in the above).
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
.
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\nThere are three classic set operations:
\n\nIntersection - The intersection of two sets is a set containing only what was in both\nsets.\n
Union - The union of two sets is a set containing whatever was in either of the sets.\n
Difference - The difference of two sets is the set of all things in the first set that\nare not in the second set.\n
include
and exclude
These two set operations are probably the most common (and flexible) set operations. Both\nsupport these basic switches:
\n\n-namespace
- Matches files that define types in the specified namespace.-class
- Matches a specific defined type.-file
- Matches filenames and/or folder names using Ant-style glob patterns (a \"*\"\nmatches only filename characters, where \"**\" matches folders).-tag
- Matches any files with the specified tag(s) (see below).-set
- The files that are present in any of the specified named sets.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.
So, let's start with a simple example and build an \"ext-all-no-charts-debug-w-comments.js\"
.
sencha compile -classpath=sdk/src \\\n exclude -namespace Ext.chart and \\\n ...\n
\n\nWhat 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.
include
and exclude
with -not
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.
For example:
\n\nsencha compile -classpath=sdk/src,js \\\n ... \\\n exclude -not -namespace Ext and \\\n ...\n
\n\nThe above exclude
command will exclude from the current set any classes that are not in\nthe Ext
namespace.
all
SetIn 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:
sencha compile -classpath=sdk/src \\\n ... \\\n include -all and \\\n ... \\\n exclude -all and \\\n ...\n
\n\nAfter the include -all
, the current set is all files. After exclude -all
it is the\nempty set.
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.
The union
command has all of the options of include
. Consider this union
command:
sencha compile -classpath=sdk/src ... and \\\n union -namespace Ext.grid,Ext.chart and \\\n ...\n
\n\nIt is exactly equivalent to this pair of exclude
and include
commands:
sencha compile -classpath=sdk/src ... and \\\n exclude -all and \\\n include -namespace Ext.grid,Ext.chart and \\\n ...\n
\n\nOne 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:
sencha compile -classpath=sdk/src,app \\\n union -r -class MyApp.App and \\\n ...\n
\n\nThe 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.
The intersect
command is a bit less flexible in the criteria it supports: it only\naccepts named sets (using -set
).
sencha compile -classpath=sdk/src,common,page1/src,page2/src \\\n ... \\\n intersect -set page1,page2 and \\\n ... \\\n
\n\nThis command above intersects the two page sets and produces their intersection as the\ncurrent set.
\n\nWhen 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.
For example,
\n\nsencha compile ... \\\n intersect -min=2 -set page1,page2,page3 and \\\n ...\n
\n\nThis use of intersect
produces in the current set all files that are found in two of\nthe three sets specified.
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\nThe list of directives is:
\n\n//@charset
//@tag
//@define
//@require
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\nThis 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\".
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.
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\nConsider the example:
\n\n//@tag foo,bar\n
\n\nThis 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.
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).
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\nThese 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.
You can use either of these directives in a file without using the other.
\n\nThe compiler supports conditional compilation directives, such as the one illustrated here:
\n\nfoo: function () {\n //<debug>\n if (sometest) {\n Ext.log.warn(\"Something is wrong...\");\n }\n //</debug>\n\n this.bar();\n}\n
\n\nThis 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\nImportant. 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\nWhen 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.
For example, this command:
\n\nsencha compile -classpath=... \\\n -debug \\\n ...\n
\n\ngenerates code like this:
\n\nfoo: function () {\n if (sometest) {\n Ext.log.warn(\"Something is wrong...\");\n }\n\n this.bar();\n}\n
\n\nHowever, this command:
\n\nsencha compile -classpath=... \\\n -debug=false \\\n ...\n
\n\ngenerates code like this:
\n\nfoo: function () {\n this.bar();\n}\n
\n\nYou can see that the if
test and the log statement are both removed.
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.
For example:
\n\n//<if debug>\n//</if>\n
\n\nThis is equivalent to the <debug>
directive.