Skip to content

Commit

Permalink
Merge pull request #69 from elsbree/master
Browse files Browse the repository at this point in the history
Add support for 'insertAt' parameter
  • Loading branch information
sokra committed Oct 15, 2015
2 parents 6d76315 + a081f99 commit 3944f22
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ Note: Behavior is undefined when `unuse`/`unref` is called more often than `use`

### Options

#### `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')`.

#### `singleton`

If defined, the style-loader will re-use a single `<style>` element, instead of adding/removing individual elements for each required module. **Note:** this option is on by default in IE9, which has strict limitations on the number of style tags allowed on a page. You can enable or disable it with the singleton query parameter (`?singleton` or `?-singleton`).
Expand Down
17 changes: 13 additions & 4 deletions addStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ module.exports = function(list, options) {
// tags it will allow on a page
if (typeof options.singleton === "undefined") options.singleton = isOldIE();

// By default, add <style> tags to the bottom of <head>.
if (typeof options.insertAt === "undefined") options.insertAt = "bottom";

var styles = listToStyles(list);
addStylesToDom(styles, options);

Expand Down Expand Up @@ -95,11 +98,17 @@ function listToStyles(list) {
return styles;
}

function createStyleElement() {
function createStyleElement(options) {
var styleElement = document.createElement("style");
var head = getHeadElement();
styleElement.type = "text/css";
head.appendChild(styleElement);
if (options.insertAt === "top") {
head.insertBefore(styleElement, head.firstChild);
} else if (options.insertAt === "bottom") {
head.appendChild(styleElement);
} else {
throw new Error("Invalid value for parameter 'insertAt'. Must be 'top' or 'bottom'.");
}
return styleElement;
}

Expand All @@ -116,7 +125,7 @@ function addStyle(obj, options) {

if (options.singleton) {
var styleIndex = singletonCounter++;
styleElement = singletonElement || (singletonElement = createStyleElement());
styleElement = singletonElement || (singletonElement = createStyleElement(options));
update = applyToSingletonTag.bind(null, styleElement, styleIndex, false);
remove = applyToSingletonTag.bind(null, styleElement, styleIndex, true);
} else if(obj.sourceMap &&
Expand All @@ -133,7 +142,7 @@ function addStyle(obj, options) {
URL.revokeObjectURL(styleElement.href);
};
} else {
styleElement = createStyleElement();
styleElement = createStyleElement(options);
update = applyToTag.bind(null, styleElement);
remove = function() {
styleElement.parentNode.removeChild(styleElement);
Expand Down

0 comments on commit 3944f22

Please sign in to comment.