1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-29 16:30:56 -05:00
denoland-deno/cli/rt/40_compiler_api.js
Bartek Iwańczuk 6c4da0e429
refactor: remove dispatch_json.js from cli/rt and cli/tsc (#7521)
Instead use Deno.core.jsonOpSync and Deno.core.jsonOpAsync
2020-09-16 22:22:43 +02:00

100 lines
2.4 KiB
JavaScript

// 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 core = window.Deno.core;
const util = window.__bootstrap.util;
function opCompile(request) {
return core.jsonOpAsync("op_compile", request);
}
function opTranspile(
request,
) {
return core.jsonOpAsync("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);