Skip to content

Commit

Permalink
Merge pull request #40 from Swatinem/inline-fallback-query
Browse files Browse the repository at this point in the history
Inline fallback query
  • Loading branch information
MoOx committed Aug 6, 2015
2 parents 34bb407 + f7e70a1 commit 6743146
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
16 changes: 9 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,12 @@ function processInline(result, from, dirname, urlMeta, to, options, decl) {
}

if (basePath) {
fullFilePath = path.join(basePath, urlMeta.value)
fullFilePath = path.join(basePath, link.pathname)
}
else {
fullFilePath = dirname !== from
? dirname + path.sep + urlMeta.value
: urlMeta.value
? dirname + path.sep + link.pathname
: link.pathname
}

var file = path.resolve(from, fullFilePath)
Expand All @@ -241,13 +241,14 @@ function processInline(result, from, dirname, urlMeta, to, options, decl) {
return createUrl(urlMeta)
}

var mimeType = mime.lookup(file)
var stats = fs.statSync(file)

if (stats.size >= maxSize) {
return processFallback()
}

var mimeType = mime.lookup(file)

if (!mimeType) {
result.warn("Unable to find asset mime-type for " + file, {node: decl})
return createUrl(urlMeta)
Expand Down Expand Up @@ -282,7 +283,7 @@ function processInline(result, from, dirname, urlMeta, to, options, decl) {
*/
function processCopy(result, from, dirname, urlMeta, to, options, decl) {
if (from === to) {
result.warn("Option `to` of postscss is required, ignoring", {node: decl})
result.warn("Option `to` of postcss is required, ignoring", {node: decl})
return createUrl(urlMeta)
}
var relativeAssetsPath = (options && options.assetsPath)
Expand All @@ -295,7 +296,8 @@ function processCopy(result, from, dirname, urlMeta, to, options, decl) {

// remove hash or parameters in the url.
// e.g., url('glyphicons-halflings-regular.eot?#iefix')
var filePath = url.parse(filePathUrl, true).pathname
var fileLink = url.parse(filePathUrl, true)
var filePath = fileLink.pathname
var name = path.basename(filePath)
var useHash = options.useHash || false

Expand All @@ -319,8 +321,8 @@ function processCopy(result, from, dirname, urlMeta, to, options, decl) {
.update(contents)
.digest("hex")
.substr(0, 16)
nameUrl = name + path.extname(filePathUrl)
name += path.extname(filePath)
nameUrl = name + (fileLink.search || "") + (fileLink.hash || "")
}
else {
if (!pathIsAbsolute.posix(from)) {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/copy-hash-parameters.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
body {
clip-path: url("imported/pixel.png?#iefix")
clip-path: url("imported/pixel.png?v=1.1#iefix")
}
4 changes: 4 additions & 0 deletions test/fixtures/copy-parameters.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ body {
clip-path: url("imported/pixel.png?#iefix")
}

header {
clip-path: url("imported/pixel.png?foo=bar")
}

div {
clip-path: url("pixel.gif#el")
}
Expand Down
10 changes: 7 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,16 @@ function testCopy(t, opts, postcssOpts) {
new RegExp("\"" + assetsPath + "imported\/pixel\.png\""),
copyPixelGif:
new RegExp("\"" + assetsPath + "pixel\\.gif\""),
copyParamsPixelPng:
copyParamsPixelPngHash:
new RegExp("\"" + assetsPath + "imported\/pixel\\.png\\?\#iefix\""),
copyParamsPixelPngParam:
new RegExp("\"" + assetsPath + "imported\/pixel\\.png\\?foo=bar\""),
copyParamsPixelGif:
new RegExp("\"" + assetsPath + "pixel\\.gif\\#el\""),
copyHashPixel:
new RegExp("\"" + assetsPath + "[a-z0-9]{16}\\.png\""),
copyHashParamsPixel:
new RegExp("\"" + assetsPath + "[a-z0-9]{16}\\.png\\?\\#iefix\""),
new RegExp("\"" + assetsPath + "[a-z0-9]{16}\\.png\\?v=1\\.1\\#iefix\""),
}

var css = postcss()
Expand All @@ -199,7 +201,8 @@ function testCopy(t, opts, postcssOpts) {

t.ok(
(
css.match(patterns.copyParamsPixelPng) &&
css.match(patterns.copyParamsPixelPngHash) &&
css.match(patterns.copyParamsPixelPngParam) &&
css.match(patterns.copyParamsPixelGif)
),
"should copy asset from the source (`from`) to the assets destination " +
Expand Down Expand Up @@ -270,6 +273,7 @@ test("copy-when-inline-fallback", function(t) {
url: "inline",
maxSize: 0,
fallback: "copy",
assetsPath: "assets",
}

compareFixtures(
Expand Down

0 comments on commit 6743146

Please sign in to comment.