Skip to content

Commit

Permalink
fix: when the publicUrl is url not a path of os (#447)
Browse files Browse the repository at this point in the history
* 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
fansenze authored and devongovett committed Jan 6, 2018
1 parent 1a853be commit a852dd2
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/utils/urlJoin.js
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);
};
67 changes: 67 additions & 0 deletions test/url-join.js
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');
});
});

0 comments on commit a852dd2

Please sign in to comment.