Skip to content
This repository has been archived by the owner on Jul 13, 2020. It is now read-only.

Latest commit

 

History

History
40 lines (30 loc) · 2.2 KB

system-register-dynamic.md

File metadata and controls

40 lines (30 loc) · 2.2 KB

System.registerDynamic

Like System.register, System.registerDynamic is a wrapper format to ensure the exact linking semantics of modules in all environments, such that it can wrap CommonJS, AMD and global modules.

Babel transforms can be used to convert these formats into System.registerDynamic. Currently the existing transforms are still coupled to SystemJS as they rely on special helper functions and modules in SystemJS, but the goal is to make these fully independent transformers in future:

Like System.register, System.registerDynamic has both an anonymous and named form, by making the first string key argument optional. This allows it to work as both a bundle transport format and a wrapper.

The format of System.registerDynamic is designed to closely match CommonJS:

System.registerDynamic('optional-name', ['unnormalized-dependency'], true, function (require, exports, module) {
  // require is executing - the dependency is only executed when we hit this require
  // Note that we can only require modules that have already been declared through the dependencies
  // array above. This is because synchronous module instantiation is not supported in the loader.
  var dep = require('unnormalized-dependency');

  // named exports
  exports.someExport = 'export';
  // Like CommonJS, "this" is the module.exports
  this.export = 'another';

  // module.id can be read as a string (but it is a file:/// URL)
  var thisModuleKey = module.id;

  // module exports object can even be set directly
  module.exports = 'can even assign module exports';
});

The resultant ModuleNamespace object is taken to be the object with the default export equal to the module.exports value from the CommonJS module, with this property set to the exports object from the beginning of the linking phase.