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
|
|
|
|
|
|
|
|
import "./globals.ts";
|
2019-09-12 17:30:04 -04:00
|
|
|
import "./ts_global.d.ts";
|
2019-09-02 17:07:11 -04:00
|
|
|
|
2019-11-20 11:02:08 -05:00
|
|
|
import { emitBundle, setRootExports } from "./bundler.ts";
|
2019-09-02 17:07:11 -04:00
|
|
|
import { bold, cyan, yellow } from "./colors.ts";
|
|
|
|
import { Console } from "./console.ts";
|
|
|
|
import { core } from "./core.ts";
|
|
|
|
import { Diagnostic, fromTypeScriptDiagnostic } from "./diagnostics.ts";
|
|
|
|
import { cwd } from "./dir.ts";
|
|
|
|
import * as dispatch from "./dispatch.ts";
|
2019-10-03 07:23:29 -04:00
|
|
|
import { sendAsync, sendSync } from "./dispatch_json.ts";
|
2019-09-02 17:07:11 -04:00
|
|
|
import * as os from "./os.ts";
|
|
|
|
import { getMappedModuleName, parseTypeDirectives } from "./type_directives.ts";
|
|
|
|
import { assert, notImplemented } from "./util.ts";
|
|
|
|
import * as util from "./util.ts";
|
|
|
|
import { window } from "./window.ts";
|
|
|
|
import { postMessage, workerClose, workerMain } from "./workers.ts";
|
2018-08-22 17:17:26 -04:00
|
|
|
|
2019-08-26 17:02:34 -04:00
|
|
|
// Warning! The values in this enum are duplicated in cli/msg.rs
|
|
|
|
// Update carefully!
|
|
|
|
enum MediaType {
|
|
|
|
JavaScript = 0,
|
2019-10-02 10:46:36 -04:00
|
|
|
JSX = 1,
|
|
|
|
TypeScript = 2,
|
|
|
|
TSX = 3,
|
|
|
|
Json = 4,
|
2019-11-14 08:31:39 -05:00
|
|
|
Wasm = 5,
|
|
|
|
Unknown = 6
|
2019-08-26 17:02:34 -04:00
|
|
|
}
|
2019-08-26 10:58:44 -04:00
|
|
|
|
2019-11-13 10:35:56 -05:00
|
|
|
// Warning! The values in this enum are duplicated in cli/msg.rs
|
|
|
|
// Update carefully!
|
|
|
|
enum CompilerRequestType {
|
|
|
|
Compile = 0,
|
|
|
|
Bundle = 1
|
|
|
|
}
|
|
|
|
|
2019-05-20 12:06:57 -04:00
|
|
|
// Startup boilerplate. This is necessary because the compiler has its own
|
|
|
|
// snapshot. (It would be great if we could remove these things or centralize
|
|
|
|
// them somewhere else.)
|
2019-03-26 08:22:07 -04:00
|
|
|
const console = new Console(core.print);
|
2019-05-20 12:06:57 -04:00
|
|
|
window.console = console;
|
|
|
|
window.workerMain = workerMain;
|
2019-11-14 08:31:39 -05:00
|
|
|
function denoMain(compilerType?: string): void {
|
|
|
|
os.start(true, compilerType || "TS");
|
2019-01-28 20:41:28 -05:00
|
|
|
}
|
2019-09-02 17:07:11 -04:00
|
|
|
window["denoMain"] = denoMain;
|
2019-01-28 20:41:28 -05:00
|
|
|
|
2019-05-20 12:06:57 -04:00
|
|
|
const ASSETS = "$asset$";
|
|
|
|
const OUT_DIR = "$deno$";
|
2018-08-22 17:17:26 -04:00
|
|
|
|
2019-05-20 12:06:57 -04:00
|
|
|
/** The format of the work message payload coming from the privileged side */
|
2019-11-13 10:35:56 -05:00
|
|
|
type CompilerRequest = {
|
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;
|
2019-11-13 10:35:56 -05:00
|
|
|
} & (
|
|
|
|
| {
|
|
|
|
type: CompilerRequestType.Compile;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: CompilerRequestType.Bundle;
|
|
|
|
outFile?: string;
|
2019-11-13 13:42:34 -05:00
|
|
|
}
|
|
|
|
);
|
2018-08-22 17:17:26 -04:00
|
|
|
|
2019-06-04 09:03:56 -04:00
|
|
|
interface ConfigureResponse {
|
|
|
|
ignoredOptions?: string[];
|
|
|
|
diagnostics?: ts.Diagnostic[];
|
|
|
|
}
|
|
|
|
|
2019-04-29 10:58:31 -04:00
|
|
|
/** Options that either do nothing in Deno, or would cause undesired behavior
|
|
|
|
* if modified. */
|
2019-09-07 12:27:18 -04:00
|
|
|
const ignoredCompilerOptions: readonly string[] = [
|
2019-04-29 10:58:31 -04:00
|
|
|
"allowSyntheticDefaultImports",
|
|
|
|
"baseUrl",
|
|
|
|
"build",
|
|
|
|
"composite",
|
|
|
|
"declaration",
|
|
|
|
"declarationDir",
|
|
|
|
"declarationMap",
|
|
|
|
"diagnostics",
|
|
|
|
"downlevelIteration",
|
|
|
|
"emitBOM",
|
|
|
|
"emitDeclarationOnly",
|
|
|
|
"esModuleInterop",
|
|
|
|
"extendedDiagnostics",
|
|
|
|
"forceConsistentCasingInFileNames",
|
|
|
|
"help",
|
|
|
|
"importHelpers",
|
|
|
|
"incremental",
|
|
|
|
"inlineSourceMap",
|
|
|
|
"inlineSources",
|
|
|
|
"init",
|
|
|
|
"isolatedModules",
|
|
|
|
"lib",
|
|
|
|
"listEmittedFiles",
|
|
|
|
"listFiles",
|
|
|
|
"mapRoot",
|
|
|
|
"maxNodeModuleJsDepth",
|
|
|
|
"module",
|
|
|
|
"moduleResolution",
|
|
|
|
"newLine",
|
|
|
|
"noEmit",
|
|
|
|
"noEmitHelpers",
|
|
|
|
"noEmitOnError",
|
|
|
|
"noLib",
|
|
|
|
"noResolve",
|
|
|
|
"out",
|
|
|
|
"outDir",
|
|
|
|
"outFile",
|
|
|
|
"paths",
|
|
|
|
"preserveSymlinks",
|
|
|
|
"preserveWatchOutput",
|
|
|
|
"pretty",
|
|
|
|
"rootDir",
|
|
|
|
"rootDirs",
|
|
|
|
"showConfig",
|
|
|
|
"skipDefaultLibCheck",
|
|
|
|
"skipLibCheck",
|
|
|
|
"sourceMap",
|
|
|
|
"sourceRoot",
|
|
|
|
"stripInternal",
|
|
|
|
"target",
|
|
|
|
"traceResolution",
|
|
|
|
"tsBuildInfoFile",
|
|
|
|
"types",
|
|
|
|
"typeRoots",
|
|
|
|
"version",
|
|
|
|
"watch"
|
|
|
|
];
|
|
|
|
|
2019-10-03 07:23:29 -04:00
|
|
|
/** The shape of the SourceFile that comes from the privileged side */
|
|
|
|
interface SourceFileJson {
|
|
|
|
url: string;
|
|
|
|
filename: string;
|
2019-08-26 17:02:34 -04:00
|
|
|
mediaType: MediaType;
|
2019-10-03 07:23:29 -04:00
|
|
|
sourceCode: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** A self registering abstraction of source files. */
|
|
|
|
class SourceFile {
|
|
|
|
extension!: ts.Extension;
|
|
|
|
filename!: string;
|
|
|
|
|
|
|
|
/** An array of tuples which represent the imports for the source file. The
|
|
|
|
* first element is the one that will be requested at compile time, the
|
|
|
|
* second is the one that should be actually resolved. This provides the
|
|
|
|
* feature of type directives for Deno. */
|
|
|
|
importedFiles?: Array<[string, string]>;
|
|
|
|
|
|
|
|
mediaType!: MediaType;
|
|
|
|
processed = false;
|
|
|
|
sourceCode!: string;
|
|
|
|
tsSourceFile?: ts.SourceFile;
|
|
|
|
url!: string;
|
|
|
|
|
|
|
|
constructor(json: SourceFileJson) {
|
|
|
|
if (SourceFile._moduleCache.has(json.url)) {
|
|
|
|
throw new TypeError("SourceFile already exists");
|
|
|
|
}
|
|
|
|
Object.assign(this, json);
|
|
|
|
this.extension = getExtension(this.url, this.mediaType);
|
|
|
|
SourceFile._moduleCache.set(this.url, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Cache the source file to be able to be retrieved by `moduleSpecifier` and
|
|
|
|
* `containingFile`. */
|
2019-11-25 09:33:23 -05:00
|
|
|
cache(moduleSpecifier: string, containingFile?: string): void {
|
|
|
|
containingFile = containingFile || "";
|
2019-10-03 07:23:29 -04:00
|
|
|
let innerCache = SourceFile._specifierCache.get(containingFile);
|
|
|
|
if (!innerCache) {
|
|
|
|
innerCache = new Map();
|
|
|
|
SourceFile._specifierCache.set(containingFile, innerCache);
|
|
|
|
}
|
|
|
|
innerCache.set(moduleSpecifier, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Process the imports for the file and return them. */
|
|
|
|
imports(): Array<[string, string]> {
|
|
|
|
if (this.processed) {
|
|
|
|
throw new Error("SourceFile has already been processed.");
|
|
|
|
}
|
|
|
|
assert(this.sourceCode != null);
|
2019-11-20 11:02:08 -05:00
|
|
|
// we shouldn't process imports for files which contain the nocheck pragma
|
|
|
|
// (like bundles)
|
|
|
|
if (this.sourceCode.match(/\/{2}\s+@ts-nocheck/)) {
|
|
|
|
util.log(`Skipping imports for "${this.filename}"`);
|
|
|
|
return [];
|
|
|
|
}
|
2019-11-13 13:42:34 -05:00
|
|
|
const preProcessedFileInfo = ts.preProcessFile(this.sourceCode, true, true);
|
2019-10-03 07:23:29 -04:00
|
|
|
this.processed = true;
|
|
|
|
const files = (this.importedFiles = [] as Array<[string, string]>);
|
|
|
|
|
|
|
|
function process(references: ts.FileReference[]): void {
|
|
|
|
for (const { fileName } of references) {
|
|
|
|
files.push([fileName, fileName]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
|
|
|
importedFiles,
|
|
|
|
referencedFiles,
|
|
|
|
libReferenceDirectives,
|
|
|
|
typeReferenceDirectives
|
|
|
|
} = preProcessedFileInfo;
|
|
|
|
const typeDirectives = parseTypeDirectives(this.sourceCode);
|
|
|
|
if (typeDirectives) {
|
|
|
|
for (const importedFile of importedFiles) {
|
|
|
|
files.push([
|
|
|
|
importedFile.fileName,
|
|
|
|
getMappedModuleName(importedFile, typeDirectives)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
process(importedFiles);
|
|
|
|
}
|
|
|
|
process(referencedFiles);
|
|
|
|
process(libReferenceDirectives);
|
|
|
|
process(typeReferenceDirectives);
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** A cache of all the source files which have been loaded indexed by the
|
|
|
|
* url. */
|
|
|
|
private static _moduleCache: Map<string, SourceFile> = new Map();
|
|
|
|
|
|
|
|
/** A cache of source files based on module specifiers and containing files
|
|
|
|
* which is used by the TypeScript compiler to resolve the url */
|
|
|
|
private static _specifierCache: Map<
|
|
|
|
string,
|
|
|
|
Map<string, SourceFile>
|
|
|
|
> = new Map();
|
|
|
|
|
|
|
|
/** Retrieve a `SourceFile` based on a `moduleSpecifier` and `containingFile`
|
|
|
|
* or return `undefined` if not preset. */
|
|
|
|
static getUrl(
|
|
|
|
moduleSpecifier: string,
|
|
|
|
containingFile: string
|
|
|
|
): string | undefined {
|
|
|
|
const containingCache = this._specifierCache.get(containingFile);
|
|
|
|
if (containingCache) {
|
|
|
|
const sourceFile = containingCache.get(moduleSpecifier);
|
|
|
|
return sourceFile && sourceFile.url;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Retrieve a `SourceFile` based on a `url` */
|
|
|
|
static get(url: string): SourceFile | undefined {
|
|
|
|
return this._moduleCache.get(url);
|
|
|
|
}
|
2019-05-20 12:06:57 -04:00
|
|
|
}
|
2018-08-24 00:07:01 -04:00
|
|
|
|
2019-06-04 09:03:56 -04:00
|
|
|
interface EmitResult {
|
|
|
|
emitSkipped: boolean;
|
|
|
|
diagnostics?: Diagnostic;
|
|
|
|
}
|
|
|
|
|
2019-10-03 07:23:29 -04:00
|
|
|
/** Ops to Rust to resolve special static assets. */
|
2019-08-30 11:11:33 -04:00
|
|
|
function fetchAsset(name: string): string {
|
|
|
|
return sendSync(dispatch.OP_FETCH_ASSET, { name });
|
|
|
|
}
|
|
|
|
|
2019-09-14 12:05:00 -04:00
|
|
|
/** Ops to Rust to resolve and fetch modules meta data. */
|
|
|
|
function fetchSourceFiles(
|
|
|
|
specifiers: string[],
|
2019-11-25 09:33:23 -05:00
|
|
|
referrer?: string
|
2019-10-03 07:23:29 -04:00
|
|
|
): Promise<SourceFileJson[]> {
|
|
|
|
util.log("compiler::fetchSourceFiles", { specifiers, referrer });
|
|
|
|
return sendAsync(dispatch.OP_FETCH_SOURCE_FILES, {
|
2019-09-14 12:05:00 -04:00
|
|
|
specifiers,
|
2019-08-26 08:50:21 -04:00
|
|
|
referrer
|
|
|
|
});
|
2019-10-03 07:23:29 -04:00
|
|
|
}
|
2019-08-26 08:50:21 -04:00
|
|
|
|
2019-10-03 07:23:29 -04:00
|
|
|
/** Recursively process the imports of modules, generating `SourceFile`s of any
|
|
|
|
* imported files.
|
|
|
|
*
|
|
|
|
* Specifiers are supplied in an array of tupples where the first is the
|
|
|
|
* specifier that will be requested in the code and the second is the specifier
|
|
|
|
* that should be actually resolved. */
|
|
|
|
async function processImports(
|
|
|
|
specifiers: Array<[string, string]>,
|
2019-11-25 09:33:23 -05:00
|
|
|
referrer?: string
|
2019-11-13 10:35:56 -05:00
|
|
|
): Promise<SourceFileJson[]> {
|
2019-10-03 07:23:29 -04:00
|
|
|
if (!specifiers.length) {
|
2019-11-14 15:05:36 -05:00
|
|
|
return [];
|
2019-10-03 07:23:29 -04:00
|
|
|
}
|
|
|
|
const sources = specifiers.map(([, moduleSpecifier]) => moduleSpecifier);
|
|
|
|
const sourceFiles = await fetchSourceFiles(sources, referrer);
|
|
|
|
assert(sourceFiles.length === specifiers.length);
|
|
|
|
for (let i = 0; i < sourceFiles.length; i++) {
|
|
|
|
const sourceFileJson = sourceFiles[i];
|
|
|
|
const sourceFile =
|
|
|
|
SourceFile.get(sourceFileJson.url) || new SourceFile(sourceFileJson);
|
|
|
|
sourceFile.cache(specifiers[i][0], referrer);
|
|
|
|
if (!sourceFile.processed) {
|
|
|
|
await processImports(sourceFile.imports(), sourceFile.url);
|
2019-09-14 12:05:00 -04:00
|
|
|
}
|
2019-10-03 07:23:29 -04:00
|
|
|
}
|
2019-11-13 10:35:56 -05:00
|
|
|
return sourceFiles;
|
2019-05-20 12:06:57 -04:00
|
|
|
}
|
2018-08-24 00:07:01 -04:00
|
|
|
|
2019-06-08 14:42:28 -04:00
|
|
|
/** Ops to rest for caching source map and compiled js */
|
2019-05-20 12:06:57 -04:00
|
|
|
function cache(extension: string, moduleId: string, contents: string): void {
|
2019-10-03 07:23:29 -04:00
|
|
|
util.log("compiler::cache", { extension, moduleId });
|
2019-08-26 08:50:21 -04:00
|
|
|
sendSync(dispatch.OP_CACHE, { extension, moduleId, contents });
|
2018-08-22 17:17:26 -04:00
|
|
|
}
|
|
|
|
|
2019-02-02 18:27:53 -05:00
|
|
|
/** Returns the TypeScript Extension enum for a given media type. */
|
2019-08-26 17:02:34 -04:00
|
|
|
function getExtension(fileName: string, mediaType: MediaType): ts.Extension {
|
2018-10-21 22:14:27 -04:00
|
|
|
switch (mediaType) {
|
2019-08-26 17:02:34 -04:00
|
|
|
case MediaType.JavaScript:
|
2018-10-21 22:14:27 -04:00
|
|
|
return ts.Extension.Js;
|
2019-10-02 10:46:36 -04:00
|
|
|
case MediaType.JSX:
|
|
|
|
return ts.Extension.Jsx;
|
2019-08-26 17:02:34 -04:00
|
|
|
case MediaType.TypeScript:
|
2018-10-21 22:14:27 -04:00
|
|
|
return fileName.endsWith(".d.ts") ? ts.Extension.Dts : ts.Extension.Ts;
|
2019-10-02 10:46:36 -04:00
|
|
|
case MediaType.TSX:
|
|
|
|
return ts.Extension.Tsx;
|
2019-08-26 17:02:34 -04:00
|
|
|
case MediaType.Json:
|
2018-10-21 22:14:27 -04:00
|
|
|
return ts.Extension.Json;
|
2019-11-14 08:31:39 -05:00
|
|
|
case MediaType.Wasm:
|
|
|
|
// Custom marker for Wasm type.
|
|
|
|
return ts.Extension.Js;
|
2019-08-26 17:02:34 -04:00
|
|
|
case MediaType.Unknown:
|
2018-10-21 22:14:27 -04:00
|
|
|
default:
|
2019-02-02 18:27:53 -05:00
|
|
|
throw TypeError("Cannot resolve extension.");
|
2018-10-21 22:14:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-20 12:06:57 -04:00
|
|
|
class Host implements ts.CompilerHost {
|
2018-10-30 23:50:40 -04:00
|
|
|
private readonly _options: ts.CompilerOptions = {
|
2018-08-22 17:17:26 -04:00
|
|
|
allowJs: true,
|
2019-02-15 11:05:29 -05:00
|
|
|
allowNonTsExtensions: true,
|
2019-11-14 15:05:36 -05:00
|
|
|
// TODO(#3324) Enable strict mode for user code.
|
|
|
|
// strict: true,
|
2019-05-20 12:06:57 -04:00
|
|
|
checkJs: false,
|
2019-01-14 00:18:42 -05:00
|
|
|
esModuleInterop: true,
|
2019-01-09 12:59:46 -05:00
|
|
|
module: ts.ModuleKind.ESNext,
|
2019-05-20 12:06:57 -04:00
|
|
|
outDir: OUT_DIR,
|
2018-10-30 23:50:40 -04:00
|
|
|
resolveJsonModule: true,
|
2018-10-28 19:41:10 -04:00
|
|
|
sourceMap: true,
|
2018-08-22 17:17:26 -04:00
|
|
|
stripComments: true,
|
2019-10-02 10:46:36 -04:00
|
|
|
target: ts.ScriptTarget.ESNext,
|
|
|
|
jsx: ts.JsxEmit.React
|
2018-08-22 17:17:26 -04:00
|
|
|
};
|
2019-01-06 14:17:13 -05:00
|
|
|
|
2019-10-03 07:23:29 -04:00
|
|
|
private _getAsset(filename: string): SourceFile {
|
|
|
|
const sourceFile = SourceFile.get(filename);
|
|
|
|
if (sourceFile) {
|
|
|
|
return sourceFile;
|
2019-09-14 12:05:00 -04:00
|
|
|
}
|
2019-10-03 07:23:29 -04:00
|
|
|
const url = filename.split("/").pop()!;
|
|
|
|
const assetName = url.includes(".") ? url : `${url}.d.ts`;
|
2019-09-14 12:05:00 -04:00
|
|
|
const sourceCode = fetchAsset(assetName);
|
2019-10-03 07:23:29 -04:00
|
|
|
return new SourceFile({
|
|
|
|
url,
|
|
|
|
filename,
|
2019-09-14 12:05:00 -04:00
|
|
|
mediaType: MediaType.TypeScript,
|
|
|
|
sourceCode
|
2019-10-03 07:23:29 -04:00
|
|
|
});
|
2019-06-08 14:42:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Deno specific APIs */
|
|
|
|
|
|
|
|
/** Provides the `ts.HostCompiler` interface for Deno.
|
|
|
|
*
|
2019-11-13 10:35:56 -05:00
|
|
|
* @param _rootNames A set of modules that are the ones that should be
|
|
|
|
* instantiated first. Used when generating a bundle.
|
2019-06-08 14:42:28 -04:00
|
|
|
* @param _bundle Set to a string value to configure the host to write out a
|
|
|
|
* bundle instead of caching individual files.
|
|
|
|
*/
|
2019-11-13 10:35:56 -05:00
|
|
|
constructor(
|
|
|
|
private _requestType: CompilerRequestType,
|
|
|
|
private _rootNames: string[],
|
|
|
|
private _outFile?: string
|
|
|
|
) {
|
|
|
|
if (this._requestType === CompilerRequestType.Bundle) {
|
2019-06-08 14:42:28 -04:00
|
|
|
// options we need to change when we are generating a bundle
|
|
|
|
const bundlerOptions: ts.CompilerOptions = {
|
|
|
|
module: ts.ModuleKind.AMD,
|
|
|
|
outDir: undefined,
|
|
|
|
outFile: `${OUT_DIR}/bundle.js`,
|
2019-11-13 10:35:56 -05:00
|
|
|
// disabled until we have effective way to modify source maps
|
2019-06-08 14:42:28 -04:00
|
|
|
sourceMap: false
|
|
|
|
};
|
|
|
|
Object.assign(this._options, bundlerOptions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-29 10:58:31 -04:00
|
|
|
/** Take a configuration string, parse it, and use it to merge with the
|
|
|
|
* compiler's configuration options. The method returns an array of compiler
|
2019-10-03 07:23:29 -04:00
|
|
|
* options which were ignored, or `undefined`. */
|
2019-06-04 09:03:56 -04:00
|
|
|
configure(path: string, configurationText: string): ConfigureResponse {
|
2019-10-03 07:23:29 -04:00
|
|
|
util.log("compiler::host.configure", path);
|
2019-05-20 12:06:57 -04:00
|
|
|
const { config, error } = ts.parseConfigFileTextToJson(
|
2019-04-29 10:58:31 -04:00
|
|
|
path,
|
|
|
|
configurationText
|
|
|
|
);
|
|
|
|
if (error) {
|
2019-06-04 09:03:56 -04:00
|
|
|
return { diagnostics: [error] };
|
2019-04-29 10:58:31 -04:00
|
|
|
}
|
2019-05-20 12:06:57 -04:00
|
|
|
const { options, errors } = ts.convertCompilerOptionsFromJson(
|
2019-04-29 10:58:31 -04:00
|
|
|
config.compilerOptions,
|
|
|
|
cwd()
|
|
|
|
);
|
|
|
|
const ignoredOptions: string[] = [];
|
|
|
|
for (const key of Object.keys(options)) {
|
|
|
|
if (
|
|
|
|
ignoredCompilerOptions.includes(key) &&
|
|
|
|
(!(key in this._options) || options[key] !== this._options[key])
|
|
|
|
) {
|
|
|
|
ignoredOptions.push(key);
|
|
|
|
delete options[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Object.assign(this._options, options);
|
2019-06-04 09:03:56 -04:00
|
|
|
return {
|
|
|
|
ignoredOptions: ignoredOptions.length ? ignoredOptions : undefined,
|
|
|
|
diagnostics: errors.length ? errors : undefined
|
|
|
|
};
|
2019-04-29 10:58:31 -04:00
|
|
|
}
|
|
|
|
|
2019-06-08 14:42:28 -04:00
|
|
|
/* TypeScript CompilerHost APIs */
|
|
|
|
|
2019-10-03 07:23:29 -04:00
|
|
|
fileExists(_fileName: string): boolean {
|
2019-06-08 14:42:28 -04:00
|
|
|
return notImplemented();
|
|
|
|
}
|
|
|
|
|
|
|
|
getCanonicalFileName(fileName: string): string {
|
|
|
|
return fileName;
|
|
|
|
}
|
|
|
|
|
2018-08-22 17:17:26 -04:00
|
|
|
getCompilationSettings(): ts.CompilerOptions {
|
2019-10-03 07:23:29 -04:00
|
|
|
util.log("compiler::host.getCompilationSettings()");
|
2018-08-22 17:17:26 -04:00
|
|
|
return this._options;
|
|
|
|
}
|
|
|
|
|
2019-06-08 14:42:28 -04:00
|
|
|
getCurrentDirectory(): string {
|
|
|
|
return "";
|
2018-08-22 17:17:26 -04:00
|
|
|
}
|
|
|
|
|
2019-06-08 14:42:28 -04:00
|
|
|
getDefaultLibFileName(_options: ts.CompilerOptions): string {
|
|
|
|
return ASSETS + "/lib.deno_runtime.d.ts";
|
|
|
|
}
|
|
|
|
|
|
|
|
getNewLine(): string {
|
|
|
|
return "\n";
|
2019-05-20 12:06:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
getSourceFile(
|
|
|
|
fileName: string,
|
|
|
|
languageVersion: ts.ScriptTarget,
|
|
|
|
onError?: (message: string) => void,
|
|
|
|
shouldCreateNewSourceFile?: boolean
|
|
|
|
): ts.SourceFile | undefined {
|
2019-10-03 07:23:29 -04:00
|
|
|
util.log("compiler::host.getSourceFile", fileName);
|
|
|
|
try {
|
|
|
|
assert(!shouldCreateNewSourceFile);
|
|
|
|
const sourceFile = fileName.startsWith(ASSETS)
|
|
|
|
? this._getAsset(fileName)
|
|
|
|
: SourceFile.get(fileName);
|
|
|
|
assert(sourceFile != null);
|
2019-11-13 13:42:34 -05:00
|
|
|
if (!sourceFile.tsSourceFile) {
|
|
|
|
sourceFile.tsSourceFile = ts.createSourceFile(
|
2019-10-03 07:23:29 -04:00
|
|
|
fileName,
|
2019-11-13 13:42:34 -05:00
|
|
|
sourceFile.sourceCode,
|
2019-10-03 07:23:29 -04:00
|
|
|
languageVersion
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return sourceFile!.tsSourceFile;
|
|
|
|
} catch (e) {
|
|
|
|
if (onError) {
|
|
|
|
onError(String(e));
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
2019-05-20 12:06:57 -04:00
|
|
|
return undefined;
|
2018-08-22 17:17:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 14:42:28 -04:00
|
|
|
readFile(_fileName: string): string | undefined {
|
|
|
|
return notImplemented();
|
2018-08-22 17:17:26 -04:00
|
|
|
}
|
|
|
|
|
2019-05-20 12:06:57 -04:00
|
|
|
resolveModuleNames(
|
|
|
|
moduleNames: string[],
|
|
|
|
containingFile: string
|
|
|
|
): Array<ts.ResolvedModuleFull | undefined> {
|
2019-10-03 07:23:29 -04:00
|
|
|
util.log("compiler::host.resolveModuleNames", {
|
|
|
|
moduleNames,
|
|
|
|
containingFile
|
|
|
|
});
|
|
|
|
return moduleNames.map(specifier => {
|
|
|
|
const url = SourceFile.getUrl(specifier, containingFile);
|
|
|
|
const sourceFile = specifier.startsWith(ASSETS)
|
|
|
|
? this._getAsset(specifier)
|
|
|
|
: url
|
|
|
|
? SourceFile.get(url)
|
2019-08-22 12:05:01 -04:00
|
|
|
: undefined;
|
2019-10-03 07:23:29 -04:00
|
|
|
if (!sourceFile) {
|
|
|
|
return undefined;
|
2019-09-14 12:05:00 -04:00
|
|
|
}
|
2019-10-03 07:23:29 -04:00
|
|
|
return {
|
|
|
|
resolvedFileName: sourceFile.url,
|
|
|
|
isExternalLibraryImport: specifier.startsWith(ASSETS),
|
|
|
|
extension: sourceFile.extension
|
|
|
|
};
|
|
|
|
});
|
2018-08-22 17:17:26 -04:00
|
|
|
}
|
|
|
|
|
2019-06-08 14:42:28 -04:00
|
|
|
useCaseSensitiveFileNames(): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
writeFile(
|
|
|
|
fileName: string,
|
|
|
|
data: string,
|
2019-10-03 07:23:29 -04:00
|
|
|
_writeByteOrderMark: boolean,
|
2019-06-08 14:42:28 -04:00
|
|
|
onError?: (message: string) => void,
|
2019-09-07 12:27:18 -04:00
|
|
|
sourceFiles?: readonly ts.SourceFile[]
|
2019-06-08 14:42:28 -04:00
|
|
|
): void {
|
2019-10-03 07:23:29 -04:00
|
|
|
util.log("compiler::host.writeFile", fileName);
|
2019-06-08 14:42:28 -04:00
|
|
|
try {
|
2019-11-13 10:35:56 -05:00
|
|
|
assert(sourceFiles != null);
|
|
|
|
if (this._requestType === CompilerRequestType.Bundle) {
|
2019-11-20 11:02:08 -05:00
|
|
|
emitBundle(this._rootNames, this._outFile, data, sourceFiles);
|
2019-06-08 14:42:28 -04:00
|
|
|
} else {
|
2019-11-13 10:35:56 -05:00
|
|
|
assert(sourceFiles.length == 1);
|
2019-11-13 13:42:34 -05:00
|
|
|
const url = sourceFiles[0].fileName;
|
2019-10-03 07:23:29 -04:00
|
|
|
const sourceFile = SourceFile.get(url);
|
2019-07-31 13:16:03 -04:00
|
|
|
|
2019-10-03 07:23:29 -04:00
|
|
|
if (sourceFile) {
|
2019-07-31 13:16:03 -04:00
|
|
|
// NOTE: If it's a `.json` file we don't want to write it to disk.
|
|
|
|
// JSON files are loaded and used by TS compiler to check types, but we don't want
|
|
|
|
// to emit them to disk because output file is the same as input file.
|
2019-10-03 07:23:29 -04:00
|
|
|
if (sourceFile.extension === ts.Extension.Json) {
|
2019-07-31 13:16:03 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: JavaScript files are only emitted to disk if `checkJs` option in on
|
2019-10-03 07:23:29 -04:00
|
|
|
if (
|
|
|
|
sourceFile.extension === ts.Extension.Js &&
|
|
|
|
!this._options.checkJs
|
|
|
|
) {
|
2019-07-31 13:16:03 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-06-08 14:42:28 -04:00
|
|
|
|
|
|
|
if (fileName.endsWith(".map")) {
|
|
|
|
// Source Map
|
2019-10-03 07:23:29 -04:00
|
|
|
cache(".map", url, data);
|
2019-06-08 14:42:28 -04:00
|
|
|
} else if (fileName.endsWith(".js") || fileName.endsWith(".json")) {
|
|
|
|
// Compiled JavaScript
|
2019-10-03 07:23:29 -04:00
|
|
|
cache(".js", url, data);
|
2019-06-08 14:42:28 -04:00
|
|
|
} else {
|
|
|
|
assert(false, "Trying to cache unhandled file type " + fileName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
if (onError) {
|
|
|
|
onError(String(e));
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
2019-02-02 18:27:53 -05:00
|
|
|
}
|
2018-08-22 17:17:26 -04:00
|
|
|
}
|
2019-01-28 20:41:28 -05:00
|
|
|
}
|
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
|
2019-04-21 16:40:10 -04:00
|
|
|
window.compilerMain = function compilerMain(): void {
|
2019-01-28 20:41:28 -05:00
|
|
|
// workerMain should have already been called since a compiler is a worker.
|
2019-11-13 10:35:56 -05:00
|
|
|
window.onmessage = async ({
|
|
|
|
data: request
|
|
|
|
}: {
|
|
|
|
data: CompilerRequest;
|
|
|
|
}): Promise<void> => {
|
|
|
|
const { rootNames, configPath, config } = request;
|
|
|
|
util.log(">>> compile start", {
|
|
|
|
rootNames,
|
|
|
|
type: CompilerRequestType[request.type]
|
|
|
|
});
|
2019-10-03 07:23:29 -04:00
|
|
|
|
|
|
|
// 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.
|
2019-11-13 13:42:34 -05:00
|
|
|
const resolvedRootModules = (
|
|
|
|
await processImports(rootNames.map(rootName => [rootName, rootName]))
|
|
|
|
).map(info => info.url);
|
2019-11-13 10:35:56 -05:00
|
|
|
|
|
|
|
const host = new Host(
|
|
|
|
request.type,
|
|
|
|
resolvedRootModules,
|
|
|
|
request.type === CompilerRequestType.Bundle ? request.outFile : undefined
|
|
|
|
);
|
2019-06-04 09:03:56 -04:00
|
|
|
let emitSkipped = true;
|
|
|
|
let diagnostics: 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);
|
|
|
|
const ignoredOptions = configResult.ignoredOptions;
|
|
|
|
diagnostics = configResult.diagnostics;
|
2019-05-20 12:06:57 -04:00
|
|
|
if (ignoredOptions) {
|
|
|
|
console.warn(
|
|
|
|
yellow(`Unsupported compiler options in "${configPath}"\n`) +
|
|
|
|
cyan(` The following options were ignored:\n`) +
|
|
|
|
` ${ignoredOptions
|
|
|
|
.map((value): string => bold(value))
|
|
|
|
.join(", ")}`
|
|
|
|
);
|
|
|
|
}
|
2019-04-04 05:33:32 -04:00
|
|
|
}
|
2018-08-22 17:17:26 -04:00
|
|
|
|
2019-06-04 09:03:56 -04:00
|
|
|
// 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();
|
|
|
|
const program = ts.createProgram(rootNames, options, host);
|
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
diagnostics = ts
|
|
|
|
.getPreEmitDiagnostics(program)
|
|
|
|
.filter(({ code }): boolean => {
|
2019-10-27 09:04:42 -04:00
|
|
|
// TS1103: 'for-await-of' statement is only allowed within an async
|
|
|
|
// function or async generator.
|
|
|
|
if (code === 1103) return false;
|
2019-09-30 12:38:23 -04:00
|
|
|
// TS1308: 'await' expression is only allowed within an async
|
|
|
|
// function.
|
|
|
|
if (code === 1308) return false;
|
2019-06-04 09:03:56 -04:00
|
|
|
// TS2691: An import path cannot end with a '.ts' extension. Consider
|
|
|
|
// importing 'bad-module' instead.
|
|
|
|
if (code === 2691) return false;
|
|
|
|
// TS5009: Cannot find the common subdirectory path for the input files.
|
|
|
|
if (code === 5009) return false;
|
|
|
|
// TS5055: Cannot write file
|
|
|
|
// 'http://localhost:4545/tests/subdir/mt_application_x_javascript.j4.js'
|
|
|
|
// because it would overwrite input file.
|
|
|
|
if (code === 5055) return false;
|
|
|
|
// TypeScript is overly opinionated that only CommonJS modules kinds can
|
|
|
|
// support JSON imports. Allegedly this was fixed in
|
|
|
|
// Microsoft/TypeScript#26825 but that doesn't seem to be working here,
|
|
|
|
// so we will ignore complaints about this compiler setting.
|
|
|
|
if (code === 5070) return false;
|
|
|
|
return true;
|
2019-11-13 13:42:34 -05:00
|
|
|
});
|
2019-06-04 09:03:56 -04:00
|
|
|
|
|
|
|
// We will only proceed with the emit if there are no diagnostics.
|
|
|
|
if (diagnostics && diagnostics.length === 0) {
|
2019-11-13 10:35:56 -05:00
|
|
|
if (request.type === CompilerRequestType.Bundle) {
|
|
|
|
// warning so it goes to stderr instead of stdout
|
|
|
|
console.warn(`Bundling "${resolvedRootModules.join(`", "`)}"`);
|
2019-06-08 14:42:28 -04:00
|
|
|
}
|
2019-11-20 11:02:08 -05:00
|
|
|
if (request.type === CompilerRequestType.Bundle) {
|
|
|
|
setRootExports(program, resolvedRootModules);
|
|
|
|
}
|
2019-06-04 09:03:56 -04:00
|
|
|
const emitResult = program.emit();
|
|
|
|
emitSkipped = emitResult.emitSkipped;
|
|
|
|
// emitResult.diagnostics is `readonly` in TS3.5+ and can't be assigned
|
|
|
|
// without casting.
|
|
|
|
diagnostics = emitResult.diagnostics as ts.Diagnostic[];
|
2019-06-03 12:58:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-04 09:03:56 -04:00
|
|
|
const result: EmitResult = {
|
|
|
|
emitSkipped,
|
|
|
|
diagnostics: diagnostics.length
|
|
|
|
? fromTypeScriptDiagnostic(diagnostics)
|
|
|
|
: undefined
|
|
|
|
};
|
2019-04-29 10:58:31 -04:00
|
|
|
|
2019-06-04 09:03:56 -04:00
|
|
|
postMessage(result);
|
2019-04-29 10:58:31 -04:00
|
|
|
|
2019-11-13 10:35:56 -05:00
|
|
|
util.log("<<< compile end", {
|
|
|
|
rootNames,
|
|
|
|
type: CompilerRequestType[request.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
|
|
|
|
|
|
|
function base64ToUint8Array(data: string): Uint8Array {
|
|
|
|
const binString = window.atob(data);
|
|
|
|
const size = binString.length;
|
|
|
|
const bytes = new Uint8Array(size);
|
|
|
|
for (let i = 0; i < size; i++) {
|
|
|
|
bytes[i] = binString.charCodeAt(i);
|
|
|
|
}
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.wasmCompilerMain = function wasmCompilerMain(): void {
|
|
|
|
// workerMain should have already been called since a compiler is a worker.
|
|
|
|
window.onmessage = async ({
|
|
|
|
data: binary
|
|
|
|
}: {
|
|
|
|
data: string;
|
|
|
|
}): Promise<void> => {
|
|
|
|
const buffer = base64ToUint8Array(binary);
|
|
|
|
// @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))
|
|
|
|
);
|
|
|
|
|
|
|
|
postMessage({
|
|
|
|
importList,
|
|
|
|
exportList
|
|
|
|
});
|
|
|
|
|
|
|
|
util.log("<<< WASM compile end");
|
|
|
|
|
|
|
|
// The compiler isolate exits after a single message.
|
|
|
|
workerClose();
|
|
|
|
};
|
|
|
|
};
|