Skip to content

Instantly share code, notes, and snippets.

@jussi-kalliokoski
Created July 3, 2014 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jussi-kalliokoski/6e0bf476760d254e5465 to your computer and use it in GitHub Desktop.
Save jussi-kalliokoski/6e0bf476760d254e5465 to your computer and use it in GitHub Desktop.
Modules without circular dependencies
// localforage.js
// if the module exports a promise, the loader waits until the promise resolves into something,
// and that is what gets imported outside.
export System.import("./IndexedDbDriver")
.catch(=> System.import("./WebSqlDriver") )
.catch(=> System.import("./LocalStorageDriver") );
// IndexedDbDriver.js
// single exports only, so this works
import indexedDb from "indexedDb";
export class IndexedDbDriver {
static getItem () { ... }
static setItem () { ... }
static clear () { ... }
}
// WebSqlDriver.js
// single exports only, which means that normal destructuring works, so multiple named imports are possible.
import { openDatabase: open } from "WebSql";
export class WebSqlDriver {
...
}
// LocalStorageDriver.js
import localStorage from "localStorage";
export class LocalStorageDriver {
...
}
// UserService.js
import { setItem, getItem } from "localforage";
import fetch from "fetch";
export class UserService {
static getLogin () { ... }
static getLoginFromCache () { ... }
static getLoginFromNetwork () { ... }
static cacheLogin () { ... }
}
// single export, CJS
var something = require("somewhere");
var optionalDependency;
try {
optionalDependency = require("optionalDependency");
} catch (error) {}
module.exports = function doSomething () {
...
};
// single exports, transpiled
export async function (module) {
var exports = module.exports;
var something = await System.import("somewhere");
var optionalDependency;
try {
optionalDependency = await System.import("optionalDependency");
} catch (error) {}
module.exports = function doSomething () {
...
};
return module.exports;
}({ exports: {} });
// multiple exports, CJS
var something = require("somewhere");
exports.foo = something(1);
exports.bar = something(2);
// multiple exports, transpiled and optimized
import something from "somewhere";
var module = { exports: {} };
var exports = module.exports;
exports.foo = something(1);
exports.bar = something(2);
export module.exports;
// single exports, AMD
define("foo", function (bar, baz) {
return function foo () {
...
};
});
// single exports, transpiled
import bar from "bar";
import baz from "baz";
exports function foo () {
...
};
// multiple exports, AMD
define("foo", function (something) {
return {
bar: function bar () { ... },
baz: function baz () { ... }
};
});
// multiple exports, transpiled
import something from "something";
export {
bar: function bar () { ... },
baz: function baz () { ... }
};
@mariusGundersen
Copy link

While I do like the idea of using promises (and, in ES7, async code), there are some edge cases in ES6 where things behave differently from expected.

For example, while single import of a promise can be sugared to wait until the promise resolves, this won't work with (destructuring) multiple import/export. For example:

//exportingModule.js
export {
  a: System.import("something"),
  b: System.import("somethingElse")
}

//importingModule.js
import {a, b} from "exportingModule";
//a and b are both promises, not the value the promises eventually resolve to

Well, unless there is a different type of destructuring here which waits until all destructured promises are resolved, but that seems like a difficult thing to implement, given that a promise can resolve to a promise, and because the destructuring can be arbitrarily complex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment