This repository has been archived by the owner on Jul 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
Load implementation #39
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8e84c8f
Load implementation. Fixes #35.
bryanforbes 3d331fd
Pass `require` instead of `module` and style changes
bryanforbes f34e2ee
Add unit tests and improve implementation based on the results
bryanforbes da89f25
Make the require argument optional
bryanforbes 338ea3e
Update typings and add reference to loader issue
bryanforbes File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import Promise from './Promise'; | ||
|
||
declare var define: { | ||
(...args: any[]): any; | ||
amd: any; | ||
}; | ||
|
||
export interface AMDRequire { | ||
(moduleIds: string[], callback: (...modules:any[]) => void): void; | ||
} | ||
export interface NodeRequire { | ||
(moduleId: string): any; | ||
} | ||
export type Require = AMDRequire | NodeRequire; | ||
|
||
export interface Load { | ||
(require: Require, ...moduleIds: string[]): Promise<any[]>; | ||
(...moduleIds: string[]): Promise<any[]>; | ||
} | ||
|
||
const load: Load = (function (): Load { | ||
if (typeof module === 'object' && typeof module.exports === 'object') { | ||
return function (contextualRequire: any, ...moduleIds: string[]): Promise<any[]> { | ||
if (typeof contextualRequire === 'string') { | ||
moduleIds.unshift(contextualRequire); | ||
contextualRequire = require; | ||
} | ||
return new Promise(function (resolve, reject) { | ||
try { | ||
resolve(moduleIds.map(function (moduleId): any { | ||
return contextualRequire(moduleId); | ||
})); | ||
} | ||
catch (error) { | ||
reject(error); | ||
} | ||
}); | ||
}; | ||
} | ||
else if (typeof define === 'function' && define.amd) { | ||
return function (contextualRequire: any, ...moduleIds: string[]): Promise<any[]> { | ||
if (typeof contextualRequire === 'string') { | ||
moduleIds.unshift(contextualRequire); | ||
contextualRequire = require; | ||
} | ||
return new Promise(function (resolve) { | ||
// TODO: Error path once https://github.com/dojo/loader/issues/14 is figured out | ||
contextualRequire(moduleIds, function (...modules: any[]) { | ||
resolve(modules); | ||
}); | ||
}); | ||
}; | ||
} | ||
else { | ||
return function () { | ||
return Promise.reject(new Error('Unknown loader')); | ||
}; | ||
} | ||
})(); | ||
export default load; |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import assert = require('intern/chai!assert'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. |
||
import registerSuite = require('intern!object'); | ||
import has from 'src/has'; | ||
import load from 'src/load'; | ||
import Promise from 'src/Promise'; | ||
|
||
const suite: any = { | ||
name: 'load', | ||
|
||
'global load'() { | ||
var def = this.async(5000); | ||
|
||
load('src/has', 'src/Promise').then(def.callback(function ([ hasModule, promiseModule ]: [ any, any ]) { | ||
assert.strictEqual(hasModule.default, has); | ||
assert.strictEqual(promiseModule.default, Promise); | ||
})); | ||
}, | ||
|
||
'contextual load'() { | ||
var def = this.async(5000); | ||
|
||
load(require, './load/a', './load/b').then(def.callback(function ([ a, b ]: [ any, any ]) { | ||
assert.deepEqual(a, { one: 1, two: 2 }); | ||
assert.deepEqual(b, { three: 3, four: 4 }); | ||
})); | ||
} | ||
|
||
// TODO: once AMD error handling is figured out, add tests for the failure case | ||
}; | ||
|
||
if (has('host-node')) { | ||
const nodeRequire: any = global.require.nodeRequire; | ||
const path: any = nodeRequire('path'); | ||
const buildDir: string = path.join(process.cwd(), '_build'); | ||
|
||
suite.node = { | ||
'different than AMD load'() { | ||
const nodeLoad: typeof load = nodeRequire(path.join(buildDir, 'src', 'load')).default; | ||
assert.notStrictEqual(nodeLoad, load); | ||
}, | ||
|
||
'global load succeeds'() { | ||
var def = this.async(5000); | ||
|
||
var result: Promise<any[]> = nodeRequire(path.join(buildDir, 'tests', 'unit', 'load', 'node')).globalSucceed; | ||
result.then(def.callback(function ([ fs, path ]: [ any, any ]) { | ||
assert.strictEqual(fs, nodeRequire('fs')); | ||
assert.strictEqual(path, nodeRequire('path')); | ||
})); | ||
}, | ||
|
||
'global load with relative path fails'() { | ||
var def = this.async(5000); | ||
|
||
var result: Promise<any[]> = nodeRequire(path.join(buildDir, 'tests', 'unit', 'load', 'node')).globalFail; | ||
result.then(function () { | ||
def.reject(new Error('load should not have succeeded')); | ||
}, def.callback(function (error: Error) { | ||
assert.instanceOf(error, Error); | ||
})); | ||
}, | ||
|
||
'contextual load succeeds'() { | ||
var def = this.async(5000); | ||
|
||
var result: Promise<any[]> = nodeRequire(path.join(buildDir, 'tests', 'unit', 'load', 'node')).succeed; | ||
result.then(def.callback(function ([ a, b ]: [ any, any ]) { | ||
assert.deepEqual(a, { one: 1, two: 2 }); | ||
assert.deepEqual(b, { three: 3, four: 4 }); | ||
})); | ||
}, | ||
|
||
'contextual load with non-existent module fails'() { | ||
var def = this.async(5000); | ||
|
||
var result: Promise<any[]> = nodeRequire(path.join(buildDir, 'tests', 'unit', 'load', 'node')).fail; | ||
result.then(function () { | ||
def.reject(new Error('load should not have succeeded')); | ||
}, def.callback(function (error: Error) { | ||
assert.instanceOf(error, Error); | ||
})); | ||
} | ||
}; | ||
} | ||
|
||
registerSuite(suite); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const one = 1; | ||
export const two = 2; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const three = 3; | ||
export const four = 4; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import load from '../../../src/load'; | ||
|
||
export const succeed = load(require, './a', './b'); | ||
export const fail = load(require, './a', './c'); | ||
|
||
export const globalSucceed = load('fs', 'path'); | ||
export const globalFail = load('fs', './a'); |
This file contains 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
This file contains 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am missing the "value" of the dual wrap of the
hostLoad
and theload
in separate closures. I mean the code is right, but it seems to add another layer that appears to be somewhat unnecessary, especially because hostLoad doesn't get exported.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't want to repeat the code for calculating the absolute module IDs, but it's not necessary if
require
is passed instead ofmodule
. Ignore this.