Created
July 3, 2014 10:44
-
-
Save jussi-kalliokoski/6e0bf476760d254e5465 to your computer and use it in GitHub Desktop.
Modules without circular dependencies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 () { ... } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 () { ... } | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
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.