-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from stealjs/minor
Merge minor into master for 1.3.0 release.
- Loading branch information
Showing
3 changed files
with
70 additions
and
3 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
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,67 @@ | ||
/* naive version of the css plugin for the slim loader */ | ||
module.exports = function(moduleId, config) { | ||
return new CssModule(config.paths[config.bundles[moduleId]]).injectLink(); | ||
}; | ||
|
||
function CssModule(address) { | ||
this.address = address; | ||
} | ||
|
||
// timeout in seconds | ||
CssModule.waitTimeout = 60; | ||
|
||
CssModule.prototype.linkExists = function() { | ||
var styleSheets = document.styleSheets; | ||
|
||
for (var i = 0; i < styleSheets.length; ++i) { | ||
if (this.address === styleSheets[i].href) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
CssModule.prototype.injectLink = function() { | ||
if (this._loadPromise) { | ||
return this._loadPromise; | ||
} | ||
|
||
if (this.linkExists()) { | ||
this._loadPromise = Promise.resolve(""); | ||
return this._loadPromise; | ||
} | ||
|
||
// inspired by https://github.com/filamentgroup/loadCSS | ||
var link = (this.link = document.createElement("link")); | ||
link.type = "text/css"; | ||
link.rel = "stylesheet"; | ||
link.href = this.address; | ||
|
||
// wait until the css file is loaded | ||
this._loadPromise = new Promise(function(resolve, reject) { | ||
var timeout = setTimeout(function() { | ||
reject("Unable to load CSS"); | ||
}, CssModule.waitTimeout * 1000); | ||
|
||
var linkEventCallback = function(event) { | ||
clearTimeout(timeout); | ||
|
||
link.removeEventListener("load", linkEventCallback); | ||
link.removeEventListener("error", linkEventCallback); | ||
|
||
if (event && event.type === "error") { | ||
reject("Unable to load CSS"); | ||
} else { | ||
resolve(""); | ||
} | ||
}; | ||
|
||
link.addEventListener("load", linkEventCallback); | ||
link.addEventListener("error", linkEventCallback); | ||
|
||
document.head.appendChild(link); | ||
}); | ||
|
||
return this._loadPromise; | ||
}; |