Skip to content
This repository has been archived by the owner on Jul 3, 2019. It is now read-only.

Build System

mbebenita edited this page Mar 4, 2013 · 6 revisions

The goal of the build system is to package all Shumway files into a single minified JavaScript file. The extension/firefox/build.js script is responsible for this. You can run the build script using:

node build.js content/web/scripts.js

If you open up the scripts.js file you'll notice that it's just a regular JavaScript file. This file is parsed by the build.js script which performs several steps:

  • Replace identifiers: $DEBUG becomes true / false, or $SHUMWAY_HOME is replaced with the appropriate absolute path. (Note: All environment variables are made available and expanded in JavaScript files.)
  • Fold constant expressions: 1 + 1 becomes 2.
  • Fold logical expressions: true || ... becomes true.
  • Remove assertions: assert(x, ...).
  • Replace load(file | directory) with the contents of the included file or those of the files in the specified directory. This process is recursive, so all the previous steps execute all over again.

How does it work?

The build script uses esprima to parse all JavaScript files, estransform to apply AST transformations on the parsed tree, and finally escodegen to serialize the AST. As an additional step, the build script can also call the closure compiler to further optimize the code. For instance if you write the following code:

if ($DEBUG) {
  ...
}

the build script may produce

if (false) {
  ...
}

and finally, the closure compiler removes the entire if expression.

Warning

The build script also replaces all identifiers that it thinks are constant XYZ... or XYZ_abc... with their initializer.

var OP_x = 1, OP_y = 2;
switch (op) {
  case OP_x: ... break;
  case OP_y: ... break;
}

becomes

var OP_x = 1, OP_y = 2;
switch (op) {
  case 1: ... break;
  case 2: ... break;
}

This is VERY unsound, no clever scoping is used so be VERY careful.