Skip to content

Commit d7098ce

Browse files
jdanyowdevongovett
authored andcommitted
preserve asset's search and hash (#621)
1 parent 8667d2b commit d7098ce

4 files changed

Lines changed: 56 additions & 8 deletions

File tree

src/Asset.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const URL = require('url');
12
const path = require('path');
23
const fs = require('./utils/fs');
34
const objectHash = require('./utils/objectHash');
@@ -78,15 +79,18 @@ class Asset {
7879
from = this.name;
7980
}
8081

81-
let resolved = path.resolve(path.dirname(from), url).replace(/[?#].*$/, '');
82+
const parsed = URL.parse(url);
83+
const resolved = path.resolve(path.dirname(from), parsed.pathname);
8284
this.addDependency(
8385
'./' + path.relative(path.dirname(this.name), resolved),
8486
Object.assign({dynamic: true}, opts)
8587
);
8688

87-
return this.options.parser
89+
parsed.pathname = this.options.parser
8890
.getAsset(resolved, this.package, this.options)
8991
.generateBundleName();
92+
93+
return URL.format(parsed);
9094
}
9195

9296
async getConfig(filenames) {

src/utils/urlJoin.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ const path = require('path');
77
*/
88
module.exports = function(publicURL, assetPath) {
99
const url = URL.parse(publicURL, false, true);
10-
url.pathname = path.posix.join(url.pathname, URL.parse(assetPath).pathname);
10+
const assetUrl = URL.parse(assetPath);
11+
url.pathname = path.posix.join(url.pathname, assetUrl.pathname);
12+
url.search = assetUrl.search;
13+
url.hash = assetUrl.hash;
1114
return URL.format(url);
1215
};

test/asset.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const {strictEqual} = require('assert');
2+
const Asset = require('../src/Asset');
3+
4+
describe('Asset', () => {
5+
describe('addURLDependency', () => {
6+
const bundleName = 'xyz';
7+
const options = {
8+
rootDir: '/root/dir',
9+
parser: {
10+
getAsset: () => {
11+
return {
12+
generateBundleName: () => bundleName
13+
};
14+
}
15+
}
16+
};
17+
const asset = new Asset('test', undefined, options);
18+
19+
it('should ignore urls', () => {
20+
const url = 'https://parceljs.org/assets.html';
21+
strictEqual(asset.addURLDependency(url), url);
22+
});
23+
24+
it('should ignore empty string', () => {
25+
strictEqual(asset.addURLDependency(''), '');
26+
});
27+
28+
it('should generate bundle name', () => {
29+
strictEqual(asset.addURLDependency('foo'), bundleName);
30+
});
31+
32+
it('should preserve query and hash', () => {
33+
strictEqual(asset.addURLDependency('foo#bar'), `${bundleName}#bar`);
34+
strictEqual(asset.addURLDependency('foo?bar'), `${bundleName}?bar`);
35+
strictEqual(
36+
asset.addURLDependency('foo?bar#baz'),
37+
`${bundleName}?bar#baz`
38+
);
39+
});
40+
});
41+
});

test/url-join.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,24 @@ describe('Url Join', () => {
3232

3333
it('should join a URL with a querystring', () => {
3434
assert.equal(
35-
urlJoin('https://parceljs.org/foo?a=123', '/bar/a.js'),
35+
urlJoin('https://parceljs.org/foo', '/bar/a.js?a=123'),
3636
'https://parceljs.org/foo/bar/a.js?a=123'
3737
);
3838

3939
assert.equal(
40-
urlJoin('https://parceljs.org/foo?a=123&b=456', '/bar/a.js'),
40+
urlJoin('https://parceljs.org/foo', '/bar/a.js?a=123&b=456'),
4141
'https://parceljs.org/foo/bar/a.js?a=123&b=456'
4242
);
4343
});
4444

4545
it('should join a URL with a hash', () => {
4646
assert.equal(
47-
urlJoin('https://parceljs.org/foo#hello', '/bar/a.js'),
47+
urlJoin('https://parceljs.org/foo', '/bar/a.js#hello'),
4848
'https://parceljs.org/foo/bar/a.js#hello'
4949
);
5050

5151
assert.equal(
52-
urlJoin('https://parceljs.org/foo?a=123&b=456#hello', '/bar/a.js'),
52+
urlJoin('https://parceljs.org/foo', '/bar/a.js?a=123&b=456#hello'),
5353
'https://parceljs.org/foo/bar/a.js?a=123&b=456#hello'
5454
);
5555
});
@@ -67,7 +67,7 @@ describe('Url Join', () => {
6767

6868
it('should parse double slashes as host', () => {
6969
assert.equal(
70-
urlJoin('//parceljs.org/foo?a=123&b=456#hello', 'bar/a.js'),
70+
urlJoin('//parceljs.org/foo', 'bar/a.js?a=123&b=456#hello'),
7171
'//parceljs.org/foo/bar/a.js?a=123&b=456#hello'
7272
);
7373
});

0 commit comments

Comments
 (0)