Skip to content
This repository has been archived by the owner on Jul 13, 2020. It is now read-only.

Commit

Permalink
update test register loader
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Sep 1, 2016
1 parent a65b287 commit 71b4b6e
Showing 1 changed file with 10 additions and 28 deletions.
38 changes: 10 additions & 28 deletions test/fixtures/system-register-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ SystemRegisterLoader.prototype = Object.create(RegisterLoader.prototype);

// normalize is never given a relative name like "./x", that part is already handled
// so we just need to do plain name detect to throw as in the WhatWG spec
SystemRegisterLoader.prototype.normalize = function(key, parent, metadata) {
SystemRegisterLoader.prototype[RegisterLoader.normalize] = function(key, parent, metadata) {
var resolved = RegisterLoader.prototype.normalize.call(this, key, parent, metadata);
if (!resolved)
throw new RangeError('System.register loader does not resolve plain module names, resolving "' + key + '" to ' + parent);
Expand All @@ -40,18 +40,23 @@ var fs;

// instantiate just needs to run System.register
// so we load the module name as a URL, and expect that to run System.register
SystemRegisterLoader.prototype.instantiate = function(key, metadata) {
SystemRegisterLoader.prototype[RegisterLoader.instantiate] = function(key, metadata) {
var thisLoader = this;

return new Promise(function(resolve, reject) {
if (isNode)
Promise.resolve(fs || (fs = typeof require !== 'undefined' ? require('fs') : loader.import('fs').then(m => m.default)))
Promise.resolve(fs || (fs = typeof require !== 'undefined' ? require('fs') : loader.import('fs').then(function(m){ return m.default })))
.then(function(fs) {
fs.readFile(fileUrlToPath(key), function(err, source) {
if (err)
return reject(err);

(0, eval)(source.toString());
// Strip Byte Order Mark out if it's the leading char
var sourceString = source.toString();
if (sourceString[0] === '\ufeff')
sourceString = sourceString.substr(1);

(0, eval)(sourceString);
thisLoader.processRegisterContext(key);
resolve();
});
Expand All @@ -66,29 +71,6 @@ SystemRegisterLoader.prototype.instantiate = function(key, metadata) {
});
};

function nodeFetch(url, authorization, fulfill, reject) {
if (url.substr(0, 8) != 'file:///')
throw new Error('Unable to fetch "' + url + '". Only file URLs of the form file:/// allowed running in Node.');
fs = fs || module.require('fs');
if (isWindows)
url = url.replace(/\//g, '\\').substr(8);
else
url = url.substr(7);
return fs.readFile(url, function(err, data) {
if (err) {
return reject(err);
}
else {
// Strip Byte Order Mark out if it's the leading char
var dataString = data + '';
if (dataString[0] === '\ufeff')
dataString = dataString.substr(1);

fulfill(dataString);
}
});
}

function scriptLoad(src, resolve, reject) {
var script = document.createElement('script');
script.type = 'text/javascript';
Expand Down Expand Up @@ -118,4 +100,4 @@ function scriptLoad(src, resolve, reject) {
}
}

export default SystemRegisterLoader;
export default SystemRegisterLoader;

0 comments on commit 71b4b6e

Please sign in to comment.