diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2020-07-23 10:27:26 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-23 16:27:26 +0200 |
commit | ca4dcb36dd5be0b14a2fafa059ea02ee7e0a0262 (patch) | |
tree | 393fc85f19e97bb66402676f67e63690d7a29e00 /cli/rt/40_compiler_api.js | |
parent | 090455936c892b6f2dfa425be9b1cdfb4c63af4a (diff) |
Rename cli/js2 to cli/rt (#6857)
Diffstat (limited to 'cli/rt/40_compiler_api.js')
-rw-r--r-- | cli/rt/40_compiler_api.js | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/cli/rt/40_compiler_api.js b/cli/rt/40_compiler_api.js new file mode 100644 index 000000000..8a2aa759a --- /dev/null +++ b/cli/rt/40_compiler_api.js @@ -0,0 +1,100 @@ +// 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. +((window) => { + const util = window.__bootstrap.util; + const { sendAsync } = window.__bootstrap.dispatchJson; + + function opCompile(request) { + return sendAsync("op_compile", request); + } + + function opTranspile( + request, + ) { + return sendAsync("op_transpile", request); + } + + function checkRelative(specifier) { + return specifier.match(/^([\.\/\\]|https?:\/{2}|file:\/{2})/) + ? specifier + : `./${specifier}`; + } + + // TODO(bartlomieju): change return type to interface? + function transpileOnly( + sources, + options = {}, + ) { + util.log("Deno.transpileOnly", { sources: Object.keys(sources), options }); + const payload = { + sources, + options: JSON.stringify(options), + }; + return opTranspile(payload); + } + + // TODO(bartlomieju): change return type to interface? + async function compile( + rootName, + sources, + options = {}, + ) { + 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 opCompile(payload); + util.assert(result.emitMap); + const maybeDiagnostics = result.diagnostics.length === 0 + ? undefined + : result.diagnostics; + + const emitMap = {}; + + for (const [key, emittedSource] of Object.entries(result.emitMap)) { + emitMap[key] = emittedSource.contents; + } + + return [maybeDiagnostics, emitMap]; + } + + // TODO(bartlomieju): change return type to interface? + async function bundle( + rootName, + sources, + options = {}, + ) { + 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 opCompile(payload); + util.assert(result.output); + const maybeDiagnostics = result.diagnostics.length === 0 + ? undefined + : result.diagnostics; + return [maybeDiagnostics, result.output]; + } + + window.__bootstrap.compilerApi = { + bundle, + compile, + transpileOnly, + }; +})(this); |