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

[now dev] Fix index-based asset lookup logic #2138

Merged
merged 1 commit into from
Apr 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/commands/dev/lib/dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,13 +681,11 @@ async function findBuildMatch(
) {
return match;
}
}
// If there's no `shouldServe()` function, then look up if there's
// a matching build asset on the `match` that has already been built.
if (findAsset(match, requestPath)) {
} else if (findAsset(match, requestPath)) {
// If there's no `shouldServe()` function, then look up if there's
// a matching build asset on the `match` that has already been built.
return match;
}

}
return null;
}
Expand All @@ -699,17 +697,17 @@ function findAsset(
if (!match.buildOutput) {
return;
}
let assetKey: string = requestPath;
let assetKey: string = requestPath.replace(/\/$/, '');
let asset = match.buildOutput[requestPath];

// In the case of an index path, fall back to iterating over the
// builder outputs and doing an "is index" check until we find one.
// builder outputs and doing an "is index" check until a match is found.
if (!asset) {
const requestDir = dirname(requestPath);
for (const [name, a] of Object.entries(match.buildOutput)) {
if (dirname(name) === requestDir && isIndex(name)) {
if (isIndex(name) && dirnameWithoutDot(name) === assetKey) {
asset = a;
assetKey = name;
break;
}
}
}
Expand All @@ -719,6 +717,14 @@ function findAsset(
}
}

function dirnameWithoutDot(path: string): string {
let dir = dirname(path);
if (dir === '.') {
dir = '';
}
return dir;
}

function isIndex(path: string): boolean {
const ext = extname(path);
const name = basename(path, ext);
Expand Down