summaryrefslogtreecommitdiff
path: root/cli/js/compiler/imports.ts
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2020-05-04 09:39:40 -0400
committerGitHub <noreply@github.com>2020-05-04 09:39:40 -0400
commit821a4ae5fd92a03dacf125b35bcf977343d27310 (patch)
tree5e9d2c859a9b1d5a2e4d7b439439be7839eaa0a9 /cli/js/compiler/imports.ts
parent58d0c4f9d69591b0cca0152c8f7ce055a2185914 (diff)
Make it so ts compiler doesn't call cwd op (#5070)
Removes duplicate implementation of the module resolution algorithm
Diffstat (limited to 'cli/js/compiler/imports.ts')
-rw-r--r--cli/js/compiler/imports.ts63
1 files changed, 17 insertions, 46 deletions
diff --git a/cli/js/compiler/imports.ts b/cli/js/compiler/imports.ts
index 4261a4123..de4402758 100644
--- a/cli/js/compiler/imports.ts
+++ b/cli/js/compiler/imports.ts
@@ -1,60 +1,30 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { MediaType, SourceFile, SourceFileJson } from "./sourcefile.ts";
-import { normalizeString, CHAR_FORWARD_SLASH } from "./util.ts";
-import { cwd } from "../ops/fs/dir.ts";
import { assert } from "../util.ts";
import * as util from "../util.ts";
import * as compilerOps from "../ops/compiler.ts";
-function resolvePath(...pathSegments: string[]): string {
- let resolvedPath = "";
- let resolvedAbsolute = false;
-
- for (let i = pathSegments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
- let path: string;
-
- if (i >= 0) path = pathSegments[i];
- else path = cwd();
-
- // Skip empty entries
- if (path.length === 0) {
- continue;
- }
-
- resolvedPath = `${path}/${resolvedPath}`;
- resolvedAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
- }
-
- // At this point the path should be resolved to a full absolute path, but
- // handle relative paths to be safe (might happen when cwd() fails)
-
- // Normalize the path
- resolvedPath = normalizeString(
- resolvedPath,
- !resolvedAbsolute,
- "/",
- (code) => code === CHAR_FORWARD_SLASH
- );
-
- if (resolvedAbsolute) {
- if (resolvedPath.length > 0) return `/${resolvedPath}`;
- else return "/";
- } else if (resolvedPath.length > 0) return resolvedPath;
- else return ".";
-}
-
function resolveSpecifier(specifier: string, referrer: string): string {
- if (!specifier.startsWith(".")) {
- return specifier;
+ // The resolveModules op only handles fully qualified URLs for referrer.
+ // However we will have cases where referrer is "/foo.ts". We add this dummy
+ // prefix "file://" in order to use the op.
+ // TODO(ry) Maybe we should perhaps ModuleSpecifier::resolve_import() to
+ // handle this situation.
+ let dummyPrefix = false;
+ const prefix = "file://";
+ if (referrer.startsWith("/")) {
+ dummyPrefix = true;
+ referrer = prefix + referrer;
+ }
+ let r = resolveModules([specifier], referrer)[0];
+ if (dummyPrefix) {
+ r = r.replace(prefix, "");
}
- const pathParts = referrer.split("/");
- pathParts.pop();
- let path = pathParts.join("/");
- path = path.endsWith("/") ? path : `${path}/`;
- return resolvePath(path, specifier);
+ return r;
}
+// TODO(ry) Remove. Unnecessary redirection to compilerOps.resolveModules.
export function resolveModules(
specifiers: string[],
referrer?: string
@@ -63,6 +33,7 @@ export function resolveModules(
return compilerOps.resolveModules(specifiers, referrer);
}
+// TODO(ry) Remove. Unnecessary redirection to compilerOps.fetchSourceFiles.
function fetchSourceFiles(
specifiers: string[],
referrer?: string