diff --git a/src/index.ts b/src/index.ts index 2eb801f..3ee25d6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,9 +16,11 @@ import { normalizeFilepath, pathExistsSync, isHttpURL, + isInDenoDir, } from "./utils"; import { universalModuleResolver } from "./module_resolver/universal_module_resolver"; +import { HashMeta } from "./module_resolver/hash_meta"; let logger: Logger; let pluginInfo: ts_module.server.PluginCreateInfo; @@ -471,10 +473,24 @@ function parseModuleName( ): string | undefined { if (parsedImportMap != null) { try { + let scriptURL: URL; + if (isInDenoDir(containingFile)) { + const meta = HashMeta.create(`${containingFile}.metadata.json`); + if (meta && meta.url) { + scriptURL = meta.url; + } else { + scriptURL = new URL("file:///" + path.dirname(containingFile) + "/") + } + } else { + scriptURL = new URL("file:///" + path.dirname(containingFile) + "/") + } + + logger && logger.info(`baseUrl: ${scriptURL}`) + const moduleUrl = resolve( moduleName, parsedImportMap, - new URL("file:///" + path.dirname(containingFile) + "/"), + scriptURL, ); if (moduleUrl.protocol === "file:") { diff --git a/src/utils.ts b/src/utils.ts index 6ca4362..94dd7bd 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -37,6 +37,12 @@ export function getDenoDepsDir(): string { return path.join(getDenoDir(), "deps"); } +export function isInDenoDir(filepath: string): boolean { + filepath = normalizeFilepath(filepath); + const denoDir = getDenoDir(); + return filepath.startsWith(denoDir); +} + export function getPluginPath( tsLsHost: ts.LanguageServiceHost, ): string {