summaryrefslogtreecommitdiff
path: root/cli/js/compiler_api.ts
blob: e0488b7f64b519020d25ebaaf26a843110a27dd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// 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 type { DiagnosticItem } from "./diagnostics.ts";
import * as util from "./util.ts";
import * as runtimeCompilerOps from "./ops/runtime_compiler.ts";
import type { TranspileOnlyResult } from "./ops/runtime_compiler.ts";
import type { 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, emittedSource] of Object.entries(result.emitMap)) {
    emitMap[key] = emittedSource.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];
}