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

Use API functions based on availability #228

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,14 @@ function logloads(loads) {
// Returns an array of ModuleSpecifiers
var traceur;
Loader.prototype.parse = function(load) {
// module already transpiled to es5?
if (load.source.startsWith('System.register')) {
load.isDeclarative = true;
var source = load.source;
__eval(source, __global, load);
return;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is in SystemJS in the register extension (https://github.com/systemjs/systemjs/blob/master/lib/extension-register.js#L458).

If you need that functionality, add just the register extension to the module loader from there.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, will try!

if (!traceur) {
if (typeof window == 'undefined' &&
typeof WorkerGlobalScope == 'undefined')
Expand Down
2 changes: 1 addition & 1 deletion src/polyfill-wrapper-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function __eval(__source, __global, load) {
// Hijack System.register to set declare function
var __curRegister = System.register;
System.register = function(name, deps, declare) {
if (typeof name != 'string') {
if (typeof name != 'string' && typeof name != 'void') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is outputting a void in the System.register name? That should never happen and if it does that is the wrong output mode in Traceur (this sounds like the Traceur register output).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, that is a good question. You're right, name should never be void, that would not make sense.
Maybe just assert that it is not void?

declare = deps;
deps = name;
}
Expand Down
18 changes: 12 additions & 6 deletions src/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
}

var fetchTextFromURL;
if (isBrowser || isWorker) {
if (typeof XMLHttpRequest != 'undefined') {
fetchTextFromURL = function(url, fulfill, reject) {
var xhr = new XMLHttpRequest();
var sameDomain = true;
Expand All @@ -72,7 +72,7 @@
sameDomain &= domainCheck[1] === window.location.protocol;
}
}
if (!sameDomain) {
if (!sameDomain && typeof XDomainRequest != 'undefined') {
xhr = new XDomainRequest();
xhr.onload = load;
xhr.onerror = error;
Expand Down Expand Up @@ -102,7 +102,7 @@
xhr.send(null);
}
}
else {
else if (typeof request != 'undefined') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this check necessary?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make sure that the functionality is actually there - there might be a lot of different environments, and I would check the availability of features on a per feature base.
if XMLHttpRequest exists -> use it.
if require exists -> assume node.js environment -> use http/fs lib.
if some future thing exists -> use that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So then shouldn't this be typeof require != 'undefined'?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yes, it should :)

var fs;
fetchTextFromURL = function(url, fulfill, reject) {
fs = fs || require('fs');
Expand All @@ -114,20 +114,26 @@
});
}
}
else {
throw new TypeError('No API available to load external resources');
}

class SystemLoader extends __global.LoaderPolyfill {

constructor(options) {
super(options || {});

// Set default baseURL and paths
if (isBrowser || isWorker) {
if (typeof __global.location != 'undefined' && typeof __global.location.href != 'undefined') {
var href = __global.location.href.split('#')[0].split('?')[0];
this.baseURL = href.substring(0, href.lastIndexOf('/') + 1);
}
else {
else if (typeof process != 'undefined' && typeof process.cwd != 'undefined') {
this.baseURL = process.cwd() + '/';
}
else {
this.baseURL = '.';
}
this.paths = { '*': '*.js' };
}

Expand Down Expand Up @@ -255,7 +261,7 @@

// <script type="module"> support
// allow a data-init function callback once loaded
if (isBrowser) {
if (isBrowser && typeof document.getElementsByTagName != 'undefined') {
var curScript = document.getElementsByTagName('script');
curScript = curScript[curScript.length - 1];

Expand Down