css-loader
Advanced tools
Comparing version 0.1.2 to 0.2.0
59
index.js
@@ -6,17 +6,24 @@ /* | ||
var csso = require("csso"); | ||
var uriRegExp = /%CSSURL\[%(.*)%\]CSSURL%/g; | ||
module.exports = function(content) { | ||
var options = this; | ||
var isRequireUrl = !this || !this.options || !this.options.css || | ||
typeof this.options.css.requireUrl === "string"; | ||
var requireUrl = this && this.options && this.options.css && | ||
this.options.css.requireUrl || | ||
"file/auto!"; | ||
var result = []; | ||
var tree = csso.parse(content, "stylesheet"); | ||
if(options.minimize) | ||
if(this && this.minimize) | ||
tree = csso.compress(tree); | ||
tree = csso.cleanInfo(tree); | ||
var imports = extractImports(tree); | ||
if(isRequireUrl) | ||
annotateUrls(tree); | ||
imports.forEach(function(imp) { | ||
if(imp.media.length > 0) { | ||
result.push(JSON.stringify("@media " + imp.media.join("") + "{")); | ||
result.push(JSON.stringify("@media(" + imp.media.join("") + "){")); | ||
} | ||
result.push("require(" + JSON.stringify(__filename) + " + \"!\" + " + JSON.stringify(urlToRequire(imp.url)) + ")"); | ||
result.push("require(" + JSON.stringify(__filename + "!" + urlToRequire(imp.url)) + ")"); | ||
if(imp.media.length > 0) { | ||
@@ -26,4 +33,13 @@ result.push(JSON.stringify("}")); | ||
}); | ||
result.push(JSON.stringify(csso.translate(tree))); | ||
var css = JSON.stringify(csso.translate(tree)); | ||
if(isRequireUrl) { | ||
css = css.replace(uriRegExp, function(match) { | ||
match = uriRegExp.exec(match); | ||
var url = JSON.parse("\"" + match[1] + "\""); | ||
return "\"+require(" + JSON.stringify(requireUrl + urlToRequire(url)) + ")+\""; | ||
}); | ||
} | ||
result.push(css); | ||
return "module.exports =\n\t" + result.join(" +\n\t") + ";"; | ||
@@ -76,2 +92,29 @@ } | ||
return results; | ||
} | ||
function annotateUrls(tree) { | ||
function iterateChildren() { | ||
for(var i = 1; i < tree.length; i++) { | ||
annotateUrls(tree[i]); | ||
} | ||
} | ||
switch(tree[0]) { | ||
case "stylesheet": return iterateChildren(); | ||
case "ruleset": return iterateChildren(); | ||
case "block": return iterateChildren(); | ||
case "declaration": return iterateChildren(); | ||
case "value": return iterateChildren(); | ||
case "uri": | ||
for(var i = 1; i < tree.length; i++) { | ||
var item = tree[i]; | ||
switch(item[0]) { | ||
case "ident": | ||
case "raw": | ||
item[1] = "%CSSURL[%" + item[1] + "%]CSSURL%"; | ||
return; | ||
case "string": | ||
item[1] = "%CSSURL[%" + item[1].substring(1, item[1].length-1) + "%]CSSURL%"; | ||
return; | ||
} | ||
} | ||
} | ||
} |
{ | ||
"name": "css-loader", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"author": "Tobias Koppers @sokra", | ||
@@ -9,2 +9,8 @@ "description": "css loader module for webpack", | ||
}, | ||
"devDependencies": { | ||
"vows": "0.6.2" | ||
}, | ||
"scripts": { | ||
"test": "node node_modules/vows/bin/vows" | ||
}, | ||
"licenses": [ | ||
@@ -11,0 +17,0 @@ { |
@@ -7,5 +7,16 @@ # css loader for webpack | ||
var css = require("css!./file.css"); | ||
// => returns css code from file.css, resolves imports | ||
// => returns css code from file.css, resolves imports and url(...) | ||
``` | ||
`@import` will be required with this css loader. | ||
`url(...)` will be required with the loader specified in the options. | ||
If `options.css.requireUrl` is a string it will be prefixed to the required url. | ||
If it isn't a string `url(...)` will not be replaced. | ||
`options.css.requireUrl` defaults to `"file/auto!"`. | ||
A alternative to the file-loader is the | ||
[url-loader](https://github.com/sokra/webpack-url-loader) which can use Data Urls. | ||
The use it specify `"url/auto!"`. | ||
Don't forget to polyfill `require` if you want to use it in node. | ||
@@ -12,0 +23,0 @@ See `webpack` documentation. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4454
111
26
1