Support loading additional TS lib files (#3863)

Fixes #3726

This PR provides support for referencing other lib files (like lib.dom.d.ts that are not
used by default in Deno.
This commit is contained in:
Kitson Kelly 2020-02-19 16:34:11 +11:00 committed by GitHub
parent 3d5bed35e0
commit 046bbb2691
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 284 additions and 67 deletions

View file

@ -1,6 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { MediaType, SourceFile } from "./compiler_sourcefile.ts";
import { ASSETS, MediaType, SourceFile } from "./compiler_sourcefile.ts";
import { OUT_DIR, WriteFileCallback, getAsset } from "./compiler_util.ts";
import { cwd } from "./dir.ts";
import { assert, notImplemented } from "./util.ts";
@ -18,8 +18,14 @@ export enum CompilerHostTarget {
}
export interface CompilerHostOptions {
/** Flag determines if the host should assume a single bundle output. */
bundle?: boolean;
/** Determines what the default library that should be used when type checking
* TS code. */
target: CompilerHostTarget;
/** A function to be used when the program emit occurs to write out files. */
writeFile: WriteFileCallback;
}
@ -28,8 +34,6 @@ export interface ConfigureResponse {
diagnostics?: ts.Diagnostic[];
}
export const ASSETS = "$asset$";
/** Options that need to be used when generating a bundle (either trusted or
* runtime). */
export const defaultBundlerOptions: ts.CompilerOptions = {
@ -96,7 +100,6 @@ const ignoredCompilerOptions: readonly string[] = [
"inlineSources",
"init",
"isolatedModules",
"lib",
"listEmittedFiles",
"listFiles",
"mapRoot",
@ -141,7 +144,10 @@ export class Host implements ts.CompilerHost {
private _writeFile: WriteFileCallback;
private _getAsset(filename: string): SourceFile {
const url = filename.split("/").pop()!;
const lastSegment = filename.split("/").pop()!;
const url = ts.libMap.has(lastSegment)
? ts.libMap.get(lastSegment)!
: lastSegment;
const sourceFile = SourceFile.get(url);
if (sourceFile) {
return sourceFile;
@ -150,7 +156,7 @@ export class Host implements ts.CompilerHost {
const sourceCode = getAsset(name);
return new SourceFile({
url,
filename,
filename: `${ASSETS}/${name}`,
mediaType: MediaType.TypeScript,
sourceCode
});
@ -230,6 +236,7 @@ export class Host implements ts.CompilerHost {
}
getDefaultLibFileName(_options: ts.CompilerOptions): string {
util.log("compiler::host.getDefaultLibFileName()");
switch (this._target) {
case CompilerHostTarget.Main:
case CompilerHostTarget.Runtime:
@ -259,7 +266,7 @@ export class Host implements ts.CompilerHost {
if (!sourceFile.tsSourceFile) {
assert(sourceFile.sourceCode != null);
sourceFile.tsSourceFile = ts.createSourceFile(
fileName,
fileName.startsWith(ASSETS) ? sourceFile.filename : fileName,
sourceFile.sourceCode,
languageVersion
);