Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

systemjs-builder

Package Overview
Dependencies
Maintainers
1
Versions
139
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

systemjs-builder

SystemJS Build Tool

  • 0.14.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.3K
decreased by-79.64%
Maintainers
1
Weekly downloads
 
Created
Source

SystemJS Build Tool Build Status

As of SystemJS Builder 0.14, builder.build and builder.buildTree are both builder.bundle, while builder.buildSFX is now builder.buildStatic. The previous APIs will continue to work, but display deprecation warnings.

Note SystemJS Builder 0.11-0.14 correspond to the SystemJS 0.17+ releases which include the breaking change making module names URLs. Read the SystemJS 0.17 release notes for more information on this change.

Provides a single-file build for SystemJS of mixed-dependency module trees.

Builds ES6 into ES5, CommonJS, AMD and globals into a single file in a way that supports the CSP SystemJS loader as well as circular references.

Example

app.js

import $ from "./jquery.js";
export var hello = 'es6';

jquery.js

define(function() {
  return 'this is jquery';
});

Will build the module app into a bundle containing both app and jquery defined through System.register calls.

Circular references and bindings in ES6, CommonJS and AMD all behave exactly as they should, including maintaining execution order.

Usage

Install

npm install systemjs-builder

Basic Use

Ensure that the transpiler is installed separately (npm install babel-core here).

var path = require("path");
var Builder = require('systemjs-builder');

var builder = new Builder({
  baseURL: 'some/folder',

  // any map config
  map: {
    jquery: 'jquery-1.2.3/jquery'
  },
  
  // opt in to Babel for transpiling over Traceur
  transpiler: 'babel'

  // etc. any SystemJS config
})
.bundle('local/module.js', 'outfile.js')
.then(function() {
  console.log('Build complete');
})
.catch(function(err) {
  console.log('Build error');
  console.log(err);
});

Setting Configuration

To load a SystemJS configuration file, containing configure calls like:

System.config({ ... });

Then we can load this config file through the builder:

// `builder.loadConfig` will load config from a file
builder.loadConfig('./cfg.js')
.then(function() {
  // additional config can also be set through `builder.config`
  builder.config({ map: { 'a': 'b.js' } });

  return builder.bundle('myModule.js', 'outfile.js');
});

Multiple config calls can be run, which will combine into the loader configuration.

To reset the loader state and configuration use builder.reset().

Self-Executing (SFX) Bundles

To make a bundle that is independent of the SystemJS loader entirely, we can make SFX bundles:

builder.buildStatic('myModule.js', 'outfile.js', options);

This bundle file can then be included with a <script> tag, and no other dependencies would need to be included in the page.

By default, Traceur or Babel runtime are automatically included in the SFX bundle if needed. To exclude the Babel or Traceur runtime set the runtime build option to false:

builder.buildStatic('myModule.js', 'outfile.js', { runtime: false });
SFX Format

SFX bundles can also be output as a custom module format - amd, cjs or es6 for consumption in different environments.

This is handled via the sfxFormat option:

builder.buildStatic('myModule.js', 'outfile.js', { sfxFormat: 'cjs' });

The first module used as input (myModule.js here) will then have its exports output as the CommonJS exports of the whole SFX bundle itself when run in a CommonJS environment.

Adapter Modules

To have globals like jQuery not included, and included in a separate script tag, set up an adapter module something like:

jquery.js

module.exports = window.jQuery;

Minfication & Source Maps

As well as an options.config parameter, it is also possible to specify minification and source maps options:

builder.bundle('myModule.js', 'outfile.js', { minify: true, sourceMaps: true, config: cfg });

Compile time with source maps can also be improved with the lowResSourceMaps option:

builder.bundle('myModule.js', 'outfile.js', { sourceMaps: true, lowResSourceMaps: true });
Minification Options
  • mangle, defaults to true.
  • globalDefs, object allowing for global definition assignments for dead code removal.
builder.bundle('myModule.js', 'outfile.js', { minify: true, mangle: false, globalDefs: { DEBUG: false } });

In-Memory Builds

Leave out the outFile option to run an in-memory build:

builder.bundle('myModule.js', { minify: true }).then(function(output) {
  output.source;    // generated bundle source
  output.sourceMap; // generated bundle source map
  output.modules;   // array of module names defined in the bundle
});

The output object above is provided for all builds, including when outFile is set.

output.modules can be used to directly populate SystemJS bundles configuration.

Ignore Resources

If loading resources that shouldn't even be traced as part of the build (say an external import), these can be configured with:

builder.config({
  meta: {
    'resource/to/ignore.js': {
      build: false
    }
  }
});

Bundle Arithmetic

Both builder.build and builder.buildStatic support bundle arithmetic expressions. This allows for the easy construction of custom bundles.

NOTE: SFX Bundles can only use addition and wildcard arithmetic.

There is also a builder.trace for building direct trace tree objects, which can be directly passed into builder.bundle or builder.buildStatic.

Example - Arithmetic Expressions

In this example we build all our application code in app/ excluding the tree app/corelibs:

var Builder = require('systemjs-builder');

var builder = new Builder({
  baseURL: '...',
  map: {
  } // etc. config
});

builder.bundle('app/* - app/corelibs.js', 'output-file.js', { minify: true, sourceMaps: true });
Example - Common Bundles

To build the dependencies in common between two modules, use the & operator:

builder.bundle('app/page1.js & app/page2.js', 'common.js');

We can then exclude this common bundle in future builds:

builder.bundle('app/componentA.js - common.js', { minify: true, sourceMaps: true });
Example - Third-Party Dependency Bundles

Build a bundle of all dependencies of the app/ package excluding anything from app/ itself.

For this we can use the [module] syntax which represents a single module instead of all its dependencies as well:

builder.bundle('app/**/* - [app/**/*]', 'dependencies.js', { minify: true, sourceMaps: true });

The above means take the tree of app and all its dependencies, and subtract just the modules in app, thus leaving us with just the tree of dependencies of the app package.

Example - Direct Trace API

Instead of using the arithmetic syntax, we can construct the trace ourselves.

In this example we build app/first and app/second into two separate bundles, while creating a separate shared bundle:

var Builder = require('systemjs-builder');

var builder = new Builder({
  // ...
});

Promise.all([builder.trace('app/first.js'), builder.trace('app/second.js')])
.then(function(trees) {
  var commonTree = builder.intersectTrees(trees[0], trees[1]);
  return Promise.all([
    builder.bundle(commonTree, 'shared-bundle.js')
    builder.bundle(builder.subtractTrees(trees[0], commonTree), 'first-bundle.js'),
    builder.bundle(builder.subtractTrees(trees[1], commonTree), 'second-bundle.js')
  ]);
});

License

MIT

FAQs

Package last updated on 19 Sep 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc