Skip to content

Commit

Permalink
Use statting instead of read in resolver (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford authored and styfle committed Jul 16, 2019
1 parent 996c859 commit 69ae77b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
14 changes: 14 additions & 0 deletions src/node-file-trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ class Job {
}
}

isFile (path) {
const stats = this.stat(path);
if (stats)
return stats.isFile();
return false;
}

isDir (path) {
const stats = this.stat(path);
if (stats)
return stats.isDirectory();
return false;
}

stat (path) {
const cached = this.statCache.get(path);
if (cached) return cached;
Expand Down
20 changes: 10 additions & 10 deletions src/resolve-dependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ function resolvePath (path, parent, job) {

function resolveFile (path, job) {
if (path.endsWith('/')) return;
if (job.readFile(path) !== null) return path;
if (job.ts && path.startsWith(job.base) && path.substr(job.base.length).indexOf(sep + 'node_modules' + sep) === -1 && job.readFile(path + '.ts') !== null) return path + '.ts';
if (job.ts && path.startsWith(job.base) && path.substr(job.base.length).indexOf(sep + 'node_modules' + sep) === -1 && job.readFile(path + '.tsx') !== null) return path + '.tsx';
if (job.readFile(path + '.js') !== null) return path + '.js';
if (job.readFile(path + '.json') !== null) return path + '.json';
if (job.readFile(path + '.node') !== null) return path + '.node';
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';
if (job.isFile(path + '.js')) return path + '.js';
if (job.isFile(path + '.json')) return path + '.json';
if (job.isFile(path + '.node')) return path + '.node';
}

function resolveDir (path, parent, job) {
const stat = job.stat(path);
if (!stat || !stat.isDirectory()) return;
const pjsonSource = job.readFile(path + '/package.json');
if (!job.isDir(path)) return;
const realPjsonPath = realpath(path + '/package.json', parent, job);
const pjsonSource = job.readFile(realPjsonPath);
if (pjsonSource) {
try {
var pjson = JSON.parse(pjsonSource);
Expand All @@ -62,7 +62,7 @@ function resolveDir (path, parent, job) {
if (pjson && typeof pjson.main === 'string') {
const resolved = resolveFile(resolve(path, pjson.main), job) || resolveFile(resolve(path, pjson.main, 'index'), job);
if (resolved) {
job.emitFile(realpath(path + '/package.json', parent, job), 'resolve', parent);
job.emitFile(realPjsonPath, 'resolve', parent);
return resolved;
}
}
Expand Down

0 comments on commit 69ae77b

Please sign in to comment.