mirror of
https://github.com/microsoft/vscode.git
synced 2025-12-24 12:19:20 +00:00
handle require.resolve failure
if require.resolve fails, defer to default behaviour -url.resolve(base, ref)
This commit is contained in:
@@ -19,9 +19,15 @@ function getModuleNameFromPath(path: string) {
|
|||||||
function resolvePathToModule(moduleName: string, relativeTo: 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
|
// 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
|
// but we want the root of the module so resolve to the package.json and then trim
|
||||||
return require
|
let resolved;
|
||||||
.resolve(`${moduleName}/package.json`, { paths: [relativeTo] })
|
try {
|
||||||
.slice(0, -12); // remove trailing `package.json`
|
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 {
|
export function getDocumentContext(documentUri: string, workspaceFolders: WorkspaceFolder[]): DocumentContext {
|
||||||
@@ -55,8 +61,10 @@ export function getDocumentContext(documentUri: string, workspaceFolders: Worksp
|
|||||||
if (ref[0] === '~' && ref[1] !== '/') {
|
if (ref[0] === '~' && ref[1] !== '/') {
|
||||||
const moduleName = getModuleNameFromPath(ref.substring(1));
|
const moduleName = getModuleNameFromPath(ref.substring(1));
|
||||||
const modulePath = resolvePathToModule(moduleName, base);
|
const modulePath = resolvePathToModule(moduleName, base);
|
||||||
const pathWithinModule = ref.substring(moduleName.length + 2);
|
if (modulePath) {
|
||||||
return url.resolve(modulePath, pathWithinModule);
|
const pathWithinModule = ref.substring(moduleName.length + 2);
|
||||||
|
return url.resolve(modulePath, pathWithinModule);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return url.resolve(base, ref);
|
return url.resolve(base, ref);
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user