2020-01-21 10:01:55 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-01-08 09:17:44 -05:00
|
|
|
|
2020-03-11 05:53:06 -04:00
|
|
|
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";
|
2020-01-08 09:17:44 -05:00
|
|
|
|
2020-01-24 14:15:01 -05:00
|
|
|
export enum CompilerHostTarget {
|
|
|
|
Main = "main",
|
|
|
|
Runtime = "runtime",
|
2020-03-28 13:03:49 -04:00
|
|
|
Worker = "worker",
|
2020-01-24 14:15:01 -05:00
|
|
|
}
|
|
|
|
|
2020-01-08 09:17:44 -05:00
|
|
|
export interface CompilerHostOptions {
|
|
|
|
bundle?: boolean;
|
2020-02-19 00:34:11 -05:00
|
|
|
|
2020-01-24 14:15:01 -05:00
|
|
|
target: CompilerHostTarget;
|
2020-02-19 00:34:11 -05:00
|
|
|
|
2020-01-08 09:17:44 -05:00
|
|
|
writeFile: WriteFileCallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ConfigureResponse {
|
|
|
|
ignoredOptions?: string[];
|
|
|
|
diagnostics?: ts.Diagnostic[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export const defaultBundlerOptions: ts.CompilerOptions = {
|
2020-03-02 16:18:27 -05:00
|
|
|
allowJs: true,
|
2020-01-08 09:17:44 -05:00
|
|
|
inlineSourceMap: false,
|
2020-02-12 16:41:51 -05:00
|
|
|
module: ts.ModuleKind.System,
|
2020-01-08 09:17:44 -05:00
|
|
|
outDir: undefined,
|
|
|
|
outFile: `${OUT_DIR}/bundle.js`,
|
|
|
|
// disabled until we have effective way to modify source maps
|
2020-03-28 13:03:49 -04:00
|
|
|
sourceMap: false,
|
2020-01-08 09:17:44 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
export const defaultCompileOptions: ts.CompilerOptions = {
|
2020-02-25 03:32:43 -05:00
|
|
|
allowJs: false,
|
2020-01-08 09:17:44 -05:00
|
|
|
allowNonTsExtensions: true,
|
|
|
|
checkJs: false,
|
|
|
|
esModuleInterop: true,
|
2020-02-25 03:32:43 -05:00
|
|
|
jsx: ts.JsxEmit.React,
|
2020-01-08 09:17:44 -05:00
|
|
|
module: ts.ModuleKind.ESNext,
|
|
|
|
outDir: OUT_DIR,
|
|
|
|
resolveJsonModule: true,
|
|
|
|
sourceMap: true,
|
2020-02-25 03:32:43 -05:00
|
|
|
strict: true,
|
2020-01-08 09:17:44 -05:00
|
|
|
stripComments: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
target: ts.ScriptTarget.ESNext,
|
2020-01-08 09:17:44 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
export const defaultRuntimeCompileOptions: ts.CompilerOptions = {
|
2020-03-28 13:03:49 -04:00
|
|
|
outDir: undefined,
|
2020-01-08 09:17:44 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
export const defaultTranspileOptions: ts.CompilerOptions = {
|
|
|
|
esModuleInterop: true,
|
|
|
|
module: ts.ModuleKind.ESNext,
|
|
|
|
sourceMap: true,
|
|
|
|
scriptComments: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
target: ts.ScriptTarget.ESNext,
|
2020-01-08 09:17:44 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
const ignoredCompilerOptions: readonly string[] = [
|
|
|
|
"allowSyntheticDefaultImports",
|
|
|
|
"baseUrl",
|
|
|
|
"build",
|
|
|
|
"composite",
|
|
|
|
"declaration",
|
|
|
|
"declarationDir",
|
|
|
|
"declarationMap",
|
|
|
|
"diagnostics",
|
|
|
|
"downlevelIteration",
|
|
|
|
"emitBOM",
|
|
|
|
"emitDeclarationOnly",
|
|
|
|
"esModuleInterop",
|
|
|
|
"extendedDiagnostics",
|
|
|
|
"forceConsistentCasingInFileNames",
|
|
|
|
"help",
|
|
|
|
"importHelpers",
|
|
|
|
"incremental",
|
|
|
|
"inlineSourceMap",
|
|
|
|
"inlineSources",
|
|
|
|
"init",
|
|
|
|
"isolatedModules",
|
|
|
|
"listEmittedFiles",
|
|
|
|
"listFiles",
|
|
|
|
"mapRoot",
|
|
|
|
"maxNodeModuleJsDepth",
|
|
|
|
"module",
|
|
|
|
"moduleResolution",
|
|
|
|
"newLine",
|
|
|
|
"noEmit",
|
|
|
|
"noEmitHelpers",
|
|
|
|
"noEmitOnError",
|
|
|
|
"noLib",
|
|
|
|
"noResolve",
|
|
|
|
"out",
|
|
|
|
"outDir",
|
|
|
|
"outFile",
|
|
|
|
"paths",
|
|
|
|
"preserveSymlinks",
|
|
|
|
"preserveWatchOutput",
|
|
|
|
"pretty",
|
|
|
|
"rootDir",
|
|
|
|
"rootDirs",
|
|
|
|
"showConfig",
|
|
|
|
"skipDefaultLibCheck",
|
|
|
|
"skipLibCheck",
|
|
|
|
"sourceMap",
|
|
|
|
"sourceRoot",
|
|
|
|
"stripInternal",
|
|
|
|
"target",
|
|
|
|
"traceResolution",
|
|
|
|
"tsBuildInfoFile",
|
|
|
|
"types",
|
|
|
|
"typeRoots",
|
|
|
|
"version",
|
2020-03-28 13:03:49 -04:00
|
|
|
"watch",
|
2020-01-08 09:17:44 -05:00
|
|
|
];
|
|
|
|
|
2020-03-28 13:03:49 -04:00
|
|
|
function getAssetInternal(filename: string): SourceFile {
|
|
|
|
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;
|
2020-01-08 09:17:44 -05:00
|
|
|
}
|
2020-03-28 13:03:49 -04:00
|
|
|
const name = url.includes(".") ? url : `${url}.d.ts`;
|
|
|
|
const sourceCode = getAsset(name);
|
|
|
|
return new SourceFile({
|
|
|
|
url,
|
|
|
|
filename: `${ASSETS}/${name}`,
|
|
|
|
mediaType: MediaType.TypeScript,
|
|
|
|
sourceCode,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Host implements ts.CompilerHost {
|
|
|
|
readonly #options = defaultCompileOptions;
|
|
|
|
#target: CompilerHostTarget;
|
|
|
|
#writeFile: WriteFileCallback;
|
2020-01-08 09:17:44 -05:00
|
|
|
|
|
|
|
/* Deno specific APIs */
|
|
|
|
|
2020-03-10 12:08:58 -04:00
|
|
|
constructor({ bundle = false, target, writeFile }: CompilerHostOptions) {
|
2020-03-28 13:03:49 -04:00
|
|
|
this.#target = target;
|
|
|
|
this.#writeFile = writeFile;
|
2020-01-08 09:17:44 -05:00
|
|
|
if (bundle) {
|
|
|
|
// options we need to change when we are generating a bundle
|
2020-03-28 13:03:49 -04:00
|
|
|
Object.assign(this.#options, defaultBundlerOptions);
|
2020-01-08 09:17:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
configure(path: string, configurationText: string): ConfigureResponse {
|
|
|
|
util.log("compiler::host.configure", path);
|
|
|
|
assert(configurationText);
|
|
|
|
const { config, error } = ts.parseConfigFileTextToJson(
|
|
|
|
path,
|
|
|
|
configurationText
|
|
|
|
);
|
|
|
|
if (error) {
|
|
|
|
return { diagnostics: [error] };
|
|
|
|
}
|
|
|
|
const { options, errors } = ts.convertCompilerOptionsFromJson(
|
|
|
|
config.compilerOptions,
|
|
|
|
cwd()
|
|
|
|
);
|
|
|
|
const ignoredOptions: string[] = [];
|
|
|
|
for (const key of Object.keys(options)) {
|
|
|
|
if (
|
|
|
|
ignoredCompilerOptions.includes(key) &&
|
2020-03-28 13:03:49 -04:00
|
|
|
(!(key in this.#options) || options[key] !== this.#options[key])
|
2020-01-08 09:17:44 -05:00
|
|
|
) {
|
|
|
|
ignoredOptions.push(key);
|
|
|
|
delete options[key];
|
|
|
|
}
|
|
|
|
}
|
2020-03-28 13:03:49 -04:00
|
|
|
Object.assign(this.#options, options);
|
2020-01-08 09:17:44 -05:00
|
|
|
return {
|
|
|
|
ignoredOptions: ignoredOptions.length ? ignoredOptions : undefined,
|
2020-03-28 13:03:49 -04:00
|
|
|
diagnostics: errors.length ? errors : undefined,
|
2020-01-08 09:17:44 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
mergeOptions(...options: ts.CompilerOptions[]): ts.CompilerOptions {
|
2020-03-28 13:03:49 -04:00
|
|
|
Object.assign(this.#options, ...options);
|
|
|
|
return Object.assign({}, this.#options);
|
2020-01-08 09:17:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* TypeScript CompilerHost APIs */
|
|
|
|
|
|
|
|
fileExists(_fileName: string): boolean {
|
|
|
|
return notImplemented();
|
|
|
|
}
|
|
|
|
|
|
|
|
getCanonicalFileName(fileName: string): string {
|
|
|
|
return fileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
getCompilationSettings(): ts.CompilerOptions {
|
|
|
|
util.log("compiler::host.getCompilationSettings()");
|
2020-03-28 13:03:49 -04:00
|
|
|
return this.#options;
|
2020-01-08 09:17:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
getCurrentDirectory(): string {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
getDefaultLibFileName(_options: ts.CompilerOptions): string {
|
2020-02-19 00:34:11 -05:00
|
|
|
util.log("compiler::host.getDefaultLibFileName()");
|
2020-03-28 13:03:49 -04:00
|
|
|
switch (this.#target) {
|
2020-01-24 14:15:01 -05:00
|
|
|
case CompilerHostTarget.Main:
|
|
|
|
case CompilerHostTarget.Runtime:
|
2020-01-29 12:54:23 -05:00
|
|
|
return `${ASSETS}/lib.deno.window.d.ts`;
|
2020-01-24 14:15:01 -05:00
|
|
|
case CompilerHostTarget.Worker:
|
2020-01-29 12:54:23 -05:00
|
|
|
return `${ASSETS}/lib.deno.worker.d.ts`;
|
2020-01-24 14:15:01 -05:00
|
|
|
}
|
2020-01-08 09:17:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
getNewLine(): string {
|
|
|
|
return "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
getSourceFile(
|
|
|
|
fileName: string,
|
|
|
|
languageVersion: ts.ScriptTarget,
|
|
|
|
onError?: (message: string) => void,
|
|
|
|
shouldCreateNewSourceFile?: boolean
|
|
|
|
): ts.SourceFile | undefined {
|
|
|
|
util.log("compiler::host.getSourceFile", fileName);
|
|
|
|
try {
|
|
|
|
assert(!shouldCreateNewSourceFile);
|
|
|
|
const sourceFile = fileName.startsWith(ASSETS)
|
2020-03-28 13:03:49 -04:00
|
|
|
? getAssetInternal(fileName)
|
2020-01-08 09:17:44 -05:00
|
|
|
: SourceFile.get(fileName);
|
|
|
|
assert(sourceFile != null);
|
|
|
|
if (!sourceFile.tsSourceFile) {
|
2020-01-22 14:18:01 -05:00
|
|
|
assert(sourceFile.sourceCode != null);
|
2020-03-18 12:39:53 -04:00
|
|
|
// even though we assert the extension for JSON modules to the compiler
|
|
|
|
// is TypeScript, TypeScript internally analyses the filename for its
|
|
|
|
// extension and tries to parse it as JSON instead of TS. We have to
|
|
|
|
// change the filename to the TypeScript file.
|
2020-01-08 09:17:44 -05:00
|
|
|
sourceFile.tsSourceFile = ts.createSourceFile(
|
2020-03-18 12:39:53 -04:00
|
|
|
fileName.startsWith(ASSETS)
|
|
|
|
? sourceFile.filename
|
|
|
|
: fileName.toLowerCase().endsWith(".json")
|
|
|
|
? `${fileName}.ts`
|
|
|
|
: fileName,
|
2020-01-08 09:17:44 -05:00
|
|
|
sourceFile.sourceCode,
|
|
|
|
languageVersion
|
|
|
|
);
|
2020-01-22 14:18:01 -05:00
|
|
|
delete sourceFile.sourceCode;
|
2020-01-08 09:17:44 -05:00
|
|
|
}
|
2020-01-22 14:18:01 -05:00
|
|
|
return sourceFile.tsSourceFile;
|
2020-01-08 09:17:44 -05:00
|
|
|
} catch (e) {
|
|
|
|
if (onError) {
|
|
|
|
onError(String(e));
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
readFile(_fileName: string): string | undefined {
|
|
|
|
return notImplemented();
|
|
|
|
}
|
|
|
|
|
|
|
|
resolveModuleNames(
|
|
|
|
moduleNames: string[],
|
|
|
|
containingFile: string
|
|
|
|
): Array<ts.ResolvedModuleFull | undefined> {
|
|
|
|
util.log("compiler::host.resolveModuleNames", {
|
|
|
|
moduleNames,
|
2020-03-28 13:03:49 -04:00
|
|
|
containingFile,
|
2020-01-08 09:17:44 -05:00
|
|
|
});
|
2020-03-28 13:03:49 -04:00
|
|
|
return moduleNames.map((specifier) => {
|
2020-01-08 09:17:44 -05:00
|
|
|
const url = SourceFile.getUrl(specifier, containingFile);
|
|
|
|
const sourceFile = specifier.startsWith(ASSETS)
|
2020-03-28 13:03:49 -04:00
|
|
|
? getAssetInternal(specifier)
|
2020-01-08 09:17:44 -05:00
|
|
|
: url
|
|
|
|
? SourceFile.get(url)
|
|
|
|
: undefined;
|
|
|
|
if (!sourceFile) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
resolvedFileName: sourceFile.url,
|
|
|
|
isExternalLibraryImport: specifier.startsWith(ASSETS),
|
2020-03-28 13:03:49 -04:00
|
|
|
extension: sourceFile.extension,
|
2020-01-08 09:17:44 -05:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
useCaseSensitiveFileNames(): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
writeFile(
|
|
|
|
fileName: string,
|
|
|
|
data: string,
|
|
|
|
_writeByteOrderMark: boolean,
|
|
|
|
_onError?: (message: string) => void,
|
|
|
|
sourceFiles?: readonly ts.SourceFile[]
|
|
|
|
): void {
|
|
|
|
util.log("compiler::host.writeFile", fileName);
|
2020-03-28 13:03:49 -04:00
|
|
|
this.#writeFile(fileName, data, sourceFiles);
|
2020-01-08 09:17:44 -05:00
|
|
|
}
|
|
|
|
}
|