From 67897bc762b1c1b2997b36292ca91eb777a3e9bf Mon Sep 17 00:00:00 2001 From: axetroy Date: Wed, 29 Apr 2020 15:07:14 +0800 Subject: [PATCH] fix: file protocol import statement not work. close #146 --- core/module_resolver.test.ts | 19 +++++++++++++++++++ core/module_resolver.ts | 8 ++++++++ 2 files changed, 27 insertions(+) diff --git a/core/module_resolver.test.ts b/core/module_resolver.test.ts index cf350a1..d4f1bdf 100644 --- a/core/module_resolver.test.ts +++ b/core/module_resolver.test.ts @@ -76,6 +76,9 @@ test("core / module_resolver: resolve module from local", () => { "./module_not_exist.ts", "https://module.not.exist.com/mod.ts", "https://example.com/x-typescript-types", + `file://${path.join(__dirname, "cache.ts")}`, + `file://./cache.ts`, + `file://../client/src/extension.ts`, ]) ).toEqual([ { @@ -128,6 +131,22 @@ test("core / module_resolver: resolve module from local", () => { "7617203222d94a074bea3e57a893d74af5546f17c1f90760f37f46299faf0cb0" ), }, + { + extension: ".ts", + origin: + "file:///Users/axetroy/gpm/github.com/axetroy/vscode-deno/core/cache.ts", + filepath: path.join(__dirname, "cache.ts"), + }, + { + extension: ".ts", + origin: "file://./cache.ts", + filepath: path.join(__dirname, "cache.ts"), + }, + { + extension: ".ts", + origin: "file://../client/src/extension.ts", + filepath: path.join(__dirname, "..", "client", "src", "extension.ts"), + }, ] as ResolvedModule[]); }); diff --git a/core/module_resolver.ts b/core/module_resolver.ts index b893df3..7129184 100644 --- a/core/module_resolver.ts +++ b/core/module_resolver.ts @@ -136,6 +136,14 @@ export class ModuleResolver implements ModuleResolverInterface { return this.resolveFromRemote(moduleName, originModuleName); } + if (moduleName.startsWith("file://")) { + // file protocol is always a unix style path + // eg: file:///Users/deno/project/mod.ts in MacOS + // eg: file:///Home/deno/project/mod.ts in Linux + // eg: file://d:/project/mod.ts in Window + moduleName = moduleName.replace(/^file:\/\//, ""); + } + const moduleFilepath = path.resolve( path.dirname(this.containingFile), normalizeFilepath(moduleName)