Repository navigation
02. Module initialisation
######RATIONALE
Importantly, modules are initialised when first required in an executed module, and NOT when they are fetched. This behaviour saves memory, but is also more consistent with Node.js. Also, modules are initialised only once (unlike other browser-side requirers). Any reference to a module that resolves to an address will be the same module throughout the entire structure that was spawned from the same loader (original HTML file).
######MECHANISM
When a module is defined, it is stored as an unexectured function with the parameters
- require
- exports
- module
- __filename
- __dirname
The function is formed in the Global scope and is bound to exports (ie exports is its this). This is consistent with Node.js usage.
The require parameter closes over a function that tells NodalRequire to initialise the module and return a reference to the module.exports. If the module is already initialised, then only the reference to module.exports is returned.
######ENVIRONMENTAL INCONSISTENCIES
The initialiser does not provide some Node.js references:
-
process:NodeJS.Process, etc global:NodeJS.GlobalsetImmediate:(callback: (...args: any[]) => void, ...args: any[])=>anyclearImmediate:(immediateId: any)=>void
Some of these are emerging ECMA standards (setImmediate, clearImmediate), others (process, etc) are not currently feasible, and global is currently left for implementation within a script that can be loaded before any others:
eg
// Add in abstraction layer:
var global = window;