2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-09-02 17:07:11 -04:00
|
|
|
// TODO(ry) Combine this implementation with //deno_typescript/compiler_main.js
|
|
|
|
|
2020-01-08 09:17:44 -05:00
|
|
|
// these are imported for their side effects
|
2019-09-02 17:07:11 -04:00
|
|
|
import "./globals.ts";
|
2019-09-12 17:30:04 -04:00
|
|
|
import "./ts_global.d.ts";
|
2019-09-02 17:07:11 -04:00
|
|
|
|
2020-01-08 09:17:44 -05:00
|
|
|
import { TranspileOnlyResult } from "./compiler_api.ts";
|
2020-01-12 06:20:33 -05:00
|
|
|
import { oldProgram } from "./compiler_bootstrap.ts";
|
2020-01-08 09:17:44 -05:00
|
|
|
import { setRootExports } from "./compiler_bundler.ts";
|
|
|
|
import {
|
|
|
|
defaultBundlerOptions,
|
|
|
|
defaultRuntimeCompileOptions,
|
|
|
|
defaultTranspileOptions,
|
|
|
|
Host
|
|
|
|
} from "./compiler_host.ts";
|
|
|
|
import {
|
|
|
|
processImports,
|
|
|
|
processLocalImports,
|
|
|
|
resolveModules
|
|
|
|
} from "./compiler_imports.ts";
|
|
|
|
import {
|
|
|
|
createWriteFile,
|
|
|
|
CompilerRequestType,
|
|
|
|
convertCompilerOptions,
|
|
|
|
ignoredDiagnostics,
|
|
|
|
WriteFileState,
|
|
|
|
processConfigureResponse
|
|
|
|
} from "./compiler_util.ts";
|
|
|
|
import { Diagnostic } from "./diagnostics.ts";
|
|
|
|
import { fromTypeScriptDiagnostic } from "./diagnostics_util.ts";
|
2019-09-02 17:07:11 -04:00
|
|
|
import * as os from "./os.ts";
|
2020-01-08 09:17:44 -05:00
|
|
|
import { assert } from "./util.ts";
|
2019-09-02 17:07:11 -04:00
|
|
|
import * as util from "./util.ts";
|
2020-01-08 09:17:44 -05:00
|
|
|
import { window as self } from "./window.ts";
|
2019-09-02 17:07:11 -04:00
|
|
|
import { postMessage, workerClose, workerMain } from "./workers.ts";
|
2018-08-22 17:17:26 -04:00
|
|
|
|
2020-01-08 09:17:44 -05:00
|
|
|
interface CompilerRequestCompile {
|
|
|
|
type: CompilerRequestType.Compile;
|
2019-05-20 12:06:57 -04:00
|
|
|
rootNames: string[];
|
|
|
|
// TODO(ry) add compiler config to this interface.
|
|
|
|
// options: ts.CompilerOptions;
|
|
|
|
configPath?: string;
|
|
|
|
config?: string;
|
2020-01-08 09:17:44 -05:00
|
|
|
bundle?: boolean;
|
|
|
|
outFile?: string;
|
2019-06-04 09:03:56 -04:00
|
|
|
}
|
|
|
|
|
2020-01-08 09:17:44 -05:00
|
|
|
interface CompilerRequestRuntimeCompile {
|
|
|
|
type: CompilerRequestType.RuntimeCompile;
|
|
|
|
rootName: string;
|
|
|
|
sources?: Record<string, string>;
|
|
|
|
bundle?: boolean;
|
|
|
|
options?: string;
|
2019-10-03 07:23:29 -04:00
|
|
|
}
|
|
|
|
|
2020-01-08 09:17:44 -05:00
|
|
|
interface CompilerRequestRuntimeTranspile {
|
|
|
|
type: CompilerRequestType.RuntimeTranspile;
|
|
|
|
sources: Record<string, string>;
|
|
|
|
options?: string;
|
2019-05-20 12:06:57 -04:00
|
|
|
}
|
2018-08-24 00:07:01 -04:00
|
|
|
|
2020-01-08 09:17:44 -05:00
|
|
|
/** The format of the work message payload coming from the privileged side */
|
|
|
|
type CompilerRequest =
|
|
|
|
| CompilerRequestCompile
|
|
|
|
| CompilerRequestRuntimeCompile
|
|
|
|
| CompilerRequestRuntimeTranspile;
|
|
|
|
|
|
|
|
/** The format of the result sent back when doing a compilation. */
|
|
|
|
interface CompileResult {
|
2019-06-04 09:03:56 -04:00
|
|
|
emitSkipped: boolean;
|
|
|
|
diagnostics?: Diagnostic;
|
|
|
|
}
|
|
|
|
|
2020-01-08 09:17:44 -05:00
|
|
|
// bootstrap the runtime environment, this gets called as the isolate is setup
|
|
|
|
self.denoMain = function denoMain(compilerType?: string): void {
|
2020-01-12 06:20:33 -05:00
|
|
|
os.start(true, compilerType ?? "TS");
|
2020-01-08 09:17:44 -05:00
|
|
|
};
|
2019-06-08 14:42:28 -04:00
|
|
|
|
2020-01-08 09:17:44 -05:00
|
|
|
// bootstrap the worker environment, this gets called as the isolate is setup
|
|
|
|
self.workerMain = workerMain;
|
2018-08-22 17:17:26 -04:00
|
|
|
|
2019-02-02 18:27:53 -05:00
|
|
|
// provide the "main" function that will be called by the privileged side when
|
2019-01-28 20:41:28 -05:00
|
|
|
// lazy instantiating the compiler web worker
|
2020-01-08 09:17:44 -05:00
|
|
|
self.compilerMain = function compilerMain(): void {
|
2019-01-28 20:41:28 -05:00
|
|
|
// workerMain should have already been called since a compiler is a worker.
|
2020-01-08 09:17:44 -05:00
|
|
|
self.onmessage = async ({
|
2019-11-13 10:35:56 -05:00
|
|
|
data: request
|
|
|
|
}: {
|
|
|
|
data: CompilerRequest;
|
|
|
|
}): Promise<void> => {
|
2020-01-08 09:17:44 -05:00
|
|
|
switch (request.type) {
|
|
|
|
// `Compile` are requests from the internals to Deno, generated by both
|
|
|
|
// the `run` and `bundle` sub command.
|
|
|
|
case CompilerRequestType.Compile: {
|
|
|
|
const { bundle, config, configPath, outFile, rootNames } = request;
|
|
|
|
util.log(">>> compile start", {
|
|
|
|
rootNames,
|
|
|
|
type: CompilerRequestType[request.type]
|
|
|
|
});
|
|
|
|
|
|
|
|
// This will recursively analyse all the code for other imports,
|
|
|
|
// requesting those from the privileged side, populating the in memory
|
|
|
|
// cache which will be used by the host, before resolving.
|
|
|
|
const resolvedRootModules = await processImports(
|
|
|
|
rootNames.map(rootName => [rootName, rootName])
|
2019-05-20 12:06:57 -04:00
|
|
|
);
|
2018-08-22 17:17:26 -04:00
|
|
|
|
2020-01-08 09:17:44 -05:00
|
|
|
// When a programme is emitted, TypeScript will call `writeFile` with
|
|
|
|
// each file that needs to be emitted. The Deno compiler host delegates
|
|
|
|
// this, to make it easier to perform the right actions, which vary
|
|
|
|
// based a lot on the request. For a `Compile` request, we need to
|
|
|
|
// cache all the files in the privileged side if we aren't bundling,
|
|
|
|
// and if we are bundling we need to enrich the bundle and either write
|
|
|
|
// out the bundle or log it to the console.
|
|
|
|
const state: WriteFileState = {
|
|
|
|
type: request.type,
|
|
|
|
bundle,
|
|
|
|
host: undefined,
|
|
|
|
outFile,
|
|
|
|
rootNames
|
|
|
|
};
|
|
|
|
const writeFile = createWriteFile(state);
|
|
|
|
|
|
|
|
const host = (state.host = new Host({ bundle, writeFile }));
|
|
|
|
let diagnostics: readonly ts.Diagnostic[] | undefined;
|
|
|
|
|
|
|
|
// if there is a configuration supplied, we need to parse that
|
|
|
|
if (config && config.length && configPath) {
|
|
|
|
const configResult = host.configure(configPath, config);
|
|
|
|
diagnostics = processConfigureResponse(configResult, configPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
let emitSkipped = true;
|
|
|
|
// if there was a configuration and no diagnostics with it, we will continue
|
|
|
|
// to generate the program and possibly emit it.
|
|
|
|
if (!diagnostics || (diagnostics && diagnostics.length === 0)) {
|
|
|
|
const options = host.getCompilationSettings();
|
2020-01-12 06:20:33 -05:00
|
|
|
const program = ts.createProgram({
|
|
|
|
rootNames,
|
|
|
|
options,
|
|
|
|
host,
|
|
|
|
oldProgram
|
|
|
|
});
|
2020-01-08 09:17:44 -05:00
|
|
|
|
|
|
|
diagnostics = ts
|
|
|
|
.getPreEmitDiagnostics(program)
|
|
|
|
.filter(({ code }) => !ignoredDiagnostics.includes(code));
|
|
|
|
|
|
|
|
// We will only proceed with the emit if there are no diagnostics.
|
|
|
|
if (diagnostics && diagnostics.length === 0) {
|
|
|
|
if (bundle) {
|
|
|
|
// we only support a single root module when bundling
|
|
|
|
assert(resolvedRootModules.length === 1);
|
|
|
|
// warning so it goes to stderr instead of stdout
|
|
|
|
console.warn(`Bundling "${resolvedRootModules[0]}"`);
|
|
|
|
setRootExports(program, resolvedRootModules[0]);
|
|
|
|
}
|
|
|
|
const emitResult = program.emit();
|
|
|
|
emitSkipped = emitResult.emitSkipped;
|
|
|
|
// emitResult.diagnostics is `readonly` in TS3.5+ and can't be assigned
|
|
|
|
// without casting.
|
|
|
|
diagnostics = emitResult.diagnostics;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const result: CompileResult = {
|
|
|
|
emitSkipped,
|
|
|
|
diagnostics: diagnostics.length
|
|
|
|
? fromTypeScriptDiagnostic(diagnostics)
|
|
|
|
: undefined
|
|
|
|
};
|
|
|
|
postMessage(result);
|
|
|
|
|
|
|
|
util.log("<<< compile end", {
|
|
|
|
rootNames,
|
|
|
|
type: CompilerRequestType[request.type]
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CompilerRequestType.RuntimeCompile: {
|
|
|
|
// `RuntimeCompile` are requests from a runtime user, both compiles and
|
|
|
|
// bundles. The process is similar to a request from the privileged
|
|
|
|
// side, but also returns the output to the on message.
|
|
|
|
const { rootName, sources, options, bundle } = request;
|
|
|
|
|
|
|
|
util.log(">>> runtime compile start", {
|
|
|
|
rootName,
|
|
|
|
bundle,
|
|
|
|
sources: sources ? Object.keys(sources) : undefined
|
2019-11-13 13:42:34 -05:00
|
|
|
});
|
2019-06-04 09:03:56 -04:00
|
|
|
|
2020-01-08 09:17:44 -05:00
|
|
|
const resolvedRootName = sources
|
|
|
|
? rootName
|
|
|
|
: resolveModules([rootName])[0];
|
|
|
|
|
|
|
|
const rootNames = sources
|
|
|
|
? processLocalImports(sources, [[resolvedRootName, resolvedRootName]])
|
|
|
|
: await processImports([[resolvedRootName, resolvedRootName]]);
|
|
|
|
|
|
|
|
const state: WriteFileState = {
|
|
|
|
type: request.type,
|
|
|
|
bundle,
|
|
|
|
host: undefined,
|
|
|
|
rootNames,
|
|
|
|
sources,
|
|
|
|
emitMap: {},
|
|
|
|
emitBundle: undefined
|
|
|
|
};
|
|
|
|
const writeFile = createWriteFile(state);
|
|
|
|
|
|
|
|
const host = (state.host = new Host({ bundle, writeFile }));
|
|
|
|
const compilerOptions = [defaultRuntimeCompileOptions];
|
|
|
|
if (options) {
|
|
|
|
compilerOptions.push(convertCompilerOptions(options));
|
2019-06-08 14:42:28 -04:00
|
|
|
}
|
2020-01-08 09:17:44 -05:00
|
|
|
if (bundle) {
|
|
|
|
compilerOptions.push(defaultBundlerOptions);
|
2019-11-20 11:02:08 -05:00
|
|
|
}
|
2020-01-08 09:17:44 -05:00
|
|
|
host.mergeOptions(...compilerOptions);
|
|
|
|
|
2020-01-12 06:20:33 -05:00
|
|
|
const program = ts.createProgram({
|
2020-01-08 09:17:44 -05:00
|
|
|
rootNames,
|
2020-01-12 06:20:33 -05:00
|
|
|
options: host.getCompilationSettings(),
|
|
|
|
host,
|
|
|
|
oldProgram
|
|
|
|
});
|
2020-01-08 09:17:44 -05:00
|
|
|
|
|
|
|
if (bundle) {
|
|
|
|
setRootExports(program, rootNames[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const diagnostics = ts
|
|
|
|
.getPreEmitDiagnostics(program)
|
|
|
|
.filter(({ code }) => !ignoredDiagnostics.includes(code));
|
|
|
|
|
2019-06-04 09:03:56 -04:00
|
|
|
const emitResult = program.emit();
|
2019-06-03 12:58:49 -04:00
|
|
|
|
2020-01-08 09:17:44 -05:00
|
|
|
assert(
|
|
|
|
emitResult.emitSkipped === false,
|
|
|
|
"Unexpected skip of the emit."
|
|
|
|
);
|
|
|
|
const { items } = fromTypeScriptDiagnostic(diagnostics);
|
|
|
|
const result = [
|
|
|
|
items && items.length ? items : undefined,
|
|
|
|
bundle ? state.emitBundle : state.emitMap
|
|
|
|
];
|
|
|
|
postMessage(result);
|
|
|
|
|
|
|
|
assert(state.emitMap);
|
|
|
|
util.log("<<< runtime compile finish", {
|
|
|
|
rootName,
|
|
|
|
sources: sources ? Object.keys(sources) : undefined,
|
|
|
|
bundle,
|
|
|
|
emitMap: Object.keys(state.emitMap)
|
|
|
|
});
|
2019-04-29 10:58:31 -04:00
|
|
|
|
2020-01-08 09:17:44 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CompilerRequestType.RuntimeTranspile: {
|
|
|
|
const result: Record<string, TranspileOnlyResult> = {};
|
|
|
|
const { sources, options } = request;
|
|
|
|
const compilerOptions = options
|
|
|
|
? Object.assign(
|
|
|
|
{},
|
|
|
|
defaultTranspileOptions,
|
|
|
|
convertCompilerOptions(options)
|
|
|
|
)
|
|
|
|
: defaultTranspileOptions;
|
|
|
|
|
|
|
|
for (const [fileName, inputText] of Object.entries(sources)) {
|
|
|
|
const { outputText: source, sourceMapText: map } = ts.transpileModule(
|
|
|
|
inputText,
|
|
|
|
{
|
|
|
|
fileName,
|
|
|
|
compilerOptions
|
|
|
|
}
|
|
|
|
);
|
|
|
|
result[fileName] = { source, map };
|
|
|
|
}
|
|
|
|
postMessage(result);
|
2019-04-29 10:58:31 -04:00
|
|
|
|
2020-01-08 09:17:44 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
util.log(
|
|
|
|
`!!! unhandled CompilerRequestType: ${
|
|
|
|
(request as CompilerRequest).type
|
|
|
|
} (${CompilerRequestType[(request as CompilerRequest).type]})`
|
|
|
|
);
|
|
|
|
}
|
2019-10-03 07:23:29 -04:00
|
|
|
|
2019-06-04 09:03:56 -04:00
|
|
|
// The compiler isolate exits after a single message.
|
2019-05-20 12:06:57 -04:00
|
|
|
workerClose();
|
|
|
|
};
|
|
|
|
};
|
2019-11-14 08:31:39 -05:00
|
|
|
|
2020-01-08 09:17:44 -05:00
|
|
|
self.wasmCompilerMain = function wasmCompilerMain(): void {
|
2019-11-14 08:31:39 -05:00
|
|
|
// workerMain should have already been called since a compiler is a worker.
|
2020-01-08 09:17:44 -05:00
|
|
|
self.onmessage = async ({
|
2019-11-14 08:31:39 -05:00
|
|
|
data: binary
|
|
|
|
}: {
|
|
|
|
data: string;
|
|
|
|
}): Promise<void> => {
|
2020-01-08 09:17:44 -05:00
|
|
|
const buffer = util.base64ToUint8Array(binary);
|
2019-11-14 08:31:39 -05:00
|
|
|
// @ts-ignore
|
|
|
|
const compiled = await WebAssembly.compile(buffer);
|
|
|
|
|
|
|
|
util.log(">>> WASM compile start");
|
|
|
|
|
|
|
|
const importList = Array.from(
|
|
|
|
// @ts-ignore
|
|
|
|
new Set(WebAssembly.Module.imports(compiled).map(({ module }) => module))
|
|
|
|
);
|
|
|
|
const exportList = Array.from(
|
|
|
|
// @ts-ignore
|
|
|
|
new Set(WebAssembly.Module.exports(compiled).map(({ name }) => name))
|
|
|
|
);
|
|
|
|
|
2020-01-08 09:17:44 -05:00
|
|
|
postMessage({ importList, exportList });
|
2019-11-14 08:31:39 -05:00
|
|
|
|
|
|
|
util.log("<<< WASM compile end");
|
|
|
|
|
|
|
|
// The compiler isolate exits after a single message.
|
|
|
|
workerClose();
|
|
|
|
};
|
|
|
|
};
|