Skip to content

Commit

Permalink
sync-import tests are now working -- we still need to confirm that 'd…
Browse files Browse the repository at this point in the history
…ist/index.js' (for NPM users) and 'dist/sync-import.js' (for use within browsers) also work correctly.
  • Loading branch information
dchambers committed Jun 9, 2015
1 parent 91ceb8c commit 92b3dbc
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modules
7 changes: 7 additions & 0 deletions .travis.yml
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
36 changes: 36 additions & 0 deletions package.json
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"
}
}
71 changes: 71 additions & 0 deletions src/URLUtils.js
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;
26 changes: 26 additions & 0 deletions src/syncImport.js
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;
}
19 changes: 19 additions & 0 deletions test/spec.js
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');
});
});

0 comments on commit 92b3dbc

Please sign in to comment.