2019-01-01 19:58:40 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-08-22 17:17:26 -04:00
|
|
|
import * as ts from "typescript";
|
2019-03-30 14:45:36 -04:00
|
|
|
import * as msg from "gen/cli/msg_generated";
|
2019-02-19 21:42:19 -05:00
|
|
|
import { window } from "./window";
|
2018-08-22 17:17:26 -04:00
|
|
|
import { assetSourceCode } from "./assets";
|
2019-04-29 10:58:31 -04:00
|
|
|
import { bold, cyan, yellow } from "./colors";
|
2019-01-28 20:41:28 -05:00
|
|
|
import { Console } from "./console";
|
2019-03-26 08:22:07 -04:00
|
|
|
import { core } from "./core";
|
2019-04-29 10:58:31 -04:00
|
|
|
import { cwd } from "./dir";
|
|
|
|
import { sendSync } from "./dispatch";
|
|
|
|
import * as flatbuffers from "./flatbuffers";
|
2018-08-22 17:17:26 -04:00
|
|
|
import * as os from "./os";
|
2019-01-28 20:41:28 -05:00
|
|
|
import { TextDecoder, TextEncoder } from "./text_encoding";
|
2019-02-02 18:27:53 -05:00
|
|
|
import { clearTimer, setTimeout } from "./timers";
|
2019-01-28 20:41:28 -05:00
|
|
|
import { postMessage, workerClose, workerMain } from "./workers";
|
2018-08-22 17:17:26 -04:00
|
|
|
import { assert, log, notImplemented } from "./util";
|
|
|
|
|
|
|
|
const EOL = "\n";
|
|
|
|
const ASSETS = "$asset$";
|
2019-02-02 18:27:53 -05:00
|
|
|
const LIB_RUNTIME = `${ASSETS}/lib.deno_runtime.d.ts`;
|
2018-08-22 17:17:26 -04:00
|
|
|
|
2019-02-02 18:27:53 -05:00
|
|
|
// An instance of console
|
2019-03-26 08:22:07 -04:00
|
|
|
const console = new Console(core.print);
|
2019-01-28 20:41:28 -05:00
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** The location that a module is being loaded from. This could be a directory,
|
2018-08-28 00:27:17 -04:00
|
|
|
* like `.`, or it could be a module specifier like
|
|
|
|
* `http://gist.github.com/somefile.ts`
|
|
|
|
*/
|
2019-01-28 20:41:28 -05:00
|
|
|
type ContainingFile = string;
|
2018-10-14 16:29:50 -04:00
|
|
|
/** The internal local filename of a compiled module. It will often be something
|
2018-08-28 00:27:17 -04:00
|
|
|
* like `/home/ry/.deno/gen/f7b4605dfbc4d3bb356e98fda6ceb1481e4a8df5.js`
|
|
|
|
*/
|
2018-08-22 17:17:26 -04:00
|
|
|
type ModuleFileName = string;
|
2018-10-14 16:29:50 -04:00
|
|
|
/** The original resolved resource name.
|
2018-09-02 18:50:46 -04:00
|
|
|
* Path to cached module file or URL from which dependency was retrieved
|
|
|
|
*/
|
|
|
|
type ModuleId = string;
|
2018-10-14 16:29:50 -04:00
|
|
|
/** The external name of a module - could be a URL or could be a relative path.
|
2018-08-28 00:27:17 -04:00
|
|
|
* Examples `http://gist.github.com/somefile.ts` or `./somefile.ts`
|
|
|
|
*/
|
2019-01-28 20:41:28 -05:00
|
|
|
type ModuleSpecifier = string;
|
2018-10-14 16:29:50 -04:00
|
|
|
/** The compiled source code which is cached in `.deno/gen/` */
|
2018-08-22 17:17:26 -04:00
|
|
|
type OutputCode = string;
|
2018-10-14 16:29:50 -04:00
|
|
|
/** The original source code */
|
2018-09-07 15:25:25 -04:00
|
|
|
type SourceCode = string;
|
2018-10-28 19:41:10 -04:00
|
|
|
/** The output source map */
|
|
|
|
type SourceMap = string;
|
2018-08-22 17:17:26 -04:00
|
|
|
|
2019-02-02 18:27:53 -05:00
|
|
|
/** The format of the work message payload coming from the privileged side */
|
2019-01-28 20:41:28 -05:00
|
|
|
interface CompilerLookup {
|
|
|
|
specifier: ModuleSpecifier;
|
|
|
|
referrer: ContainingFile;
|
2019-04-04 05:33:32 -04:00
|
|
|
cmdId: number;
|
2019-01-28 20:41:28 -05:00
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Abstraction of the APIs required from the `os` module so they can be
|
2018-08-22 17:17:26 -04:00
|
|
|
* easily mocked.
|
|
|
|
*/
|
2019-01-28 20:41:28 -05:00
|
|
|
interface Os {
|
2019-02-18 10:42:15 -05:00
|
|
|
fetchModuleMetaData: typeof os.fetchModuleMetaData;
|
2018-08-22 17:17:26 -04:00
|
|
|
exit: typeof os.exit;
|
2019-04-29 10:58:31 -04:00
|
|
|
noColor: typeof os.noColor;
|
2018-08-22 17:17:26 -04:00
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Abstraction of the APIs required from the `typescript` module so they can
|
2018-08-22 17:17:26 -04:00
|
|
|
* be easily mocked.
|
|
|
|
*/
|
2019-01-28 20:41:28 -05:00
|
|
|
interface Ts {
|
2019-04-29 10:58:31 -04:00
|
|
|
convertCompilerOptionsFromJson: typeof ts.convertCompilerOptionsFromJson;
|
2018-08-22 17:17:26 -04:00
|
|
|
createLanguageService: typeof ts.createLanguageService;
|
|
|
|
formatDiagnosticsWithColorAndContext: typeof ts.formatDiagnosticsWithColorAndContext;
|
2019-02-10 18:19:31 -05:00
|
|
|
formatDiagnostics: typeof ts.formatDiagnostics;
|
2019-04-29 10:58:31 -04:00
|
|
|
parseConfigFileTextToJson: typeof ts.parseConfigFileTextToJson;
|
2018-08-22 17:17:26 -04:00
|
|
|
}
|
|
|
|
|
2019-04-29 10:58:31 -04:00
|
|
|
/** Options that either do nothing in Deno, or would cause undesired behavior
|
|
|
|
* if modified. */
|
|
|
|
const ignoredCompilerOptions: ReadonlyArray<string> = [
|
|
|
|
"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"
|
|
|
|
];
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** A simple object structure for caching resolved modules and their contents.
|
2018-08-22 17:17:26 -04:00
|
|
|
*
|
|
|
|
* Named `ModuleMetaData` to clarify it is just a representation of meta data of
|
|
|
|
* the module, not the actual module instance.
|
|
|
|
*/
|
2019-01-28 20:41:28 -05:00
|
|
|
class ModuleMetaData implements ts.IScriptSnapshot {
|
2018-08-22 17:17:26 -04:00
|
|
|
public scriptVersion = "";
|
|
|
|
|
|
|
|
constructor(
|
2018-09-02 18:50:46 -04:00
|
|
|
public readonly moduleId: ModuleId,
|
2018-08-28 00:27:17 -04:00
|
|
|
public readonly fileName: ModuleFileName,
|
2019-01-28 20:41:28 -05:00
|
|
|
public readonly mediaType: msg.MediaType,
|
2018-09-07 15:25:25 -04:00
|
|
|
public readonly sourceCode: SourceCode = "",
|
2018-10-28 19:41:10 -04:00
|
|
|
public outputCode: OutputCode = "",
|
2018-12-06 23:05:36 -05:00
|
|
|
public sourceMap: SourceMap = ""
|
2018-08-22 17:17:26 -04:00
|
|
|
) {
|
|
|
|
if (outputCode !== "" || fileName.endsWith(".d.ts")) {
|
|
|
|
this.scriptVersion = "1";
|
|
|
|
}
|
|
|
|
}
|
2018-08-24 00:07:01 -04:00
|
|
|
|
2019-02-02 18:27:53 -05:00
|
|
|
/** TypeScript IScriptSnapshot Interface */
|
|
|
|
|
2018-08-24 00:07:01 -04:00
|
|
|
public getText(start: number, end: number): string {
|
2019-02-02 18:27:53 -05:00
|
|
|
return start === 0 && end === this.sourceCode.length
|
|
|
|
? this.sourceCode
|
|
|
|
: this.sourceCode.substring(start, end);
|
2018-08-24 00:07:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public getLength(): number {
|
|
|
|
return this.sourceCode.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getChangeRange(): undefined {
|
|
|
|
// Required `IScriptSnapshot` API, but not implemented/needed in deno
|
|
|
|
return undefined;
|
|
|
|
}
|
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. */
|
2018-10-21 22:14:27 -04:00
|
|
|
function getExtension(
|
|
|
|
fileName: ModuleFileName,
|
2019-01-28 20:41:28 -05:00
|
|
|
mediaType: msg.MediaType
|
2019-02-02 18:27:53 -05:00
|
|
|
): ts.Extension {
|
2018-10-21 22:14:27 -04:00
|
|
|
switch (mediaType) {
|
2019-01-28 20:41:28 -05:00
|
|
|
case msg.MediaType.JavaScript:
|
2018-10-21 22:14:27 -04:00
|
|
|
return ts.Extension.Js;
|
2019-01-28 20:41:28 -05:00
|
|
|
case msg.MediaType.TypeScript:
|
2018-10-21 22:14:27 -04:00
|
|
|
return fileName.endsWith(".d.ts") ? ts.Extension.Dts : ts.Extension.Ts;
|
2019-01-28 20:41:28 -05:00
|
|
|
case msg.MediaType.Json:
|
2018-10-21 22:14:27 -04:00
|
|
|
return ts.Extension.Json;
|
2019-01-28 20:41:28 -05:00
|
|
|
case msg.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 23:50:40 -04:00
|
|
|
/** Generate output code for a provided JSON string along with its source. */
|
2019-01-28 20:41:28 -05:00
|
|
|
function jsonEsmTemplate(
|
2018-10-30 23:50:40 -04:00
|
|
|
jsonString: string,
|
|
|
|
sourceFileName: string
|
|
|
|
): OutputCode {
|
2019-02-02 18:27:53 -05:00
|
|
|
return (
|
|
|
|
`const _json = JSON.parse(\`${jsonString}\`);\n` +
|
|
|
|
`export default _json;\n` +
|
|
|
|
`//# sourceURL=${sourceFileName}\n`
|
|
|
|
);
|
2018-10-30 23:50:40 -04:00
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** A singleton class that combines the TypeScript Language Service host API
|
2018-08-22 17:17:26 -04:00
|
|
|
* with Deno specific APIs to provide an interface for compiling and running
|
|
|
|
* TypeScript and JavaScript modules.
|
|
|
|
*/
|
2019-01-28 20:41:28 -05:00
|
|
|
class Compiler implements ts.LanguageServiceHost, ts.FormatDiagnosticsHost {
|
2018-08-22 17:17:26 -04:00
|
|
|
// Modules are usually referenced by their ModuleSpecifier and ContainingFile,
|
|
|
|
// and keeping a map of the resolved module file name allows more efficient
|
|
|
|
// future resolution
|
|
|
|
private readonly _fileNamesMap = new Map<
|
|
|
|
ContainingFile,
|
|
|
|
Map<ModuleSpecifier, ModuleFileName>
|
|
|
|
>();
|
|
|
|
// A reference to the log utility, so it can be monkey patched during testing
|
|
|
|
private _log = log;
|
|
|
|
// A map of module file names to module meta data
|
|
|
|
private readonly _moduleMetaDataMap = new Map<
|
|
|
|
ModuleFileName,
|
|
|
|
ModuleMetaData
|
|
|
|
>();
|
|
|
|
// TODO ideally this are not static and can be influenced by command line
|
|
|
|
// arguments
|
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,
|
2018-10-24 15:26:21 -04:00
|
|
|
checkJs: true,
|
2019-01-14 00:18:42 -05:00
|
|
|
esModuleInterop: true,
|
2019-01-09 12:59:46 -05:00
|
|
|
module: ts.ModuleKind.ESNext,
|
2018-08-22 17:17:26 -04:00
|
|
|
outDir: "$deno$",
|
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,
|
|
|
|
target: ts.ScriptTarget.ESNext
|
|
|
|
};
|
|
|
|
// A reference to the `./os.ts` module, so it can be monkey patched during
|
|
|
|
// testing
|
|
|
|
private _os: Os = os;
|
2019-01-06 14:17:13 -05:00
|
|
|
// Used to contain the script file we are currently running
|
2018-08-22 17:17:26 -04:00
|
|
|
private _scriptFileNames: string[] = [];
|
|
|
|
// A reference to the TypeScript LanguageService instance so it can be
|
|
|
|
// monkey patched during testing
|
|
|
|
private _service: ts.LanguageService;
|
|
|
|
// A reference to `typescript` module so it can be monkey patched during
|
|
|
|
// testing
|
|
|
|
private _ts: Ts = ts;
|
2019-01-13 23:44:11 -05:00
|
|
|
|
2019-04-01 15:09:59 -04:00
|
|
|
private readonly _assetsSourceCode: { [key: string]: string };
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** The TypeScript language service often refers to the resolved fileName of
|
2018-08-22 17:17:26 -04:00
|
|
|
* a module, this is a shortcut to avoid unnecessary module resolution logic
|
|
|
|
* for modules that may have been initially resolved by a `moduleSpecifier`
|
|
|
|
* and `containingFile`. Also, `resolveModule()` throws when the module
|
|
|
|
* cannot be resolved, which isn't always valid when dealing with the
|
|
|
|
* TypeScript compiler, but the TypeScript compiler shouldn't be asking about
|
|
|
|
* external modules that we haven't told it about yet.
|
|
|
|
*/
|
2019-01-13 23:44:11 -05:00
|
|
|
private _getModuleMetaData(
|
|
|
|
fileName: ModuleFileName
|
|
|
|
): ModuleMetaData | undefined {
|
2019-02-02 18:27:53 -05:00
|
|
|
return (
|
|
|
|
this._moduleMetaDataMap.get(fileName) ||
|
|
|
|
(fileName.startsWith(ASSETS)
|
|
|
|
? this._resolveModule(fileName, "")
|
|
|
|
: undefined)
|
|
|
|
);
|
2018-08-22 17:17:26 -04:00
|
|
|
}
|
|
|
|
|
2019-04-29 10:58:31 -04:00
|
|
|
/** Log TypeScript diagnostics to the console and exit */
|
|
|
|
private _logDiagnostics(diagnostics: ts.Diagnostic[]): never {
|
|
|
|
const errMsg = this._os.noColor
|
|
|
|
? this._ts.formatDiagnostics(diagnostics, this)
|
|
|
|
: this._ts.formatDiagnosticsWithColorAndContext(diagnostics, this);
|
|
|
|
|
|
|
|
console.log(errMsg);
|
|
|
|
// TODO The compiler isolate shouldn't exit. Errors should be forwarded to
|
|
|
|
// to the caller and the caller exit.
|
|
|
|
return this._os.exit(1);
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Given a `moduleSpecifier` and `containingFile` retrieve the cached
|
2018-09-06 12:45:09 -04:00
|
|
|
* `fileName` for a given module. If the module has yet to be resolved
|
|
|
|
* this will return `undefined`.
|
|
|
|
*/
|
|
|
|
private _resolveFileName(
|
|
|
|
moduleSpecifier: ModuleSpecifier,
|
|
|
|
containingFile: ContainingFile
|
|
|
|
): ModuleFileName | undefined {
|
2019-02-02 18:27:53 -05:00
|
|
|
this._log("compiler._resolveFileName", { moduleSpecifier, containingFile });
|
2018-09-06 12:45:09 -04:00
|
|
|
const innerMap = this._fileNamesMap.get(containingFile);
|
|
|
|
if (innerMap) {
|
|
|
|
return innerMap.get(moduleSpecifier);
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2019-02-02 18:27:53 -05:00
|
|
|
/** Given a `moduleSpecifier` and `containingFile`, resolve the module and
|
|
|
|
* return the `ModuleMetaData`.
|
|
|
|
*/
|
|
|
|
private _resolveModule(
|
|
|
|
moduleSpecifier: ModuleSpecifier,
|
|
|
|
containingFile: ContainingFile
|
|
|
|
): ModuleMetaData {
|
|
|
|
this._log("compiler._resolveModule", { moduleSpecifier, containingFile });
|
|
|
|
assert(moduleSpecifier != null && moduleSpecifier.length > 0);
|
|
|
|
let fileName = this._resolveFileName(moduleSpecifier, containingFile);
|
|
|
|
if (fileName && this._moduleMetaDataMap.has(fileName)) {
|
|
|
|
return this._moduleMetaDataMap.get(fileName)!;
|
|
|
|
}
|
|
|
|
let moduleId: ModuleId | undefined;
|
|
|
|
let mediaType = msg.MediaType.Unknown;
|
|
|
|
let sourceCode: SourceCode | undefined;
|
|
|
|
if (
|
|
|
|
moduleSpecifier.startsWith(ASSETS) ||
|
|
|
|
containingFile.startsWith(ASSETS)
|
|
|
|
) {
|
|
|
|
// Assets are compiled into the runtime javascript bundle.
|
|
|
|
// we _know_ `.pop()` will return a string, but TypeScript doesn't so
|
|
|
|
// not null assertion
|
|
|
|
moduleId = moduleSpecifier.split("/").pop()!;
|
|
|
|
const assetName = moduleId.includes(".") ? moduleId : `${moduleId}.d.ts`;
|
2019-04-01 15:09:59 -04:00
|
|
|
assert(
|
|
|
|
assetName in this._assetsSourceCode,
|
|
|
|
`No such asset "${assetName}"`
|
|
|
|
);
|
2019-02-02 18:27:53 -05:00
|
|
|
mediaType = msg.MediaType.TypeScript;
|
2019-04-01 15:09:59 -04:00
|
|
|
sourceCode = this._assetsSourceCode[assetName];
|
2019-02-02 18:27:53 -05:00
|
|
|
fileName = `${ASSETS}/${assetName}`;
|
|
|
|
} else {
|
|
|
|
// We query Rust with a CodeFetch message. It will load the sourceCode,
|
|
|
|
// and if there is any outputCode cached, will return that as well.
|
2019-02-18 10:42:15 -05:00
|
|
|
const fetchResponse = this._os.fetchModuleMetaData(
|
|
|
|
moduleSpecifier,
|
|
|
|
containingFile
|
|
|
|
);
|
2019-02-02 18:27:53 -05:00
|
|
|
moduleId = fetchResponse.moduleName;
|
|
|
|
fileName = fetchResponse.filename;
|
|
|
|
mediaType = fetchResponse.mediaType;
|
|
|
|
sourceCode = fetchResponse.sourceCode;
|
|
|
|
}
|
|
|
|
assert(moduleId != null, "No module ID.");
|
|
|
|
assert(fileName != null, "No file name.");
|
|
|
|
assert(
|
|
|
|
mediaType !== msg.MediaType.Unknown,
|
|
|
|
`Unknown media type for: "${moduleSpecifier}" from "${containingFile}".`
|
|
|
|
);
|
|
|
|
this._log(
|
|
|
|
"resolveModule sourceCode length:",
|
|
|
|
sourceCode && sourceCode.length
|
|
|
|
);
|
|
|
|
this._log("resolveModule has media type:", msg.MediaType[mediaType]);
|
|
|
|
// fileName is asserted above, but TypeScript does not track so not null
|
|
|
|
this._setFileName(moduleSpecifier, containingFile, fileName!);
|
|
|
|
if (fileName && this._moduleMetaDataMap.has(fileName)) {
|
|
|
|
return this._moduleMetaDataMap.get(fileName)!;
|
|
|
|
}
|
|
|
|
const moduleMetaData = new ModuleMetaData(
|
|
|
|
moduleId!,
|
|
|
|
fileName!,
|
|
|
|
mediaType,
|
|
|
|
sourceCode
|
|
|
|
);
|
|
|
|
this._moduleMetaDataMap.set(fileName!, moduleMetaData);
|
|
|
|
return moduleMetaData;
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Caches the resolved `fileName` in relationship to the `moduleSpecifier`
|
2018-09-06 12:45:09 -04:00
|
|
|
* and `containingFile` in order to reduce calls to the privileged side
|
|
|
|
* to retrieve the contents of a module.
|
|
|
|
*/
|
|
|
|
private _setFileName(
|
|
|
|
moduleSpecifier: ModuleSpecifier,
|
|
|
|
containingFile: ContainingFile,
|
|
|
|
fileName: ModuleFileName
|
|
|
|
): void {
|
2019-02-02 18:27:53 -05:00
|
|
|
this._log("compiler._setFileName", { moduleSpecifier, containingFile });
|
2018-09-06 12:45:09 -04:00
|
|
|
let innerMap = this._fileNamesMap.get(containingFile);
|
|
|
|
if (!innerMap) {
|
|
|
|
innerMap = new Map();
|
|
|
|
this._fileNamesMap.set(containingFile, innerMap);
|
|
|
|
}
|
|
|
|
innerMap.set(moduleSpecifier, fileName);
|
|
|
|
}
|
|
|
|
|
2019-04-01 15:09:59 -04:00
|
|
|
constructor(assetsSourceCode: { [key: string]: string }) {
|
|
|
|
this._assetsSourceCode = assetsSourceCode;
|
2018-08-22 17:17:26 -04:00
|
|
|
this._service = this._ts.createLanguageService(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deno specific compiler API
|
|
|
|
|
2019-02-18 10:42:15 -05:00
|
|
|
/** Retrieve the output of the TypeScript compiler for a given module.
|
2018-08-22 17:17:26 -04:00
|
|
|
*/
|
2019-01-13 23:44:11 -05:00
|
|
|
compile(
|
|
|
|
moduleSpecifier: ModuleSpecifier,
|
|
|
|
containingFile: ContainingFile
|
2019-02-18 10:42:15 -05:00
|
|
|
): { outputCode: OutputCode; sourceMap: SourceMap } {
|
2019-02-02 18:27:53 -05:00
|
|
|
this._log("compiler.compile", { moduleSpecifier, containingFile });
|
|
|
|
const moduleMetaData = this._resolveModule(moduleSpecifier, containingFile);
|
2019-05-11 10:23:19 -04:00
|
|
|
const { fileName, mediaType, sourceCode } = moduleMetaData;
|
2019-02-02 18:27:53 -05:00
|
|
|
this._scriptFileNames = [fileName];
|
2019-02-18 10:42:15 -05:00
|
|
|
let outputCode: string;
|
|
|
|
let sourceMap = "";
|
2018-10-30 23:50:40 -04:00
|
|
|
// Instead of using TypeScript to transpile JSON modules, we will just do
|
|
|
|
// it directly.
|
2019-01-28 20:41:28 -05:00
|
|
|
if (mediaType === msg.MediaType.Json) {
|
2019-02-18 10:42:15 -05:00
|
|
|
outputCode = moduleMetaData.outputCode = jsonEsmTemplate(
|
|
|
|
sourceCode,
|
|
|
|
fileName
|
|
|
|
);
|
2018-10-30 23:50:40 -04:00
|
|
|
} else {
|
2018-11-01 20:30:30 -04:00
|
|
|
const service = this._service;
|
2018-10-30 23:50:40 -04:00
|
|
|
assert(
|
2019-01-28 20:41:28 -05:00
|
|
|
mediaType === msg.MediaType.TypeScript ||
|
|
|
|
mediaType === msg.MediaType.JavaScript
|
2018-08-22 17:17:26 -04:00
|
|
|
);
|
2018-10-30 23:50:40 -04:00
|
|
|
const output = service.getEmitOutput(fileName);
|
2018-08-22 17:17:26 -04:00
|
|
|
|
2018-10-30 23:50:40 -04:00
|
|
|
// Get the relevant diagnostics - this is 3x faster than
|
|
|
|
// `getPreEmitDiagnostics`.
|
|
|
|
const diagnostics = [
|
2018-11-01 20:30:30 -04:00
|
|
|
// 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.
|
|
|
|
...service
|
|
|
|
.getCompilerOptionsDiagnostics()
|
2019-04-21 16:40:10 -04:00
|
|
|
.filter((diagnostic): boolean => diagnostic.code !== 5070),
|
2018-10-30 23:50:40 -04:00
|
|
|
...service.getSyntacticDiagnostics(fileName),
|
|
|
|
...service.getSemanticDiagnostics(fileName)
|
|
|
|
];
|
|
|
|
if (diagnostics.length > 0) {
|
2019-04-29 10:58:31 -04:00
|
|
|
this._logDiagnostics(diagnostics);
|
2018-10-30 23:50:40 -04:00
|
|
|
}
|
2018-08-22 17:17:26 -04:00
|
|
|
|
2018-10-30 23:50:40 -04:00
|
|
|
assert(
|
|
|
|
!output.emitSkipped,
|
|
|
|
"The emit was skipped for an unknown reason."
|
|
|
|
);
|
|
|
|
|
|
|
|
assert(
|
|
|
|
output.outputFiles.length === 2,
|
|
|
|
`Expected 2 files to be emitted, got ${output.outputFiles.length}.`
|
|
|
|
);
|
|
|
|
|
|
|
|
const [sourceMapFile, outputFile] = output.outputFiles;
|
|
|
|
assert(
|
|
|
|
sourceMapFile.name.endsWith(".map"),
|
|
|
|
"Expected first emitted file to be a source map"
|
|
|
|
);
|
|
|
|
assert(
|
|
|
|
outputFile.name.endsWith(".js"),
|
|
|
|
"Expected second emitted file to be JavaScript"
|
|
|
|
);
|
2019-02-18 10:42:15 -05:00
|
|
|
outputCode = moduleMetaData.outputCode = `${
|
2018-10-30 23:50:40 -04:00
|
|
|
outputFile.text
|
|
|
|
}\n//# sourceURL=${fileName}`;
|
2019-02-18 10:42:15 -05:00
|
|
|
sourceMap = moduleMetaData.sourceMap = sourceMapFile.text;
|
2018-10-30 23:50:40 -04:00
|
|
|
}
|
2018-08-22 17:17:26 -04:00
|
|
|
|
2018-08-28 00:22:57 -04:00
|
|
|
moduleMetaData.scriptVersion = "1";
|
2019-02-18 10:42:15 -05:00
|
|
|
return { outputCode, sourceMap };
|
2019-01-06 14:17:13 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* options which were ignored, or `undefined`.
|
|
|
|
*/
|
|
|
|
configure(path: string, configurationText: string): string[] | undefined {
|
|
|
|
this._log("compile.configure", path);
|
|
|
|
const { config, error } = this._ts.parseConfigFileTextToJson(
|
|
|
|
path,
|
|
|
|
configurationText
|
|
|
|
);
|
|
|
|
if (error) {
|
|
|
|
this._logDiagnostics([error]);
|
|
|
|
}
|
|
|
|
const { options, errors } = this._ts.convertCompilerOptionsFromJson(
|
|
|
|
config.compilerOptions,
|
|
|
|
cwd()
|
|
|
|
);
|
|
|
|
if (errors.length) {
|
|
|
|
this._logDiagnostics(errors);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
return ignoredOptions.length ? ignoredOptions : undefined;
|
|
|
|
}
|
|
|
|
|
2018-09-06 12:45:09 -04:00
|
|
|
// TypeScript Language Service and Format Diagnostic Host API
|
2018-08-22 17:17:26 -04:00
|
|
|
|
2018-09-06 12:34:25 -04:00
|
|
|
getCanonicalFileName(fileName: string): string {
|
|
|
|
this._log("getCanonicalFileName", fileName);
|
|
|
|
return fileName;
|
|
|
|
}
|
|
|
|
|
2018-08-22 17:17:26 -04:00
|
|
|
getCompilationSettings(): ts.CompilerOptions {
|
|
|
|
this._log("getCompilationSettings()");
|
|
|
|
return this._options;
|
|
|
|
}
|
|
|
|
|
|
|
|
getNewLine(): string {
|
|
|
|
return EOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
getScriptFileNames(): string[] {
|
|
|
|
// This is equal to `"files"` in the `tsconfig.json`, therefore we only need
|
|
|
|
// to include the actual base source files we are evaluating at the moment,
|
2019-02-02 18:27:53 -05:00
|
|
|
// which would be what is set during the `.compile()`
|
2018-08-22 17:17:26 -04:00
|
|
|
return this._scriptFileNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
getScriptKind(fileName: ModuleFileName): ts.ScriptKind {
|
|
|
|
this._log("getScriptKind()", fileName);
|
2019-01-13 23:44:11 -05:00
|
|
|
const moduleMetaData = this._getModuleMetaData(fileName);
|
2018-10-21 22:14:27 -04:00
|
|
|
if (moduleMetaData) {
|
|
|
|
switch (moduleMetaData.mediaType) {
|
2019-01-28 20:41:28 -05:00
|
|
|
case msg.MediaType.TypeScript:
|
2018-10-21 22:14:27 -04:00
|
|
|
return ts.ScriptKind.TS;
|
2019-01-28 20:41:28 -05:00
|
|
|
case msg.MediaType.JavaScript:
|
2018-10-21 22:14:27 -04:00
|
|
|
return ts.ScriptKind.JS;
|
2019-01-28 20:41:28 -05:00
|
|
|
case msg.MediaType.Json:
|
2018-10-21 22:14:27 -04:00
|
|
|
return ts.ScriptKind.JSON;
|
|
|
|
default:
|
|
|
|
return this._options.allowJs ? ts.ScriptKind.JS : ts.ScriptKind.TS;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return this._options.allowJs ? ts.ScriptKind.JS : ts.ScriptKind.TS;
|
2018-08-22 17:17:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getScriptVersion(fileName: ModuleFileName): string {
|
2019-01-13 23:44:11 -05:00
|
|
|
const moduleMetaData = this._getModuleMetaData(fileName);
|
2019-02-02 18:27:53 -05:00
|
|
|
const version = (moduleMetaData && moduleMetaData.scriptVersion) || "";
|
|
|
|
this._log("getScriptVersion()", fileName, version);
|
|
|
|
return version;
|
2018-08-22 17:17:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
getScriptSnapshot(fileName: ModuleFileName): ts.IScriptSnapshot | undefined {
|
|
|
|
this._log("getScriptSnapshot()", fileName);
|
2019-01-13 23:44:11 -05:00
|
|
|
return this._getModuleMetaData(fileName);
|
2018-08-22 17:17:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
getCurrentDirectory(): string {
|
|
|
|
this._log("getCurrentDirectory()");
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
getDefaultLibFileName(): string {
|
|
|
|
this._log("getDefaultLibFileName()");
|
2018-10-11 17:23:13 -04:00
|
|
|
const moduleSpecifier = LIB_RUNTIME;
|
2019-02-02 18:27:53 -05:00
|
|
|
const moduleMetaData = this._getModuleMetaData(moduleSpecifier);
|
|
|
|
assert(moduleMetaData != null);
|
|
|
|
return moduleMetaData!.fileName;
|
2018-08-22 17:17:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
useCaseSensitiveFileNames(): boolean {
|
2018-08-28 00:27:17 -04:00
|
|
|
this._log("useCaseSensitiveFileNames()");
|
2018-08-22 17:17:26 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
readFile(path: string): string | undefined {
|
2018-08-28 00:27:17 -04:00
|
|
|
this._log("readFile()", path);
|
2018-08-22 17:17:26 -04:00
|
|
|
return notImplemented();
|
|
|
|
}
|
|
|
|
|
|
|
|
fileExists(fileName: string): boolean {
|
2019-01-13 23:44:11 -05:00
|
|
|
const moduleMetaData = this._getModuleMetaData(fileName);
|
2018-08-22 17:17:26 -04:00
|
|
|
const exists = moduleMetaData != null;
|
2018-08-28 00:27:17 -04:00
|
|
|
this._log("fileExists()", fileName, exists);
|
2018-08-22 17:17:26 -04:00
|
|
|
return exists;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolveModuleNames(
|
|
|
|
moduleNames: ModuleSpecifier[],
|
|
|
|
containingFile: ContainingFile
|
2018-10-21 22:14:27 -04:00
|
|
|
): Array<ts.ResolvedModuleFull | ts.ResolvedModule> {
|
2018-08-28 00:27:17 -04:00
|
|
|
this._log("resolveModuleNames()", { moduleNames, containingFile });
|
2019-02-02 18:27:53 -05:00
|
|
|
const resolvedModuleNames: ts.ResolvedModuleFull[] = [];
|
|
|
|
for (const moduleName of moduleNames) {
|
2019-03-07 21:56:56 -05:00
|
|
|
const moduleMetaData = this._resolveModule(moduleName, containingFile);
|
2018-08-22 17:17:26 -04:00
|
|
|
// According to the interface we shouldn't return `undefined` but if we
|
|
|
|
// fail to return the same length of modules to those we cannot resolve
|
|
|
|
// then TypeScript fails on an assertion that the lengths can't be
|
|
|
|
// different, so we have to return an "empty" resolved module
|
|
|
|
// TODO: all this does is push the problem downstream, and TypeScript
|
|
|
|
// will complain it can't identify the type of the file and throw
|
|
|
|
// a runtime exception, so we need to handle missing modules better
|
2018-10-21 22:14:27 -04:00
|
|
|
const resolvedFileName = moduleMetaData.fileName || "";
|
2018-08-22 17:17:26 -04:00
|
|
|
// This flags to the compiler to not go looking to transpile functional
|
|
|
|
// code, anything that is in `/$asset$/` is just library code
|
|
|
|
const isExternalLibraryImport = resolvedFileName.startsWith(ASSETS);
|
2019-02-02 18:27:53 -05:00
|
|
|
resolvedModuleNames.push({
|
2018-10-21 22:14:27 -04:00
|
|
|
resolvedFileName,
|
|
|
|
isExternalLibraryImport,
|
|
|
|
extension: getExtension(resolvedFileName, moduleMetaData.mediaType)
|
2019-02-02 18:27:53 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return resolvedModuleNames;
|
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-04-01 15:09:59 -04:00
|
|
|
const compiler = new Compiler(assetSourceCode);
|
2019-01-28 20:41:28 -05:00
|
|
|
|
|
|
|
// set global objects for compiler web worker
|
|
|
|
window.clearTimeout = clearTimer;
|
|
|
|
window.console = console;
|
|
|
|
window.postMessage = postMessage;
|
|
|
|
window.setTimeout = setTimeout;
|
|
|
|
window.workerMain = workerMain;
|
|
|
|
window.close = workerClose;
|
|
|
|
window.TextDecoder = TextDecoder;
|
|
|
|
window.TextEncoder = TextEncoder;
|
|
|
|
|
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-04-21 16:40:10 -04:00
|
|
|
window.onmessage = ({ data }: { data: CompilerLookup }): void => {
|
2019-04-04 05:33:32 -04:00
|
|
|
const { specifier, referrer, cmdId } = data;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const result = compiler.compile(specifier, referrer);
|
|
|
|
postMessage({
|
|
|
|
success: true,
|
|
|
|
cmdId,
|
|
|
|
data: result
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
postMessage({
|
|
|
|
success: false,
|
|
|
|
cmdId,
|
|
|
|
data: JSON.parse(core.errorToJSON(e))
|
|
|
|
});
|
|
|
|
}
|
2019-01-28 20:41:28 -05:00
|
|
|
};
|
|
|
|
};
|
2018-08-22 17:17:26 -04:00
|
|
|
|
2019-04-29 10:58:31 -04:00
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
|
|
|
// Perform the op to retrieve the compiler configuration if there was any
|
|
|
|
// provided on startup.
|
|
|
|
function getCompilerConfig(
|
|
|
|
compilerType: string
|
|
|
|
): { path: string; data: string } {
|
|
|
|
const builder = flatbuffers.createBuilder();
|
|
|
|
const compilerType_ = builder.createString(compilerType);
|
|
|
|
msg.CompilerConfig.startCompilerConfig(builder);
|
|
|
|
msg.CompilerConfig.addCompilerType(builder, compilerType_);
|
|
|
|
const inner = msg.CompilerConfig.endCompilerConfig(builder);
|
|
|
|
const baseRes = sendSync(builder, msg.Any.CompilerConfig, inner);
|
|
|
|
assert(baseRes != null);
|
|
|
|
assert(msg.Any.CompilerConfigRes === baseRes!.innerType());
|
|
|
|
const res = new msg.CompilerConfigRes();
|
|
|
|
assert(baseRes!.inner(res) != null);
|
|
|
|
|
|
|
|
// the privileged side does not normalize path separators in windows, so we
|
|
|
|
// will normalize them here
|
|
|
|
const path = res.path()!.replace(/\\/g, "/");
|
|
|
|
assert(path != null);
|
|
|
|
const dataArray = res.dataArray()!;
|
|
|
|
assert(dataArray != null);
|
|
|
|
const data = decoder.decode(dataArray);
|
|
|
|
return { path, data };
|
|
|
|
}
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
export default function denoMain(): void {
|
2019-02-10 20:07:02 -05:00
|
|
|
os.start("TS");
|
2019-04-29 10:58:31 -04:00
|
|
|
|
|
|
|
const { path, data } = getCompilerConfig("typescript");
|
|
|
|
if (data.length) {
|
|
|
|
const ignoredOptions = compiler.configure(path, data);
|
|
|
|
if (ignoredOptions) {
|
|
|
|
console.warn(
|
|
|
|
yellow(`Unsupported compiler options in "${path}"\n`) +
|
|
|
|
cyan(` The following options were ignored:\n`) +
|
|
|
|
` ${ignoredOptions.map((value): string => bold(value)).join(", ")}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-08-22 17:17:26 -04:00
|
|
|
}
|