Skip to content

Commit

Permalink
handle require.resolve failure
Browse files Browse the repository at this point in the history
if require.resolve fails, defer to default behaviour -url.resolve(base, ref)
  • Loading branch information
Alasdair McLeay committed Mar 20, 2019
1 parent b170858 commit 2e9bd07
Showing 1 changed file with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ function getModuleNameFromPath(path: string) {
function resolvePathToModule(moduleName: string, relativeTo: string) {
// if we require.resolve('my-module') then it will follow the main property in the linked package.json
// but we want the root of the module so resolve to the package.json and then trim
return require
.resolve(`${moduleName}/package.json`, { paths: [relativeTo] })
.slice(0, -12); // remove trailing `package.json`
let resolved;
try {
resolved = require
.resolve(`${moduleName}/package.json`, { paths: [relativeTo] });
}
catch (ex) {
return null;
}
return resolved.slice(0, -12); // remove trailing `package.json`
}

export function getDocumentContext(documentUri: string, workspaceFolders: WorkspaceFolder[]): DocumentContext {
Expand Down Expand Up @@ -55,8 +61,10 @@ export function getDocumentContext(documentUri: string, workspaceFolders: Worksp
if (ref[0] === '~' && ref[1] !== '/') {
const moduleName = getModuleNameFromPath(ref.substring(1));
const modulePath = resolvePathToModule(moduleName, base);
const pathWithinModule = ref.substring(moduleName.length + 2);
return url.resolve(modulePath, pathWithinModule);
if (modulePath) {
const pathWithinModule = ref.substring(moduleName.length + 2);
return url.resolve(modulePath, pathWithinModule);
}
}
return url.resolve(base, ref);
},
Expand Down

0 comments on commit 2e9bd07

Please sign in to comment.