makeRootedResolver: preserve trailing slash for directory-like lookup
MODIFY
src/resolve.js
src/resolve.js
@@ -31,9 +31,11 @@ export const makeRootedResolver = root => {
// Find the subpath elements.
const appendEls = [];
+ let trailingDots = false;
for (let i = 0; i < specEls.length; i += 1) {
if (specEls[i] === '.') {
// Do nothing.
+ trailingDots = true;
} else if (specEls[i] === '..') {
// Navigate up in the appended elements, if possible.
if (appendEls.length === 0) {
@@ -42,12 +44,15 @@ export const makeRootedResolver = root => {
);
}
appendEls.pop();
+ trailingDots = true;
} else {
// Navigate down according to the specifier.
appendEls.push(specEls[i]);
+ trailingDots = false;
}
}
- return rootSlash + appendEls.join('/');
+ const trailingSlash = trailingDots ? '/' : '';
+ return rootSlash + appendEls.join('/') + trailingSlash;
};
};

MODIFY
test/test-resolve.js
test/test-resolve.js
@@ -21,6 +21,31 @@ test('rooted resolve', async t => {
TypeError,
`cannot skip root`,
);
+ t.equal(
+ resolve('.', referrer),
+ 'file:///some/where/over/rainbow/',
+ 'dot keeps slash',
+ );
+ t.equal(
+ resolve('./foo/', referrer),
+ 'file:///some/where/over/rainbow/foo/',
+ 'explicit directory keeps slash',
+ );
+ t.equal(
+ resolve('./foo/.', referrer),
+ 'file:///some/where/over/rainbow/foo/',
+ 'trailing dot keeps slash',
+ );
+ t.equal(
+ resolve('..', `${referrer}/bar`),
+ 'file:///some/where/over/rainbow/',
+ 'two dots keeps slash',
+ );
+ t.equal(
+ resolve('./foo/..', referrer),
+ 'file:///some/where/over/rainbow/',
+ 'trailing two dots keep slash',
+ );
} catch (e) {
t.isNot(e, e, 'unexpected exception');
} finally {

makeRootedResolver: preserve trailing slash for directory-like lookup