-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: when the publicUrl is url not a path of os (#447)
* fix: when the publicUrl is url not a path of os * remove extra params * fix * fix * add test and change doc * Simplify urlJoin Just use URL.parse always, and use posix path joining.
- Loading branch information
1 parent
1a853be
commit a852dd2
Showing
2 changed files
with
75 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
const url = require('url'); | ||
const URL = require('url'); | ||
const path = require('path'); | ||
|
||
/** | ||
* Joins a path onto a URL, and normalizes Windows paths | ||
* e.g. from \path\to\res.js to /path/to/res.js. | ||
*/ | ||
module.exports = function(publicURL, assetPath) { | ||
// Use url.resolve to normalize path for windows | ||
// from \path\to\res.js to /path/to/res.js | ||
return url.resolve(path.join(publicURL, assetPath), ''); | ||
const url = URL.parse(publicURL); | ||
url.pathname = path.posix.join(url.pathname, URL.parse(assetPath).pathname); | ||
return URL.format(url); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const assert = require('assert'); | ||
const urlJoin = require('../src/utils/urlJoin'); | ||
|
||
describe('Url Join', () => { | ||
it('should join a filename with a URL', () => { | ||
assert.equal( | ||
urlJoin('https://parceljs.org', 'a.js'), | ||
'https://parceljs.org/a.js' | ||
); | ||
}); | ||
|
||
it('should join a path with a URL', () => { | ||
assert.equal( | ||
urlJoin('https://parceljs.org', 'bar/a.js'), | ||
'https://parceljs.org/bar/a.js' | ||
); | ||
}); | ||
|
||
it('should join a paths together', () => { | ||
assert.equal( | ||
urlJoin('https://parceljs.org/foo/', 'bar/a.js'), | ||
'https://parceljs.org/foo/bar/a.js' | ||
); | ||
}); | ||
|
||
it('should join an absolute path with a URL', () => { | ||
assert.equal( | ||
urlJoin('https://parceljs.org/foo/', '/bar/a.js'), | ||
'https://parceljs.org/foo/bar/a.js' | ||
); | ||
}); | ||
|
||
it('should join a URL with a querystring', () => { | ||
assert.equal( | ||
urlJoin('https://parceljs.org/foo?a=123', '/bar/a.js'), | ||
'https://parceljs.org/foo/bar/a.js?a=123' | ||
); | ||
|
||
assert.equal( | ||
urlJoin('https://parceljs.org/foo?a=123&b=456', '/bar/a.js'), | ||
'https://parceljs.org/foo/bar/a.js?a=123&b=456' | ||
); | ||
}); | ||
|
||
it('should join a URL with a hash', () => { | ||
assert.equal( | ||
urlJoin('https://parceljs.org/foo#hello', '/bar/a.js'), | ||
'https://parceljs.org/foo/bar/a.js#hello' | ||
); | ||
|
||
assert.equal( | ||
urlJoin('https://parceljs.org/foo?a=123&b=456#hello', '/bar/a.js'), | ||
'https://parceljs.org/foo/bar/a.js?a=123&b=456#hello' | ||
); | ||
}); | ||
|
||
it('should join two paths together', () => { | ||
assert.equal( | ||
urlJoin('/Users/people/projects/parcel', '/bar/foo.js'), | ||
'/Users/people/projects/parcel/bar/foo.js' | ||
); | ||
}); | ||
|
||
it('should support windows paths', () => { | ||
assert.equal(urlJoin('dist\\foo', '\\bar\\foo.js'), 'dist/foo/bar/foo.js'); | ||
}); | ||
}); |