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
64 changes: 61 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,13 @@ module.exports = postcss.plugin(
: from

var cb = getDeclProcessor(result, from, to, callback, options, isCustom)
var promises = []

styles.walkDecls(cb)
styles.walkDecls(function() {
promises.push(cb.apply(null, arguments))
})

return Promise.all(promises)
}
}
)
Expand Down Expand Up @@ -131,26 +136,79 @@ function getDeclProcessor(result, from, to, cb, options, isCustom) {
? path.dirname(decl.source.input.file)
: process.cwd()

var warnBadUrl = function(newUrl, oldUrl) {
result.warn("Bad url result: '"
+ newUrl
+ "'. Fallback to old url: "
+ oldUrl, { node: decl })
}

var newUrl

if (isCustom || !isUrlShouldBeIgnored(oldUrl)) {
newUrl = cb(result, from, dirname, oldUrl, to, options, decl)
}
else {
newUrl = oldUrl
}

if (!newUrl) {
warnBadUrl(newUrl, oldUrl)

newUrl = oldUrl
}

if (newUrl && newUrl.then) {
return Promise.resolve(newUrl)
.then(function(url) {
return url || Promise.reject(url)
})
.catch(function(err) {
warnBadUrl(err, oldUrl)

return newUrl || oldUrl
return Promise.resolve(oldUrl)
})
}
else {
return newUrl
}
}

return function(decl) {
var id = 0 // for async replacements
var promises = []

UrlsPatterns.some(function(pattern) {
if (pattern.test(decl.value)) {
decl.value = decl.value
.replace(pattern, function(_, beforeUrl, oldUrl, afterUrl) {
return beforeUrl + valueCallback(decl, oldUrl) + afterUrl
var newUrl = valueCallback(decl, oldUrl)
var buildUrl = function(newUrl) {
return beforeUrl + newUrl + afterUrl
}

if (newUrl.then) {
var marker = "::id" + id++

promises.push(newUrl.then(function(newUrl) {
decl.value = decl.value.replace(
marker,
buildUrl(newUrl)
)
}))

return marker
}
else {
return buildUrl(newUrl)
}
})

return true
}
})

return Promise.all(promises)
}
}

Expand Down
Loading