Skip to content

Commit ca59ea8

Browse files
committed
Merge pull request #6 from mankdev/url-with-postfix
Handling urls with postfixes
2 parents 454be5c + b8e5668 commit ca59ea8

File tree

6 files changed

+56
-4
lines changed

6 files changed

+56
-4
lines changed

index.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var fs = require('fs');
22
var path = require('path');
3-
var url = require('url');
3+
var parseURL = require('url').parse;
44
var reduceFunctionCall = require('reduce-function-call');
55
var mkdirp = require('mkdirp');
66
var postcss = require('postcss');
@@ -94,13 +94,35 @@ function getAsset(filePath) {
9494

9595
}
9696

97+
function getPostfix(url) {
98+
var parsedURL = parseURL(url);
99+
var postfix = '';
100+
101+
if (parsedURL.search) {
102+
postfix += parsedURL.search;
103+
}
104+
105+
if (parsedURL.hash) {
106+
postfix += parsedURL.hash;
107+
}
108+
109+
return postfix;
110+
}
111+
112+
function getClearUrl(url) {
113+
return parseURL(url).pathname;
114+
}
115+
97116
function processUrlRebase(dirname, url, to, options) {
98117

99118
var relativeAssetsPath = '';
100119
var absoluteAssetsPath = '.';
101120

102-
var filePath = path.resolve(dirname, url);
103-
var fileName = path.basename(url);
121+
var postfix = getPostfix(url);
122+
var clearUrl = getClearUrl(url);
123+
124+
var filePath = path.resolve(dirname, clearUrl);
125+
var fileName = path.basename(clearUrl);
104126

105127
var assetContents = getAsset(filePath);
106128

@@ -130,5 +152,9 @@ function processUrlRebase(dirname, url, to, options) {
130152
}
131153
copyAsset(absoluteAssetsPath, assetContents);
132154

155+
if (postfix) {
156+
relativeAssetsPath += postfix;
157+
}
158+
133159
return composeUrl(relativeAssetsPath);
134160
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"chalk": "^1.1.0",
2828
"mkdirp": "^0.5.0",
2929
"postcss": "^4.1.0",
30-
"reduce-function-call": "^1.0.1"
30+
"reduce-function-call": "^1.0.1",
31+
"url": "^0.10.3"
3132
},
3233
"devDependencies": {
3334
"rimraf": "^2.4.0",

test/expected/copy-url-postfixes.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
body {
2+
background: url(imported/another-img.jpg#iefix);
3+
background: url(imported/img.jpg?some);
4+
background: url(imported/some.jpg?#blabla);
5+
}

test/fixtures/another-assets/some.jpg

5.22 KB
Loading

test/fixtures/copy-url-postfixes.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
body {
2+
background: url(another-assets/another-img.jpg#iefix);
3+
background: url(another-assets/one-more/img.jpg?some);
4+
background: url(another-assets/some.jpg?#blabla);
5+
}

test/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,18 @@ test('duplicated images', function(t) {
106106
t.end();
107107

108108
});
109+
110+
test('urls with postfixes', function(t) {
111+
var rebaserOptions = {
112+
assetsPath: 'imported',
113+
relative: 'true'
114+
};
115+
var postcssOptions = {
116+
from: 'test/fixtures/copy-url-postfixes.css',
117+
to: 'test/result/copy-url-postfixes.css'
118+
};
119+
120+
clearResults('test/result/copy-copy-with-hashes.css', 'test/result/imported');
121+
compareFixtures(t, 'should proper process urls with postfixes', rebaserOptions, postcssOptions);
122+
t.end();
123+
});

0 commit comments

Comments
 (0)