System
This is a CommonJS/npm compatible module system. It works both client-side and server-side in Node.js. For browsers, it supports refresh-to-reload debugging, as well as a build step comparable to Browserify to produce bundles for production. The System module loader can resolve both module and resource locations by module identifier across package boundaries.
In addition, System adds support for configuring module translators (text to JavaScript text) and dependency analyzers.
Examples of usage
npm init
npm install --save system
To load in Node.js:
var System = ;System;
To load in a browser during development:
If the root of the package is a different directory, the module loader will need to locate it.
To bundle for deployment:
sysjs entry.js > bundle.js
Then to load in production:
Extensions
System supports plugins for translating modules to JavaScript, on the fly in
the browser or in the sysjs
build step.
The same module loader plugins can work for both development and production,
leaving little trace of the module system in the generate bundles.
Configure plugins with annotations in package.json
.
Extensions only apply within the scope of the packages that explicitly
configure them.
The following package uses the Guten Tag HTML to JavaScript extension.
Extensions are modules that implement any combination of analyze
and
translate
.
The analyze(module)
function takes the CommonJS module object and is
responsible for populating module.dependencies
with module references if the
module depends on other modules at run-time.
The analyzer may also leave annotations to the module
object that the
translate
function will be able to use.
The translate(module)
function takes the same CommonJS module object and is
responsible for converting module.text
from the language implied by its
module.extension
, rewrite that module.text
to JavaScript, and reassign the
module.extension
to "js"
.
The following extension converts a JSON document containing key-value pairs into a module that exports other modules.
exports { modulemodel = JSON; moduledependencies = Object;}; exports { moduletext = moduledependencies;};
Alterations made by the translator and analyzer to the module
object are not preserved in sysjs
build products, so they should be used only
to communicate with the module system.
Analyzers can also introduce a package to one of their own dependencies.
This is useful if generated code needs to use a library that the host package
does not directly depend upon.
The System module loader enforces dependency relationships between packages.
A package that is not mentioned in package.json
or expressly introduced
through the extension system cannot be loaded.
var host = modulesystem; exports { host; moduledependencies;}; exports { moduletext = "require(\"utility\")";};
History
This project started at Motorola Mobility with the work of Tom Robinson (@tlrobinson), originally called C.js. This became the foundation for module loading in Motorola Mobility's MontageJS web application framework, thus the name Montage Require, or Mr. Kris Kowal (@kriskowal) took responsibility for maintaining the library, converted it to use promises internally, and added support for loading packages installed by npm. Stuart Knightley (@stuk) took over responsibility for maintaining the library when work on MontageJS resumed at Montage Studio.
The System module loader is an iteration from that lineage, with a more focused scope, targetting npm packages more precisely, and adding support for configurable (per package in package.json) translators, compilers, and dependency analyzers.