Merge branch 'elsbree-master'

master
Tobias Koppers 2015-10-19 20:56:12 +02:00
commit 4678852eed
5 changed files with 38 additions and 14 deletions

View File

@ -47,7 +47,7 @@ Note: Behavior is undefined when `unuse`/`unref` is called more often than `use`
#### `insertAt`
By default, the style-loader appends `<style>` elements to the end of the `<head>` tag of the page. This will cause CSS created by the loader to take priority over CSS already present in the document head. To insert style elements at the beginning of the head, set this query parameter to 'bottom', e.g. `require('../style.css?insertAt=bottom')`.
By default, the style-loader appends `<style>` elements to the end of the `<head>` tag of the page. This will cause CSS created by the loader to take priority over CSS already present in the document head. To insert style elements at the beginning of the head, set this query parameter to 'top', e.g. `require('../style.css?insertAt=top')`.
#### `singleton`

View File

@ -17,7 +17,8 @@ var stylesInDom = {},
return document.head || document.getElementsByTagName("head")[0];
}),
singletonElement = null,
singletonCounter = 0;
singletonCounter = 0,
styleElementsInsertedAtTop = [];
module.exports = function(list, options) {
if(typeof DEBUG !== "undefined" && DEBUG) {
@ -98,25 +99,44 @@ function listToStyles(list) {
return styles;
}
function createStyleElement(options) {
var styleElement = document.createElement("style");
function insertStyleElement(options, styleElement) {
var head = getHeadElement();
styleElement.type = "text/css";
var lastStyleElementInsertedAtTop = styleElementsInsertedAtTop[styleElementsInsertedAtTop.length - 1];
if (options.insertAt === "top") {
head.insertBefore(styleElement, head.firstChild);
if(!lastStyleElementInsertedAtTop) {
head.insertBefore(styleElement, head.firstChild);
} else if(lastStyleElementInsertedAtTop.nextSibling) {
head.insertBefore(styleElement, lastStyleElementInsertedAtTop.nextSibling);
} else {
head.appendChild(styleElement);
}
styleElementsInsertedAtTop.push(styleElement);
} else if (options.insertAt === "bottom") {
head.appendChild(styleElement);
} else {
throw new Error("Invalid value for parameter 'insertAt'. Must be 'top' or 'bottom'.");
}
}
function removeStyleElement(styleElement) {
styleElement.parentNode.removeChild(styleElement);
var idx = styleElementsInsertedAtTop.indexOf(styleElement);
if(idx >= 0) {
styleElementsInsertedAtTop.splice(idx, 1);
}
}
function createStyleElement(options) {
var styleElement = document.createElement("style");
styleElement.type = "text/css";
insertStyleElement(options, styleElement);
return styleElement;
}
function createLinkElement() {
function createLinkElement(options) {
var linkElement = document.createElement("link");
var head = getHeadElement();
linkElement.rel = "stylesheet";
head.appendChild(linkElement);
insertStyleElement(options, linkElement);
return linkElement;
}
@ -134,10 +154,10 @@ function addStyle(obj, options) {
typeof URL.revokeObjectURL === "function" &&
typeof Blob === "function" &&
typeof btoa === "function") {
styleElement = createLinkElement();
styleElement = createLinkElement(options);
update = updateLink.bind(null, styleElement);
remove = function() {
styleElement.parentNode.removeChild(styleElement);
removeStyleElement(styleElement);
if(styleElement.href)
URL.revokeObjectURL(styleElement.href);
};
@ -145,7 +165,7 @@ function addStyle(obj, options) {
styleElement = createStyleElement(options);
update = applyToTag.bind(null, styleElement);
remove = function() {
styleElement.parentNode.removeChild(styleElement);
removeStyleElement(styleElement);
};
}

3
fixtures/c.css Normal file
View File

@ -0,0 +1,3 @@
body {
border: 3px solid blue;
}

View File

@ -1,2 +1,3 @@
require("./b.css");
require("./a.css");
require("./a.css");
require("./c.css");

View File

@ -1,7 +1,7 @@
module.exports = {
module: {
loaders: [
{ test: /\.css$/, loader: "style!css?sourceMap" }
{ test: /\.css$/, loader: "style?insertAt=top!css?sourceMap" }
]
}
}