-
Notifications
You must be signed in to change notification settings - Fork 581
Self Loading
ES6 defines a Loader object to load modules. Traceur is written in ES6. So how does traceur load itself?
The command ./traceur
runs node
and uses node's loader (require()
) to load src/node/command.js
.
src/node/command.js
processes the command line and loads src/node/traceur.js
.
src/node/traceur.js
reads the file bin/traceur.js
and applies eval()
to its content.
bin/traceur.js
starts with some non-transcoded files from src/runtime
, followed by lots of calls to ModuleStore.registerModule()
. Each call is the output of compiling one module definition. Thus static module compile into calls to ModuleStore.registerModule()
.
At the very end of bin/traceur.js
, a global variable traceur
is set to the module stored as a result of compiling src/traceur.js
. The access will trigger many module definition calls, that is calls to the functions stored when we called ModuleStore.registerModule()
. This traceur
global allows some access to traceur internals, including the Loader object.
Returning to src/node/command.js
, we finish processing the command line then call one of three functions to compile a file, a directory, or to interpret a module. These functions use the traceur object to access the Loader.
In node, the System
global is attached by require('./System')
from src/node/System.js
.
In a Web page, we simply include bin/traceur.js
which fills the ModuleStore
as above and evaluates traceur
at the end.
In a Web page, the System
global is attached by loading the System
module; this is done currently when traceur loads System
module, but that prevents alternative system implementations.