funcunit
Version:
<!-- @hide title
83 lines (68 loc) • 2.41 kB
JavaScript
var loader = require("@loader");
if(isProduction()) {
exports.fetch = function(load) {
// return a thenable for fetching (as per specification)
// alternatively return new Promise(function(resolve, reject) { ... })
var cssFile = load.address;
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = cssFile;
document.head.appendChild(link);
return "";
};
} else {
exports.instantiate = function(load) {
var loader = this;
load.metadata.deps = [];
load.metadata.execute = function(){
var source = load.source+"/*# sourceURL="+load.address+" */";
source = source.replace(/url\(['"]?([^'"\)]*)['"]?\)/g, function(whole, part) {
return "url(" + steal.joinURIs( load.address, part) + ")";
});
// Replace @import's that don't start with a "u" or "U" and do start
// with a single or double quote with a path wrapped in "url()"
// relative to the page
source = source.replace(/@import [^uU]['"]?([^'"\)]*)['"]?/g, function(whole, part) {
return "@import url(" + steal.joinURIs( load.address, part) + ")";
});
if(load.source && typeof document !== "undefined") {
var doc = document.head ? document : document.getElementsByTagName ?
document : document.documentElement;
var head = doc.head || doc.getElementsByTagName('head')[0],
style = document.createElement('style');
if(!head) {
var docEl = doc.documentElement || doc;
head = document.createElement("head");
docEl.insertBefore(head, docEl.firstChild);
}
// make source load relative to the current page
style.type = 'text/css';
if (style.styleSheet){
style.styleSheet.cssText = source;
} else {
style.appendChild(document.createTextNode(source));
}
head.appendChild(style);
if(loader.has("live-reload")) {
var cssReload = loader["import"]("live-reload", { name: "$css" });
Promise.resolve(cssReload).then(function(reload){
loader["import"](load.name).then(function(){
reload.once(load.name, function(){
head.removeChild(style);
});
});
});
}
}
return System.newModule({source: source});
};
load.metadata.format = "css";
};
}
function isProduction(){
return (loader.isEnv && loader.isEnv("production")) ||
loader.env === "production";
}
exports.locateScheme = true;
exports.buildType = "css";
exports.includeInBuild = true;