Package detail

system

gutentags89.7kMIT2.0.1

Flexible module and resource system

module, resource, system, commonjs

readme

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 = require("system");
System.loadSystem(location)
.then(function (system) {
    return system.import("./entry");
});

To load in a browser during development:

<script src="node_modules/system/boot.js" data-import="./entry"></script>

If the root of the package is a different directory, the module loader will need to locate it.

<script
    src="node_modules/system/boot.js"
    data-import="./entry"
    data-package="../"
></script>

To bundle for deployment:

sysjs entry.js > bundle.js

Then to load in production:

<script src="bundle.js"></script>

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.

{
  "dependencies": {
    "gutentag": "^2.2.0"
  },
  "extensions": {
    "html": "gutentag/extension"
  },
  "redirects": {
    "./main.html": "./play.html"
  },
  "scripts": {
    "build": "sysjs index.js > bundle.js"
  }
}

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.analyze = function analyze(module) {
    module.model = JSON.parse(module.text);
    module.dependencies = Object.keys(module.model);
};

exports.translate = function translate(module) {
    module.text = module.dependencies.map(function (id) {
        return (
            "exports[" + JSON.stringify(module.model[id]) + "] = " +
            "require(" + JSON.stringify(id) + ");\n"
        );
    }).join("");
};

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 = module.system;

exports.analyze = function (module) {
    host.introduce(module.system, "utility");
    module.dependencies.push("utility");
};

exports.translate = function (module) {
    module.text = "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.

changelog

2.0

  • 0
    • Adds support for "introduction", whereby an extension can introduce a generated module to a dependency not listed in its package.json.
    • Removes support for "translators", "analyzers", and "compilers" plugins. Only "extensions" plugins are supported, which can export "analyze" and "translate" hooks. Compilers are removed entirely since they were never supported in bundles.
    • The boot script now depends on the global URL and Promise objects instead of using Q promises and a trick with base and anchor tags for URL resolution.
    • The executable bundler is now called wc for "web compiler", like cc but funny maybe.
  • 1
    • Fixed wc, makes it jscat, before anyone notices that wc was a terrible idea.

1.3

  • 0
    • Adds support for npm version 3.

1.2

  • 0
    • Adds a sysjs alias for bundle, since the latter is too generic.
  • 1
    • Fixes a bug with extensions and redirects.

1.1

  • 0
    • Adds support for consolidated "extensions" instead of "analyzers" and "translators".
    • Removes documentation for "compilers", since these do not play well with the bundler.
  • 1
    • Updates the boot script.

1.0

  • 0
    • Fork Mr.
    • Basic support for browser and bundler.
    • Develop for Gutentag HTML to JavaScript code generator.
    • Add support for "main" module.
  • 1
    • Fixes sourceURL in generated JavaScript.
    • Fixes a race between cyclic dependencies.
  • 2
    • Fixes the bundle script, making it executable.
  • 3
    • Fixes a bug with cross-package module identifier normalization.
  • 4
    • Improves error messages
    • Explicitly distinguishes internal and external module identifier redirects.
    • The developer mode boot script now exposes the module system as window.system. Behavior if there are multiple boot scripts is not deteriministic.
  • 5
    • Modernizes sourceURL notation.
    • Dependency resolution errors now cause the bundler to fail loudly.
  • 6
    • Updates the boot script.