Now that you have introduced code splitting, you should consider a few aspects:
- Minification,
- Deployment and URLs.
Although Haxe's generated JavaScript is reasonably efficient, it is also very friendly to minification, so you really want to add a minification step even if your server has gzip enabled.
Minification will easily reduce the JavaScript payload by 30 to 40%, and is perfectly recommendable on Modular projects.
Modular has been used with 2 industry-standard tools:
- Uglify-JS - including mangle option
- Google Closure Compiler - "simple" mode
Both generally work fine and give you similar output size, but Closure is known to be much heavier and slower, and additionally we've found rare cases of erroneous optimisation.
Although you can proceed to minify each JavaScript file yourself using your own scripts, you can also apply the minification automatically using Haxe libraries:
- Uglifyjs haxelib:
- install
uglify-js
from NPM, - install
uglifyjs
from Haxelib, - add
-lib uglifyjs
to your release build.
- install
- Closure haxelib:
- install
closure
from Haxelib (Closure is included), - add
-lib closure
to your release build.
- install
These 2 libraries will be automatically piloted by Modular to apply the minification to each bundle.
To only compress your build for release you can specify arguments in the command line:
# Debug build
haxe build.hxml -debug
# Release build
haxe build.hxml -lib uglifyjs
haxe build.hxml -lib closure
Each JavaScript bundle is loaded as a file, using a <script>
tag. By default, files
are loaded relatively to the HTML page as ./{bundleName}.js
.
Interally, bundles loading uses Modular's Require
class, and this class has a few
properties you can change to adjust to your deployment structure.
The real URL of the bundles is then: Require.jsPath + bundleName + Require.jsExt
Require.jsPath = './'; // default
Require.jsPath = '/scripts/'; // subfolder
Require.jsPath = 'https://path-to-static-domain/'; // other domain
Require.jsExt = '.js'; // default
Require.jsPath = '.js?c=' + Math.random(); // cache buster
Require.jsPath = '.js?v=' + VERSION; // versioning