diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-05-06 00:19:18 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-06 00:19:18 +0200 |
commit | 2ecdbb62ae8f925b01e3e49f397d439a2f464a21 (patch) | |
tree | ca45ddcf322337affdf1bbb8f87139b373d2d93d /cli/js/compiler_api.ts | |
parent | 9cd7d598405f8bf6600775827f870848fd3e120a (diff) |
refactor: merge TS compiler into single file (#5091)
Diffstat (limited to 'cli/js/compiler_api.ts')
-rw-r--r-- | cli/js/compiler_api.ts | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/cli/js/compiler_api.ts b/cli/js/compiler_api.ts new file mode 100644 index 000000000..88d99e5b4 --- /dev/null +++ b/cli/js/compiler_api.ts @@ -0,0 +1,84 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +// This file contains the runtime APIs which will dispatch work to the internal +// compiler within Deno. + +import { DiagnosticItem } from "./diagnostics.ts"; +import * as util from "./util.ts"; +import * as runtimeCompilerOps from "./ops/runtime_compiler.ts"; +import { TranspileOnlyResult } from "./ops/runtime_compiler.ts"; +import { CompilerOptions } from "./compiler_options.ts"; + +function checkRelative(specifier: string): string { + return specifier.match(/^([\.\/\\]|https?:\/{2}|file:\/{2})/) + ? specifier + : `./${specifier}`; +} + +// TODO(bartlomieju): change return type to interface? +export function transpileOnly( + sources: Record<string, string>, + options: CompilerOptions = {} +): Promise<Record<string, TranspileOnlyResult>> { + util.log("Deno.transpileOnly", { sources: Object.keys(sources), options }); + const payload = { + sources, + options: JSON.stringify(options), + }; + return runtimeCompilerOps.transpile(payload); +} + +// TODO(bartlomieju): change return type to interface? +export async function compile( + rootName: string, + sources?: Record<string, string>, + options: CompilerOptions = {} +): Promise<[DiagnosticItem[] | undefined, Record<string, string>]> { + const payload = { + rootName: sources ? rootName : checkRelative(rootName), + sources, + options: JSON.stringify(options), + bundle: false, + }; + util.log("Deno.compile", { + rootName: payload.rootName, + sources: !!sources, + options, + }); + const result = await runtimeCompilerOps.compile(payload); + util.assert(result.emitMap); + const maybeDiagnostics = + result.diagnostics.length === 0 ? undefined : result.diagnostics; + + const emitMap: Record<string, string> = {}; + + for (const [key, emmitedSource] of Object.entries(result.emitMap)) { + emitMap[key] = emmitedSource.contents; + } + + return [maybeDiagnostics, emitMap]; +} + +// TODO(bartlomieju): change return type to interface? +export async function bundle( + rootName: string, + sources?: Record<string, string>, + options: CompilerOptions = {} +): Promise<[DiagnosticItem[] | undefined, string]> { + const payload = { + rootName: sources ? rootName : checkRelative(rootName), + sources, + options: JSON.stringify(options), + bundle: true, + }; + util.log("Deno.bundle", { + rootName: payload.rootName, + sources: !!sources, + options, + }); + const result = await runtimeCompilerOps.compile(payload); + util.assert(result.output); + const maybeDiagnostics = + result.diagnostics.length === 0 ? undefined : result.diagnostics; + return [maybeDiagnostics, result.output]; +} |