2020-01-21 10:01:55 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-01-08 09:17:44 -05:00
|
|
|
|
|
|
|
// This file contains the runtime APIs which will dispatch work to the internal
|
|
|
|
// compiler within Deno.
|
|
|
|
|
2020-07-08 05:26:39 -04:00
|
|
|
import type { DiagnosticItem } from "./diagnostics.ts";
|
2020-05-05 18:19:18 -04:00
|
|
|
import * as util from "./util.ts";
|
|
|
|
import * as runtimeCompilerOps from "./ops/runtime_compiler.ts";
|
2020-07-08 05:26:39 -04:00
|
|
|
import type { TranspileOnlyResult } from "./ops/runtime_compiler.ts";
|
|
|
|
import type { CompilerOptions } from "./compiler_options.ts";
|
2020-01-08 09:17:44 -05:00
|
|
|
|
|
|
|
function checkRelative(specifier: string): string {
|
|
|
|
return specifier.match(/^([\.\/\\]|https?:\/{2}|file:\/{2})/)
|
|
|
|
? specifier
|
|
|
|
: `./${specifier}`;
|
|
|
|
}
|
|
|
|
|
2020-05-05 12:23:15 -04:00
|
|
|
// TODO(bartlomieju): change return type to interface?
|
|
|
|
export function transpileOnly(
|
2020-01-08 09:17:44 -05:00
|
|
|
sources: Record<string, string>,
|
2020-07-14 15:24:17 -04:00
|
|
|
options: CompilerOptions = {},
|
2020-01-08 09:17:44 -05:00
|
|
|
): Promise<Record<string, TranspileOnlyResult>> {
|
|
|
|
util.log("Deno.transpileOnly", { sources: Object.keys(sources), options });
|
|
|
|
const payload = {
|
|
|
|
sources,
|
2020-03-28 13:03:49 -04:00
|
|
|
options: JSON.stringify(options),
|
2020-01-08 09:17:44 -05:00
|
|
|
};
|
2020-05-05 12:23:15 -04:00
|
|
|
return runtimeCompilerOps.transpile(payload);
|
2020-01-08 09:17:44 -05:00
|
|
|
}
|
|
|
|
|
2020-05-05 12:23:15 -04:00
|
|
|
// TODO(bartlomieju): change return type to interface?
|
2020-03-08 08:09:22 -04:00
|
|
|
export async function compile(
|
2020-01-08 09:17:44 -05:00
|
|
|
rootName: string,
|
|
|
|
sources?: Record<string, string>,
|
2020-07-14 15:24:17 -04:00
|
|
|
options: CompilerOptions = {},
|
2020-02-07 01:54:05 -05:00
|
|
|
): Promise<[DiagnosticItem[] | undefined, Record<string, string>]> {
|
2020-01-08 09:17:44 -05:00
|
|
|
const payload = {
|
|
|
|
rootName: sources ? rootName : checkRelative(rootName),
|
|
|
|
sources,
|
2020-03-10 12:08:58 -04:00
|
|
|
options: JSON.stringify(options),
|
2020-03-28 13:03:49 -04:00
|
|
|
bundle: false,
|
2020-01-08 09:17:44 -05:00
|
|
|
};
|
|
|
|
util.log("Deno.compile", {
|
|
|
|
rootName: payload.rootName,
|
|
|
|
sources: !!sources,
|
2020-03-28 13:03:49 -04:00
|
|
|
options,
|
2020-01-08 09:17:44 -05:00
|
|
|
});
|
2020-03-08 08:09:22 -04:00
|
|
|
const result = await runtimeCompilerOps.compile(payload);
|
2020-05-05 12:23:15 -04:00
|
|
|
util.assert(result.emitMap);
|
2020-07-14 15:24:17 -04:00
|
|
|
const maybeDiagnostics = result.diagnostics.length === 0
|
|
|
|
? undefined
|
|
|
|
: result.diagnostics;
|
2020-05-05 12:23:15 -04:00
|
|
|
|
|
|
|
const emitMap: Record<string, string> = {};
|
|
|
|
|
2020-07-06 21:45:39 -04:00
|
|
|
for (const [key, emittedSource] of Object.entries(result.emitMap)) {
|
|
|
|
emitMap[key] = emittedSource.contents;
|
2020-05-05 12:23:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return [maybeDiagnostics, emitMap];
|
2020-01-08 09:17:44 -05:00
|
|
|
}
|
|
|
|
|
2020-05-05 12:23:15 -04:00
|
|
|
// TODO(bartlomieju): change return type to interface?
|
2020-03-08 08:09:22 -04:00
|
|
|
export async function bundle(
|
2020-01-08 09:17:44 -05:00
|
|
|
rootName: string,
|
|
|
|
sources?: Record<string, string>,
|
2020-07-14 15:24:17 -04:00
|
|
|
options: CompilerOptions = {},
|
2020-02-07 01:54:05 -05:00
|
|
|
): Promise<[DiagnosticItem[] | undefined, string]> {
|
2020-01-08 09:17:44 -05:00
|
|
|
const payload = {
|
|
|
|
rootName: sources ? rootName : checkRelative(rootName),
|
|
|
|
sources,
|
2020-03-10 12:08:58 -04:00
|
|
|
options: JSON.stringify(options),
|
2020-03-28 13:03:49 -04:00
|
|
|
bundle: true,
|
2020-01-08 09:17:44 -05:00
|
|
|
};
|
|
|
|
util.log("Deno.bundle", {
|
|
|
|
rootName: payload.rootName,
|
|
|
|
sources: !!sources,
|
2020-03-28 13:03:49 -04:00
|
|
|
options,
|
2020-01-08 09:17:44 -05:00
|
|
|
});
|
2020-03-08 08:09:22 -04:00
|
|
|
const result = await runtimeCompilerOps.compile(payload);
|
2020-05-05 12:23:15 -04:00
|
|
|
util.assert(result.output);
|
2020-07-14 15:24:17 -04:00
|
|
|
const maybeDiagnostics = result.diagnostics.length === 0
|
|
|
|
? undefined
|
|
|
|
: result.diagnostics;
|
2020-05-05 12:23:15 -04:00
|
|
|
return [maybeDiagnostics, result.output];
|
2020-01-08 09:17:44 -05:00
|
|
|
}
|