Skip to content

Commit 25d73d0

Browse files
committed
v.6.0.3
- fixed error with hash url
1 parent 6355a0d commit 25d73d0

File tree

7 files changed

+60
-12
lines changed

7 files changed

+60
-12
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 6.0.3 - 2017-04-04
2+
Fixed: hash url error
3+
([#89])(https://github.com/postcss/postcss-url/issues/89)
4+
15
# 6.0.2 - 2017-04-04
26
Fixed: match options before analyzing
37
([pull-88](https://github.com/postcss/postcss-url/pull/88))

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-url",
3-
"version": "6.0.2",
3+
"version": "6.0.3",
44
"description": "PostCSS plugin to rebase or inline on url().",
55
"keywords": [
66
"css",

src/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@ const getPattern = (decl) =>
6161
* @returns {String|undefined}
6262
*/
6363
const replaceUrl = (url, dir, options, result, decl) => {
64-
const asset = prepareAsset(url, dir, options.basePath);
64+
const asset = prepareAsset(url, dir, decl);
6565
const relativeToRoot = path.relative(process.cwd(), asset.absolutePath);
6666

6767
const matchedOptions = matchOptions(relativeToRoot, options);
68+
6869
if (!matchedOptions) return;
6970

7071
const isFunction = typeof matchedOptions.url === 'function';
72+
7173
if (!isFunction && isUrlShouldBeIgnored(url, matchedOptions)) return;
7274

7375
const mode = isFunction ? 'custom' : (matchedOptions.url || 'rebase');
@@ -114,7 +116,7 @@ module.exports = postcss.plugin('postcss-url', (options) => {
114116

115117
return declProcessor(options, result, dir, decl);
116118
});
117-
}
119+
};
118120
});
119121

120122
/**

src/lib/paths.js

+29-8
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,27 @@ const normalize = (assetUrl) => {
1616
};
1717

1818
/**
19-
* Check if url is absolute, hash or data-uri
20-
*
2119
* @param {String} assetUrl
22-
* @param {PostcssUrl~Options} options
2320
* @returns {Boolean}
2421
*/
25-
const isUrlShouldBeIgnored = (assetUrl, options) => {
22+
const isUrlWithoutPathname = (assetUrl) => {
2623
return assetUrl[0] === '#'
2724
|| assetUrl.indexOf('%23') === 0
28-
|| (assetUrl[0] === '/' && !options.basePath)
2925
|| assetUrl.indexOf('data:') === 0
3026
|| /^[a-z]+:\/\//.test(assetUrl);
3127
};
3228

29+
/**
30+
* Check if url is absolute, hash or data-uri
31+
*
32+
* @param {String} assetUrl
33+
* @param {PostcssUrl~Options} options
34+
* @returns {Boolean}
35+
*/
36+
const isUrlShouldBeIgnored = (assetUrl, options) => {
37+
return isUrlWithoutPathname(assetUrl) || (assetUrl[0] === '/' && !options.basePath);
38+
};
39+
3340
/**
3441
* @param {String} baseDir - absolute target path
3542
* @param {String} assetsPath - extend asset path, can be absolute path
@@ -48,14 +55,23 @@ const getAssetsPath = (baseDir, assetsPath, relative) =>
4855
const getTargetDir = (dir) =>
4956
dir.from !== dir.to ? dir.to : process.cwd();
5057

58+
/**
59+
* Stylesheet file path from decl
60+
*
61+
* @param {Decl} decl
62+
* @returns {String}
63+
*/
64+
const getPathDeclFile = (decl) =>
65+
decl.source && decl.source.input && decl.source.input.file;
66+
5167
/**
5268
* Stylesheet file dir from decl
5369
*
5470
* @param {Decl} decl
5571
* @returns {String}
5672
*/
5773
const getDirDeclFile = (decl) => {
58-
const filename = decl.source && decl.source.input && decl.source.input.file;
74+
const filename = getPathDeclFile(decl);
5975

6076
return filename ? path.dirname(filename) : process.cwd();
6177
};
@@ -85,11 +101,15 @@ const getPathByBasePath = (basePath, dirFrom, relPath) => {
85101
*
86102
* @param {String} assetUrl
87103
* @param {PostcssUrl~Dir} dir
104+
* @param {Decl} decl
88105
* @returns {PostcssUrl~Asset}
89106
*/
90-
const prepareAsset = (assetUrl, dir) => {
107+
const prepareAsset = (assetUrl, dir, decl) => {
91108
const parsedUrl = url.parse(assetUrl);
92-
const absolutePath = path.join(dir.file, parsedUrl.pathname);
109+
const pathname = !isUrlWithoutPathname(assetUrl) ? parsedUrl.pathname : null;
110+
const absolutePath = pathname
111+
? path.join(dir.file, pathname)
112+
: getPathDeclFile(decl);
93113

94114
return {
95115
url: assetUrl,
@@ -106,6 +126,7 @@ module.exports = {
106126
prepareAsset,
107127
getAssetsPath,
108128
getDirDeclFile,
129+
getPathDeclFile,
109130
getTargetDir,
110131
getPathByBasePath,
111132
isUrlShouldBeIgnored

test/fixtures/rebase-to-from.css

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
body {
22
background: url("./one"), url("./two");
3+
filter: url("#one");
34
}
+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
body {
22
background: url("one"), url("two");
3-
}
3+
filter: url("#one");
4+
}

test/lib/paths.js

+19
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,23 @@ describe('paths', () => {
133133
hash: '#23'
134134
});
135135
});
136+
137+
it('should prepare custom assets', () => {
138+
const dirs = {
139+
from: '/project/css',
140+
file: '/project/css/imported'
141+
};
142+
const decl = {
143+
source: { input: { file: '/project/styles/style.css' } }
144+
};
145+
146+
const checkCustomAsset = (assetUrl) => {
147+
const asset = paths.prepareAsset(assetUrl, dirs, decl);
148+
149+
assert.equal(asset.absolutePath, path.resolve('/project/styles/style.css'));
150+
assert.equal(asset.relativePath, '../styles/style.css');
151+
};
152+
153+
['#hash', '%23ecodedhash', 'data:'].forEach(checkCustomAsset);
154+
});
136155
});

0 commit comments

Comments
 (0)