-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sync-import tests are now working -- we still need to confirm that 'd…
…ist/index.js' (for NPM users) and 'dist/sync-import.js' (for use within browsers) also work correctly.
- Loading branch information
Showing
6 changed files
with
161 additions
and
0 deletions.
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,2 @@ | ||
dist | ||
node_modules |
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 @@ | ||
language: node_js | ||
node_js: | ||
- "0.12.3" | ||
install: | ||
- npm install | ||
script: | ||
- npm test |
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,36 @@ | ||
{ | ||
"author": "Dominic Chambers", | ||
"name": "sync-import", | ||
"description": "Synchronous import of modules for SystemJS", | ||
"version": "0.0.0", | ||
"main": "dist/index.js", | ||
"keywords": [ | ||
"SystemJS", | ||
"synchronous", | ||
"import" | ||
], | ||
"homepage": "https://github.com/dchambers/sync-import", | ||
"bugs": { | ||
"url": "https://github.com/dchambers/sync-import/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/dchambers/sync-import.git" | ||
}, | ||
"scripts": { | ||
"prepublish": "mkdirp dist && babel src/syncImport.js -o dist/index.js && browserify -t babelify -e src/syncImport.js -o dist/sync-import.js -s syncImport", | ||
"test": "mocha --compilers js:babel/register src/URLUtils.js node_modules/systemjs/dist/system-csp-production.src.js test" | ||
}, | ||
"dependencies": { | ||
"bluebird": "^2.9.27" | ||
}, | ||
"devDependencies": { | ||
"babel": "^5.5.6", | ||
"babelify": "^6.1.2", | ||
"browserify": "^10.2.4", | ||
"chai": "^3.0.0", | ||
"mkdirp": "^0.5.1", | ||
"mocha": "^2.2.5", | ||
"systemjs": "^0.17.1" | ||
} | ||
} |
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,71 @@ | ||
/* | ||
* SystemJS Polyfills for URL and Promise providing IE8+ Support | ||
*/ | ||
// from https://gist.github.com/Yaffle/1088850 | ||
function URLPolyfill(url, baseURL) { | ||
if (typeof url != 'string') | ||
throw new TypeError('URL must be a string'); | ||
var m = String(url).replace(/^\s+|\s+$/g, "").match(/^([^:\/?#]+:)?(?:\/\/(?:([^:@\/?#]*)(?::([^:@\/?#]*))?@)?(([^:\/?#]*)(?::(\d*))?))?([^?#]*)(\?[^#]*)?(#[\s\S]*)?/); | ||
if (!m) { | ||
throw new RangeError(); | ||
} | ||
var protocol = m[1] || ""; | ||
var username = m[2] || ""; | ||
var password = m[3] || ""; | ||
var host = m[4] || ""; | ||
var hostname = m[5] || ""; | ||
var port = m[6] || ""; | ||
var pathname = m[7] || ""; | ||
var search = m[8] || ""; | ||
var hash = m[9] || ""; | ||
if (baseURL !== undefined) { | ||
var base = baseURL instanceof URLPolyfill ? baseURL : new URLPolyfill(baseURL); | ||
var flag = protocol === "" && host === "" && username === ""; | ||
if (flag && pathname === "" && search === "") { | ||
search = base.search; | ||
} | ||
if (flag && pathname.charAt(0) !== "/") { | ||
pathname = (pathname !== "" ? (((base.host !== "" || base.username !== "") && base.pathname === "" ? "/" : "") + base.pathname.slice(0, base.pathname.lastIndexOf("/") + 1) + pathname) : base.pathname); | ||
} | ||
// dot segments removal | ||
var output = []; | ||
pathname.replace(/^(\.\.?(\/|$))+/, "") | ||
.replace(/\/(\.(\/|$))+/g, "/") | ||
.replace(/\/\.\.$/, "/../") | ||
.replace(/\/?[^\/]*/g, function (p) { | ||
if (p === "/..") { | ||
output.pop(); | ||
} else { | ||
output.push(p); | ||
} | ||
}); | ||
pathname = output.join("").replace(/^\//, pathname.charAt(0) === "/" ? "/" : ""); | ||
if (flag) { | ||
port = base.port; | ||
hostname = base.hostname; | ||
host = base.host; | ||
password = base.password; | ||
username = base.username; | ||
} | ||
if (protocol === "") { | ||
protocol = base.protocol; | ||
} | ||
} | ||
|
||
// convert windows file URLs to use / | ||
if (protocol == 'file:') | ||
pathname = pathname.replace(/\\/g, '/'); | ||
|
||
this.origin = protocol + (protocol !== "" || host !== "" ? "//" : "") + host; | ||
this.href = protocol + (protocol !== "" || host !== "" ? "//" : "") + (username !== "" ? username + (password !== "" ? ":" + password : "") + "@" : "") + host + pathname + search + hash; | ||
this.protocol = protocol; | ||
this.username = username; | ||
this.password = password; | ||
this.host = host; | ||
this.hostname = hostname; | ||
this.port = port; | ||
this.pathname = pathname; | ||
this.search = search; | ||
this.hash = hash; | ||
} | ||
(typeof self != 'undefined' ? self : global).URLPolyfill = URLPolyfill; |
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,26 @@ | ||
'use strict'; | ||
|
||
import Promise from 'bluebird'; | ||
|
||
export default function syncImport(name) { | ||
let scheduledFuncs = []; | ||
let origScheduler = Promise.setScheduler((fn) => scheduledFuncs.push(fn)); | ||
let origPromise = global.Promise; | ||
|
||
try { | ||
global.Promise = Promise; | ||
var promise = System.import(name); | ||
|
||
for(var fn of scheduledFuncs) { | ||
fn(); | ||
} | ||
|
||
var value = promise.value(); | ||
} | ||
finally { | ||
Promise.setScheduler(origScheduler); | ||
global.Promise = origPromise; | ||
} | ||
|
||
return value; | ||
} |
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,19 @@ | ||
'use strict'; | ||
|
||
import syncImport from '../src/syncImport'; | ||
import chai from 'chai'; | ||
const expect = chai.expect; | ||
|
||
describe('syncImport()', function() { | ||
it("allows modules to be synchronously imported", function() { | ||
System.registerDynamic('A', [], true, function(require, exports, module) { | ||
module.exports = '@A'; | ||
return module.exports; | ||
}); | ||
const A = System.normalizeSync('A'); | ||
|
||
expect(System.get(A)).to.be.undefined; | ||
expect(syncImport('A')).to.equal('@A'); | ||
expect(System.get(A).default).to.equal('@A'); | ||
}); | ||
}); |