Skip to content
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
39 changes: 24 additions & 15 deletions addStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var stylesInDom = {},
singletonCounter = 0,
styleElementsInsertedAtTop = [];

module.exports = function(list, options) {
module.exports = function(object, options) {
if(typeof DEBUG !== "undefined" && DEBUG) {
if(typeof document !== "object") throw new Error("The style-loader cannot be used in a non-browser environment");
}
Expand All @@ -33,19 +33,19 @@ module.exports = function(list, options) {
// By default, add <style> tags to the bottom of <head>.
if (typeof options.insertAt === "undefined") options.insertAt = "bottom";

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

return function update(newList) {
return function update(newObject) {
var mayRemove = [];
for(var i = 0; i < styles.length; i++) {
var item = styles[i];
var domStyle = stylesInDom[item.id];
domStyle.refs--;
mayRemove.push(domStyle);
}
if(newList) {
var newStyles = listToStyles(newList);
if(newObject) {
var newStyles = objectToStyles(newObject);
addStylesToDom(newStyles, options);
}
for(var i = 0; i < mayRemove.length; i++) {
Expand Down Expand Up @@ -81,21 +81,30 @@ function addStylesToDom(styles, options) {
}
}

function listToStyles(list) {
function objectToStyles(object) {
var styles = [];
var newStyles = {};
for(var i = 0; i < list.length; i++) {
var item = list[i];
var id = item[0];
var css = item[1];
var media = item[2];
var sourceMap = item[3];
var part = {css: css, media: media, sourceMap: sourceMap};
if(!newStyles[id])

styles.push(
newStyles[object.id] = { id: object.id, parts: [
{css: object.content, media: '', sourceMap: object.sourceMap}
] }
);

for (var i = 0; i < object.imports.length; i++) {
var childObject = object.imports[i][0];
var mediaQuery = object.imports[i][1];
var id = childObject.id;
var css = childObject.content;
var sourceMap = childObject.sourceMap;
var part = {css: css, media: mediaQuery, sourceMap: sourceMap};
if(!newStyles[id]) {
styles.push(newStyles[id] = {id: id, parts: [part]});
else
} else {
newStyles[id].parts.push(part);
}
}

return styles;
}

Expand Down
23 changes: 18 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,28 @@ module.exports = function() {};
module.exports.pitch = function(remainingRequest) {
if(this.cacheable) this.cacheable();
var query = loaderUtils.parseQuery(this.query);

var importJs;
var loaders = remainingRequest.split('!');
if (loaders[0].indexOf('/css-loader/index.js') != -1) {
importJs = "import { $css } from " + loaderUtils.stringifyRequest(this, "!!" + remainingRequest) + ";\n" +
"export * from " + loaderUtils.stringifyRequest(this, "!!" + remainingRequest) + ";\n" +
"export { default } from " + loaderUtils.stringifyRequest(this, "!!" + remainingRequest) + ";\n";
} else {
importJs = "var $css = {\n" +
"\t id: module.id,\n" +
"\t content: require(" + loaderUtils.stringifyRequest(this, "!!" + remainingRequest) + "),\n" +
"\t mediaQueries: []\n" +
"};\n";
}
return [
"// style-loader: Adds some css to the DOM by adding a <style> tag",
"",
// TODO: Update HMR
"var content = { locals: true };",
"// load the styles",
"var content = require(" + loaderUtils.stringifyRequest(this, "!!" + remainingRequest) + ");",
"if(typeof content === 'string') content = [[module.id, content, '']];",
importJs,
"// add the styles to the DOM",
"var update = require(" + loaderUtils.stringifyRequest(this, "!" + path.join(__dirname, "addStyles.js")) + ")(content, " + JSON.stringify(query) + ");",
"if(content.locals) module.exports = content.locals;",
"var update = require(" + loaderUtils.stringifyRequest(this, "!" + path.join(__dirname, "addStyles.js")) + ")($css, " + JSON.stringify(query) + ");",
"// Hot Module Replacement",
"if(module.hot) {",
" // When the styles change, update the <style> tags",
Expand Down