ciena-blueplanet / dagre

Directed graph renderer for javascript
MIT License
36 stars 13 forks source link

Why Doesn't this project Use requires and module.exports? #13

Closed choucalate closed 7 years ago

choucalate commented 7 years ago

This is more of a question- ^

I'm looking at this for reference but I can't understand why this is doing it differently https://github.com/ciena-blueplanet/graphlib/blob/master/index.js

I'm trying to use this library with webpack, but it's not able to recognize the import keyword and it's exploding.

sandersky commented 7 years ago

@choucalate When using this with Webpack are you consuming the src directory or the lib directory? You'll notice when looking at this repository on Github the lib directory doesn't exist, just the src directory but if you look at it in your node_modules directory it'll contain lib as well. The src directory is written in ES6 but the lib directory is a Babel transpiled ES5 version of the code that should work with Node out-of-box. If you are simply consuming it via a require('ciena-graphlib') or import graphlib from 'ciena-graphlib' in your consuming code then you're probably consuming the lib/ES5 version which I don't think should contain import to begin with. In order to help more context would help, such as a link to your project if it is open source.

choucalate commented 7 years ago

Thanks @sandersky ! I'm simply using

import graphlib from 'ciena-dagre';

But it's picking up Uncaught SyntaxError: Unexpected token import

However, because either the package.json has a "main" key for "index.js" or it's automatically resolving to node_modules/ciena-dagre/index.js by order of resolution, it's picking up the ES6 version.

Unfortunately the project I'm working on isn't open source.

choucalate commented 7 years ago

Ohh seems like if I use it like this, it seems to work

import { layout } from 'ciena-dagre/lib';

Also, has the api changed since it was forked?

sandersky commented 7 years ago

I believe you can do

import layout from 'ciena-dagre'

or

import {graphlib} from 'ciena-dagre'

In the event you want both you can do:

import layout, {graphlib} from 'ciena-dagre'

The project is using ES6 exports and thus uses named exports for most things.

choucalate commented 7 years ago

Thanks @sandersky I think I understand it in concept. I think the problem is mostly with my own webpack configuration, where I think it needs to also use babel to transpile ciena-dagre in order for everything to work together. But perhaps the issue is that I'm excluding node_modules from babel-loader? I'm not exactly sure.

I appreciate the help, thanks!

joewood commented 7 years ago

I think there may be a problem here. The default entry point for the library is defined in package.json as:

 "main": "index.js",

But that file uses ES6 module syntax, so won't work unless you use babel and webpack. I think the entry point module should be using the require syntax:

import {debugOrdering} from './lib/debug'

export const debug = {
  debugOrdering
}

export {default as layout} from './lib/layout'

import {
  addBorderNode,
  addDummyNode,
  asNonCompoundGraph,
  buildLayerMatrix,