angularsystemjsdagre

Dagre Angular2 SystemJS module loading


Using Angular 2.4.0, tsc 2.3.1, I'm attempting to use Dagre with JointJS/graphlib for drawing graphs in Angular2 using SystemJS as a loader.

http://www.daviddurman.com/automatic-graph-layout-with-jointjs-and-dagre.html

I can't figure out how to get the 'dagre' object in the browser as required by JointJS var dagre = require('dagre');

My Angular2 component includes import * as dagre from 'dagre';

My SystemJS config:

(function () {
  System.config({
    map: {
        'dagre': '/static/libs/dagre/dagre.js'
    }
});
})();

Not being able to load dagre leads to an undefined error in JointJS when it attempts to call the dagre.layout: Cannot read property 'layout' of undefined

// Executes the layout.
dagre.layout(glGraph, { debugTiming: !!opt.debugTiming });

Maybe this is a problem with the library not exporting itself or do I have to specify a format in my SystemJS config?


Solution

  • So I'm not sure exactly what fixed this, but I did three things:

    1) npm install -D @types/dagre

    2) import * as dagre from dagre; in my component.ts

    3) added format: global to the SystemJS config

    (function () {
      System.config({
        map: {
            'lodash': '/static/libs/lodash/index.js',
            'dagre': '/static/libs/dagre/dagre.js',
            'graphlib': '/static/libs/graphlib/grpahlib.umd.js',
        },
        meta: {
          'dagre': {
             format: 'global',
             deps: ['lodash'],
          },
          'graphlib': {
             format: 'global',
             deps: ['lodash'],
          }
        }
    });
    })();