Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix trailing slash handling #128

Merged
merged 7 commits into from
Jun 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/resolve-dependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ const { isAbsolute, resolve, sep } = require('path');
// (package.json files are emitted as they are hit)
module.exports = function resolveDependency (specifier, parent, job) {
let resolved;
if (isAbsolute(specifier) || specifier === '.' || specifier === '..' || specifier.startsWith('./') || specifier.startsWith('../'))
resolved = resolvePath(resolve(parent, '..', specifier), parent, job);
else
if (isAbsolute(specifier) || specifier === '.' || specifier === '..' || specifier.startsWith('./') || specifier.startsWith('../')) {
const trailingSlash = specifier.endsWith('/');
resolved = resolvePath(resolve(parent, '..', specifier) + (trailingSlash ? '/' : ''), parent, job);
}
else {
resolved = resolvePackage(specifier, parent, job);
}
if (resolved.startsWith('node:')) return resolved;
return job.realpath(resolved, parent);
};
Expand All @@ -18,8 +21,8 @@ function resolvePath (path, parent, job) {
}

function resolveFile (path, parent, job) {
if (path.endsWith('/')) return;
path = job.realpath(path, parent);
if (path.endsWith(sep)) return;
if (job.isFile(path)) return path;
if (job.ts && path.startsWith(job.base) && path.substr(job.base.length).indexOf(sep + 'node_modules' + sep) === -1 && job.isFile(path + '.ts')) return path + '.ts';
if (job.ts && path.startsWith(job.base) && path.substr(job.base.length).indexOf(sep + 'node_modules' + sep) === -1 && job.isFile(path + '.tsx')) return path + '.tsx';
Expand All @@ -29,6 +32,7 @@ function resolveFile (path, parent, job) {
}

function resolveDir (path, parent, job) {
if (path.endsWith('/')) path = path.slice(0, -1);
if (!job.isDir(path)) return;
const realPjsonPath = job.realpath(path + sep + 'package.json', parent);
const pjsonSource = job.readFile(realPjsonPath);
Expand Down
2 changes: 2 additions & 0 deletions test/unit/file-folder-slash/file-folder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module.exports = 'file';

2 changes: 2 additions & 0 deletions test/unit/file-folder-slash/file-folder/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module.exports = 'folder';

2 changes: 2 additions & 0 deletions test/unit/file-folder-slash/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('./file-folder/');

4 changes: 4 additions & 0 deletions test/unit/file-folder-slash/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
"test/unit/file-folder-slash/file-folder/index.js",
"test/unit/file-folder-slash/input.js"
]