diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2020-05-04 09:39:40 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-04 09:39:40 -0400 |
commit | 821a4ae5fd92a03dacf125b35bcf977343d27310 (patch) | |
tree | 5e9d2c859a9b1d5a2e4d7b439439be7839eaa0a9 /cli/js/compiler | |
parent | 58d0c4f9d69591b0cca0152c8f7ce055a2185914 (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')
-rw-r--r-- | cli/js/compiler/host.ts | 9 | ||||
-rw-r--r-- | cli/js/compiler/imports.ts | 63 |
2 files changed, 23 insertions, 49 deletions
diff --git a/cli/js/compiler/host.ts b/cli/js/compiler/host.ts index afe184d3e..de2eacfa9 100644 --- a/cli/js/compiler/host.ts +++ b/cli/js/compiler/host.ts @@ -2,7 +2,6 @@ import { ASSETS, MediaType, SourceFile } from "./sourcefile.ts"; import { OUT_DIR, WriteFileCallback, getAsset } from "./util.ts"; -import { cwd } from "../ops/fs/dir.ts"; import { assert, notImplemented } from "../util.ts"; import * as util from "../util.ts"; @@ -167,7 +166,11 @@ export class Host implements ts.CompilerHost { } } - configure(path: string, configurationText: string): ConfigureResponse { + configure( + cwd: string, + path: string, + configurationText: string + ): ConfigureResponse { util.log("compiler::host.configure", path); assert(configurationText); const { config, error } = ts.parseConfigFileTextToJson( @@ -179,7 +182,7 @@ export class Host implements ts.CompilerHost { } const { options, errors } = ts.convertCompilerOptionsFromJson( config.compilerOptions, - cwd() + cwd ); const ignoredOptions: string[] = []; for (const key of Object.keys(options)) { 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 |