Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Resolve assets path start with '/' need to relative current project path. Add static css url resolves. #65

Closed
wants to merge 1 commit into from
Closed
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
30 changes: 21 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var Assets = require('assets');
var dirname = require('path').dirname;
var p = require('path');
var dirname = p.dirname;
var functions = require('postcss-functions');
var postcss = require('postcss');
var quote = require('./quote');
Expand All @@ -23,6 +24,15 @@ function formatHeight(measurements) {
return util.format('%dpx', measurements.height);
}

// Normalize css assets path, resolve to project base if start with '/'
function normalizePath(path, options) {
var normalizedPath = unquote(unescapeCss(path));
if (normalizedPath.charAt(0) === '/') {
normalizedPath = p.resolve(options.basePath || '.', normalizedPath.slice(1));
}
return normalizedPath;
}

function plugin(options) {
var params = options || {};
var resolver;
Expand All @@ -46,6 +56,10 @@ function plugin(options) {
});
}

function resolveUrl(path) {
return resolver.url(normalizePath(path, resolver.options)).then(formatUrl);
}

return postcss()
.use(function appendInputDir(css) {
var inputDir;
Expand All @@ -67,24 +81,22 @@ function plugin(options) {
})
.use(functions({
functions: {
resolve: function resolve(path) {
var normalizedPath = unquote(unescapeCss(path));
return resolver.url(normalizedPath).then(formatUrl);
},
url: resolveUrl,
resolve: resolveUrl,
inline: function inline(path) {
var normalizedPath = unquote(unescapeCss(path));
var normalizedPath = normalizePath(path, resolver.options);
return resolver.data(normalizedPath).then(formatUrl);
},
size: function size(path, density) {
var normalizedPath = unquote(unescapeCss(path));
var normalizedPath = normalizePath(path, resolver.options);
return measure(normalizedPath, density).then(formatSize);
},
width: function width(path, density) {
var normalizedPath = unquote(unescapeCss(path));
var normalizedPath = normalizePath(path, resolver.options);
return measure(normalizedPath, density).then(formatWidth);
},
height: function height(path, density) {
var normalizedPath = unquote(unescapeCss(path));
var normalizedPath = normalizePath(path, resolver.options);
return measure(normalizedPath, density).then(formatHeight);
}
}
Expand Down