summaryrefslogtreecommitdiff
path: root/runtime/js/40_compiler_api.js
blob: 0d36001013601914aec141b4331a0ad565b08a9c (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
87
88
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.

// @ts-check

// This file contains the runtime APIs which will dispatch work to the internal
// compiler within Deno.
((window) => {
  const core = window.Deno.core;
  const util = window.__bootstrap.util;

  /**
   * @typedef {object} ImportMap
   * @property {Record<string, string>} imports
   * @property {Record<string, Record<string, string>>=} scopes
   */

  /**
   * @typedef {object} OpEmitRequest
   * @property {"esm"=} bundle
   * @property {boolean=} check
   * @property {Record<string, any>=} compilerOptions
   * @property {ImportMap=} importMap
   * @property {string=} importMapPath
   * @property {string} rootSpecifier
   * @property {Record<string, string>=} sources
   */

  /**
   * @typedef OpEmitResponse
   * @property {any[]} diagnostics
   * @property {Record<string, string>} files
   * @property {string[]=} ignoredOptions
   * @property {Array<[string, number]>} stats
   */

  /**
   * @param {OpEmitRequest} request 
   * @returns {Promise<OpEmitResponse>}
   */
  function opEmit(request) {
    return core.jsonOpAsync("op_emit", request);
  }

  /**
   * @param {string} specifier 
   * @returns {string}
   */
  function checkRelative(specifier) {
    return specifier.match(/^([\.\/\\]|https?:\/{2}|file:\/{2})/)
      ? specifier
      : `./${specifier}`;
  }

  /**
   * @typedef {object} EmitOptions
   * @property {"esm"=} bundle
   * @property {boolean=} check
   * @property {Record<string, any>=} compilerOptions
   * @property {ImportMap=} importMap
   * @property {string=} importMapPath
   * @property {Record<string, string>=} sources
   */

  /**
   * @param {string | URL} rootSpecifier 
   * @param {EmitOptions=} options
   * @returns {Promise<OpEmitResponse>} 
   */
  function emit(rootSpecifier, options = {}) {
    util.log(`Deno.emit`, { rootSpecifier });
    if (!rootSpecifier) {
      return Promise.reject(
        new TypeError("A root specifier must be supplied."),
      );
    }
    if (!(typeof rootSpecifier === "string")) {
      rootSpecifier = rootSpecifier.toString();
    }
    if (!options.sources) {
      rootSpecifier = checkRelative(rootSpecifier);
    }
    return opEmit({ rootSpecifier, ...options });
  }

  window.__bootstrap.compilerApi = {
    emit,
  };
})(this);