diff options
Diffstat (limited to 'runtime/js/40_compiler_api.js')
-rw-r--r-- | runtime/js/40_compiler_api.js | 139 |
1 files changed, 65 insertions, 74 deletions
diff --git a/runtime/js/40_compiler_api.js b/runtime/js/40_compiler_api.js index ea963b67b..ced4bcb00 100644 --- a/runtime/js/40_compiler_api.js +++ b/runtime/js/40_compiler_api.js @@ -1,97 +1,88 @@ // Copyright 2018-2020 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; - function opCompile(request) { - return core.jsonOpAsync("op_compile", request); - } + /** + * @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 + */ - function opTranspile( - request, - ) { - return core.jsonOpAsync("op_transpile", request); + /** + * @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}`; } - // 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, - }); - /** @type {{ emittedFiles: Record<string, string>, diagnostics: any[] }} */ - const result = await opCompile(payload); - util.assert(result.emittedFiles); - const maybeDiagnostics = result.diagnostics.length === 0 - ? undefined - : result.diagnostics; - - return [maybeDiagnostics, result.emittedFiles]; - } + /** + * @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 + */ - // 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, - }); - /** @type {{ emittedFiles: Record<string, string>, diagnostics: any[] }} */ - const result = await opCompile(payload); - const output = result.emittedFiles["deno:///bundle.js"]; - util.assert(output); - const maybeDiagnostics = result.diagnostics.length === 0 - ? undefined - : result.diagnostics; - return [maybeDiagnostics, output]; + /** + * @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 = { - bundle, - compile, - transpileOnly, + emit, }; })(this); |