2018-08-22 17:17:26 -04:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
|
|
|
import * as ts from "typescript";
|
2018-10-21 22:14:27 -04:00
|
|
|
import { MediaType } from "gen/msg_generated";
|
2018-08-22 17:17:26 -04:00
|
|
|
import { assetSourceCode } from "./assets";
|
2018-10-05 10:13:22 -04:00
|
|
|
// tslint:disable-next-line:no-circular-imports
|
2018-08-22 17:17:26 -04:00
|
|
|
import * as deno from "./deno";
|
2018-10-03 21:41:59 -04:00
|
|
|
import { globalEval } from "./global_eval";
|
2018-08-25 15:42:49 -04:00
|
|
|
import { libdeno } from "./libdeno";
|
|
|
|
import { window } from "./globals";
|
2018-08-22 17:17:26 -04:00
|
|
|
import * as os from "./os";
|
|
|
|
import { RawSourceMap } from "./types";
|
|
|
|
import { assert, log, notImplemented } from "./util";
|
|
|
|
import * as sourceMaps from "./v8_source_maps";
|
|
|
|
|
|
|
|
const EOL = "\n";
|
|
|
|
const ASSETS = "$asset$";
|
2018-10-11 17:23:13 -04:00
|
|
|
const LIB_RUNTIME = "lib.deno_runtime.d.ts";
|
2018-08-22 17:17:26 -04:00
|
|
|
|
|
|
|
// tslint:disable:no-any
|
|
|
|
type AmdCallback = (...args: any[]) => void;
|
|
|
|
type AmdErrback = (err: any) => void;
|
|
|
|
export type AmdFactory = (...args: any[]) => object | void;
|
|
|
|
// tslint:enable:no-any
|
2018-08-28 00:22:57 -04:00
|
|
|
export type AmdDefine = (deps: ModuleSpecifier[], factory: AmdFactory) => void;
|
|
|
|
type AMDRequire = (
|
|
|
|
deps: ModuleSpecifier[],
|
|
|
|
callback: AmdCallback,
|
|
|
|
errback: AmdErrback
|
|
|
|
) => void;
|
2018-08-22 17:17:26 -04: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`
|
|
|
|
*/
|
2018-08-22 17:17:26 -04: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`
|
|
|
|
*/
|
2018-08-22 17:17:26 -04: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
|
|
|
|
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.
|
2018-09-04 15:23:38 -04:00
|
|
|
* @internal
|
2018-08-22 17:17:26 -04:00
|
|
|
*/
|
|
|
|
export interface Os {
|
|
|
|
codeCache: typeof os.codeCache;
|
|
|
|
codeFetch: typeof os.codeFetch;
|
|
|
|
exit: typeof os.exit;
|
|
|
|
}
|
|
|
|
|
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.
|
2018-09-04 15:23:38 -04:00
|
|
|
* @internal
|
2018-08-22 17:17:26 -04:00
|
|
|
*/
|
|
|
|
export interface Ts {
|
|
|
|
createLanguageService: typeof ts.createLanguageService;
|
|
|
|
/* tslint:disable-next-line:max-line-length */
|
|
|
|
formatDiagnosticsWithColorAndContext: typeof ts.formatDiagnosticsWithColorAndContext;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2018-08-24 00:07:01 -04:00
|
|
|
export class ModuleMetaData implements ts.IScriptSnapshot {
|
2018-08-28 00:22:57 -04:00
|
|
|
public deps?: ModuleFileName[];
|
2018-10-30 23:50:40 -04:00
|
|
|
public exports = {};
|
2018-08-28 00:22:57 -04:00
|
|
|
public factory?: AmdFactory;
|
2018-09-01 17:28:53 -04:00
|
|
|
public gatheringDeps = false;
|
2018-08-28 00:22:57 -04:00
|
|
|
public hasRun = false;
|
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,
|
2018-10-21 22:14:27 -04:00
|
|
|
public readonly mediaType: MediaType,
|
2018-09-07 15:25:25 -04:00
|
|
|
public readonly sourceCode: SourceCode = "",
|
2018-10-28 19:41:10 -04:00
|
|
|
public outputCode: OutputCode = "",
|
|
|
|
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
|
|
|
|
|
|
|
public getText(start: number, end: number): string {
|
|
|
|
return this.sourceCode.substring(start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-21 22:14:27 -04:00
|
|
|
function getExtension(
|
|
|
|
fileName: ModuleFileName,
|
|
|
|
mediaType: MediaType
|
|
|
|
): ts.Extension | undefined {
|
|
|
|
switch (mediaType) {
|
|
|
|
case MediaType.JavaScript:
|
|
|
|
return ts.Extension.Js;
|
|
|
|
case MediaType.TypeScript:
|
|
|
|
return fileName.endsWith(".d.ts") ? ts.Extension.Dts : ts.Extension.Ts;
|
|
|
|
case MediaType.Json:
|
|
|
|
return ts.Extension.Json;
|
|
|
|
case MediaType.Unknown:
|
|
|
|
default:
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 23:50:40 -04:00
|
|
|
/** Generate output code for a provided JSON string along with its source. */
|
|
|
|
export function jsonAmdTemplate(
|
|
|
|
jsonString: string,
|
|
|
|
sourceFileName: string
|
|
|
|
): OutputCode {
|
|
|
|
// tslint:disable-next-line:max-line-length
|
|
|
|
return `define([], function() { return JSON.parse(\`${jsonString}\`); });\n//# sourceURL=${sourceFileName}`;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2018-09-06 12:34:25 -04:00
|
|
|
export class DenoCompiler
|
|
|
|
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 global eval, so it can be monkey patched during testing
|
|
|
|
private _globalEval = globalEval;
|
|
|
|
// 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,
|
2018-10-24 15:26:21 -04:00
|
|
|
checkJs: true,
|
2018-08-22 17:17:26 -04:00
|
|
|
module: ts.ModuleKind.AMD,
|
|
|
|
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;
|
2018-08-28 00:22:57 -04:00
|
|
|
// Contains a queue of modules that have been resolved, but not yet
|
|
|
|
// run
|
|
|
|
private _runQueue: ModuleMetaData[] = [];
|
2018-08-22 17:17:26 -04:00
|
|
|
// Used to contain the script file we are currently running
|
|
|
|
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;
|
|
|
|
// A reference to the global scope so it can be monkey patched during
|
|
|
|
// testing
|
|
|
|
private _window = window;
|
2018-09-24 15:33:50 -04:00
|
|
|
// Flags forcing recompilation of TS code
|
|
|
|
public recompile = false;
|
2018-08-22 17:17:26 -04:00
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Drain the run queue, retrieving the arguments for the module
|
2018-08-28 00:22:57 -04:00
|
|
|
* factory and calling the module's factory.
|
|
|
|
*/
|
|
|
|
private _drainRunQueue(): void {
|
|
|
|
this._log(
|
|
|
|
"compiler._drainRunQueue",
|
|
|
|
this._runQueue.map(metaData => metaData.fileName)
|
|
|
|
);
|
|
|
|
let moduleMetaData: ModuleMetaData | undefined;
|
|
|
|
while ((moduleMetaData = this._runQueue.shift())) {
|
|
|
|
assert(
|
|
|
|
moduleMetaData.factory != null,
|
|
|
|
"Cannot run module without factory."
|
|
|
|
);
|
|
|
|
assert(moduleMetaData.hasRun === false, "Module has already been run.");
|
|
|
|
// asserts not tracked by TypeScripts, so using not null operator
|
2018-10-30 23:50:40 -04:00
|
|
|
const exports = moduleMetaData.factory!(
|
|
|
|
...this._getFactoryArguments(moduleMetaData)
|
|
|
|
);
|
|
|
|
// For JSON module support and potential future features.
|
|
|
|
// TypeScript always imports `exports` and mutates it directly, but the
|
|
|
|
// AMD specification allows values to be returned from the factory.
|
|
|
|
if (exports != null) {
|
|
|
|
moduleMetaData.exports = exports;
|
|
|
|
}
|
2018-08-28 00:22:57 -04:00
|
|
|
moduleMetaData.hasRun = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Get the dependencies for a given module, but don't run the module,
|
2018-08-28 00:22:57 -04:00
|
|
|
* just add the module factory to the run queue.
|
|
|
|
*/
|
|
|
|
private _gatherDependencies(moduleMetaData: ModuleMetaData): void {
|
|
|
|
this._log("compiler._resolveDependencies", moduleMetaData.fileName);
|
|
|
|
|
|
|
|
// if the module has already run, we can short circuit.
|
|
|
|
// it is intentional though that if we have already resolved dependencies,
|
|
|
|
// we won't short circuit, as something may have changed, or we might have
|
|
|
|
// only collected the dependencies to be able to able to obtain the graph of
|
|
|
|
// dependencies
|
|
|
|
if (moduleMetaData.hasRun) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-06 12:45:09 -04:00
|
|
|
this._window.define = this._makeDefine(moduleMetaData);
|
2018-08-28 00:22:57 -04:00
|
|
|
this._globalEval(this.compile(moduleMetaData));
|
|
|
|
this._window.define = undefined;
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Retrieve the arguments to pass a module's factory function. */
|
2018-08-28 00:22:57 -04:00
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
private _getFactoryArguments(moduleMetaData: ModuleMetaData): any[] {
|
|
|
|
if (!moduleMetaData.deps) {
|
|
|
|
throw new Error("Cannot get arguments until dependencies resolved.");
|
|
|
|
}
|
|
|
|
return moduleMetaData.deps.map(dep => {
|
|
|
|
if (dep === "require") {
|
|
|
|
return this._makeLocalRequire(moduleMetaData);
|
|
|
|
}
|
|
|
|
if (dep === "exports") {
|
|
|
|
return moduleMetaData.exports;
|
|
|
|
}
|
|
|
|
if (dep in DenoCompiler._builtins) {
|
|
|
|
return DenoCompiler._builtins[dep];
|
|
|
|
}
|
|
|
|
const dependencyMetaData = this._getModuleMetaData(dep);
|
|
|
|
assert(dependencyMetaData != null, `Missing dependency "${dep}".`);
|
|
|
|
// TypeScript does not track assert, therefore using not null operator
|
|
|
|
return dependencyMetaData!.exports;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
private _getModuleMetaData(
|
|
|
|
fileName: ModuleFileName
|
|
|
|
): ModuleMetaData | undefined {
|
|
|
|
return this._moduleMetaDataMap.has(fileName)
|
|
|
|
? this._moduleMetaDataMap.get(fileName)
|
|
|
|
: fileName.startsWith(ASSETS)
|
|
|
|
? this.resolveModule(fileName, "")
|
|
|
|
: undefined;
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Create a localized AMD `define` function and return it. */
|
2018-09-06 12:45:09 -04:00
|
|
|
private _makeDefine(moduleMetaData: ModuleMetaData): AmdDefine {
|
|
|
|
return (deps: ModuleSpecifier[], factory: AmdFactory): void => {
|
|
|
|
this._log("compiler.localDefine", moduleMetaData.fileName);
|
|
|
|
moduleMetaData.factory = factory;
|
|
|
|
// when there are circular dependencies, we need to skip recursing the
|
|
|
|
// dependencies
|
|
|
|
moduleMetaData.gatheringDeps = true;
|
|
|
|
// we will recursively resolve the dependencies for any modules
|
|
|
|
moduleMetaData.deps = deps.map(dep => {
|
|
|
|
if (
|
|
|
|
dep === "require" ||
|
|
|
|
dep === "exports" ||
|
|
|
|
dep in DenoCompiler._builtins
|
|
|
|
) {
|
|
|
|
return dep;
|
|
|
|
}
|
|
|
|
const dependencyMetaData = this.resolveModule(
|
|
|
|
dep,
|
|
|
|
moduleMetaData.fileName
|
|
|
|
);
|
|
|
|
if (!dependencyMetaData.gatheringDeps) {
|
|
|
|
this._gatherDependencies(dependencyMetaData);
|
|
|
|
}
|
|
|
|
return dependencyMetaData.fileName;
|
|
|
|
});
|
|
|
|
moduleMetaData.gatheringDeps = false;
|
|
|
|
if (!this._runQueue.includes(moduleMetaData)) {
|
|
|
|
this._runQueue.push(moduleMetaData);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Returns a require that specifically handles the resolution of a transpiled
|
2018-08-28 00:22:57 -04:00
|
|
|
* emit of a dynamic ES `import()` from TypeScript.
|
|
|
|
*/
|
|
|
|
private _makeLocalRequire(moduleMetaData: ModuleMetaData): AMDRequire {
|
2018-09-01 18:49:47 -04:00
|
|
|
return (
|
2018-08-28 00:22:57 -04:00
|
|
|
deps: ModuleSpecifier[],
|
|
|
|
callback: AmdCallback,
|
|
|
|
errback: AmdErrback
|
|
|
|
): void => {
|
|
|
|
log("localRequire", deps);
|
|
|
|
assert(
|
|
|
|
deps.length === 1,
|
|
|
|
"Local require requires exactly one dependency."
|
|
|
|
);
|
|
|
|
const [moduleSpecifier] = deps;
|
|
|
|
try {
|
|
|
|
const requiredMetaData = this.run(
|
|
|
|
moduleSpecifier,
|
|
|
|
moduleMetaData.fileName
|
|
|
|
);
|
|
|
|
callback(requiredMetaData.exports);
|
|
|
|
} catch (e) {
|
|
|
|
errback(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
this._log("compiler.resolveFileName", { moduleSpecifier, containingFile });
|
|
|
|
const innerMap = this._fileNamesMap.get(containingFile);
|
|
|
|
if (innerMap) {
|
|
|
|
return innerMap.get(moduleSpecifier);
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
this._log("compiler.setFileName", { moduleSpecifier, containingFile });
|
|
|
|
let innerMap = this._fileNamesMap.get(containingFile);
|
|
|
|
if (!innerMap) {
|
|
|
|
innerMap = new Map();
|
|
|
|
this._fileNamesMap.set(containingFile, innerMap);
|
|
|
|
}
|
|
|
|
innerMap.set(moduleSpecifier, fileName);
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Setup being able to map back source references back to their source
|
2018-08-22 17:17:26 -04:00
|
|
|
*
|
|
|
|
* TODO is this the best place for this? It is tightly coupled to how the
|
|
|
|
* compiler works, but it is also tightly coupled to how the whole runtime
|
|
|
|
* environment is bootstrapped. It also needs efficient access to the
|
|
|
|
* `outputCode` of the module information, which exists inside of the
|
|
|
|
* compiler instance.
|
|
|
|
*/
|
|
|
|
private _setupSourceMaps(): void {
|
2018-10-28 19:41:10 -04:00
|
|
|
let lastModule: ModuleMetaData | undefined;
|
2018-08-22 17:17:26 -04:00
|
|
|
sourceMaps.install({
|
|
|
|
installPrepareStackTrace: true,
|
|
|
|
getGeneratedContents: (fileName: string): string | RawSourceMap => {
|
2018-08-28 00:27:17 -04:00
|
|
|
this._log("compiler.getGeneratedContents", fileName);
|
2018-08-22 17:17:26 -04:00
|
|
|
if (fileName === "gen/bundle/main.js") {
|
|
|
|
assert(libdeno.mainSource.length > 0);
|
|
|
|
return libdeno.mainSource;
|
|
|
|
} else if (fileName === "main.js.map") {
|
|
|
|
return libdeno.mainSourceMap;
|
|
|
|
} else if (fileName === "deno_main.js") {
|
|
|
|
return "";
|
2018-10-28 19:41:10 -04:00
|
|
|
} else if (!fileName.endsWith(".map")) {
|
2018-08-22 17:17:26 -04:00
|
|
|
const moduleMetaData = this._moduleMetaDataMap.get(fileName);
|
|
|
|
if (!moduleMetaData) {
|
2018-10-28 19:41:10 -04:00
|
|
|
lastModule = undefined;
|
2018-08-22 17:17:26 -04:00
|
|
|
return "";
|
|
|
|
}
|
2018-10-28 19:41:10 -04:00
|
|
|
lastModule = moduleMetaData;
|
2018-08-22 17:17:26 -04:00
|
|
|
return moduleMetaData.outputCode;
|
2018-10-28 19:41:10 -04:00
|
|
|
} else {
|
|
|
|
if (lastModule && lastModule.sourceMap) {
|
|
|
|
// Assuming the the map will always be asked for after the source
|
|
|
|
// code.
|
|
|
|
const { sourceMap } = lastModule;
|
|
|
|
lastModule = undefined;
|
|
|
|
return sourceMap;
|
|
|
|
} else {
|
|
|
|
// Errors thrown here are caught by source-map.
|
|
|
|
throw new Error(`Unable to find source map: "${fileName}"`);
|
|
|
|
}
|
2018-08-22 17:17:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private constructor() {
|
|
|
|
if (DenoCompiler._instance) {
|
|
|
|
throw new TypeError("Attempt to create an additional compiler.");
|
|
|
|
}
|
|
|
|
this._service = this._ts.createLanguageService(this);
|
|
|
|
this._setupSourceMaps();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deno specific compiler API
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Retrieve the output of the TypeScript compiler for a given module and
|
2018-09-24 15:33:50 -04:00
|
|
|
* cache the result. Re-compilation can be forced using '--recompile' flag.
|
2018-08-22 17:17:26 -04:00
|
|
|
*/
|
2018-08-28 00:22:57 -04:00
|
|
|
compile(moduleMetaData: ModuleMetaData): OutputCode {
|
2018-09-24 15:33:50 -04:00
|
|
|
const recompile = !!this.recompile;
|
|
|
|
if (!recompile && moduleMetaData.outputCode) {
|
2018-08-28 00:22:57 -04:00
|
|
|
return moduleMetaData.outputCode;
|
|
|
|
}
|
2018-10-30 23:50:40 -04:00
|
|
|
const { fileName, sourceCode, mediaType, moduleId } = moduleMetaData;
|
2018-09-26 22:58:11 -04:00
|
|
|
console.warn("Compiling", moduleId);
|
2018-10-30 23:50:40 -04:00
|
|
|
// Instead of using TypeScript to transpile JSON modules, we will just do
|
|
|
|
// it directly.
|
|
|
|
if (mediaType === MediaType.Json) {
|
|
|
|
moduleMetaData.outputCode = jsonAmdTemplate(sourceCode, fileName);
|
|
|
|
} else {
|
2018-11-01 20:30:30 -04:00
|
|
|
const service = this._service;
|
2018-10-30 23:50:40 -04:00
|
|
|
assert(
|
|
|
|
mediaType === MediaType.TypeScript || mediaType === 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()
|
|
|
|
.filter(diagnostic => diagnostic.code !== 5070),
|
2018-10-30 23:50:40 -04:00
|
|
|
...service.getSyntacticDiagnostics(fileName),
|
|
|
|
...service.getSemanticDiagnostics(fileName)
|
|
|
|
];
|
|
|
|
if (diagnostics.length > 0) {
|
|
|
|
const errMsg = this._ts.formatDiagnosticsWithColorAndContext(
|
|
|
|
diagnostics,
|
|
|
|
this
|
|
|
|
);
|
|
|
|
console.log(errMsg);
|
|
|
|
// All TypeScript errors are terminal for deno
|
|
|
|
this._os.exit(1);
|
|
|
|
}
|
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"
|
|
|
|
);
|
|
|
|
moduleMetaData.outputCode = `${
|
|
|
|
outputFile.text
|
|
|
|
}\n//# sourceURL=${fileName}`;
|
|
|
|
moduleMetaData.sourceMap = sourceMapFile.text;
|
|
|
|
}
|
2018-08-22 17:17:26 -04:00
|
|
|
|
2018-08-28 00:22:57 -04:00
|
|
|
moduleMetaData.scriptVersion = "1";
|
2018-10-30 23:50:40 -04:00
|
|
|
this._os.codeCache(
|
|
|
|
fileName,
|
|
|
|
sourceCode,
|
|
|
|
moduleMetaData.outputCode,
|
|
|
|
moduleMetaData.sourceMap
|
|
|
|
);
|
2018-08-28 00:22:57 -04:00
|
|
|
return moduleMetaData.outputCode;
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** For a given module specifier and containing file, return a list of
|
|
|
|
* absolute identifiers for dependent modules that are required by this
|
|
|
|
* module.
|
2018-08-28 00:22:57 -04:00
|
|
|
*/
|
|
|
|
getModuleDependencies(
|
|
|
|
moduleSpecifier: ModuleSpecifier,
|
|
|
|
containingFile: ContainingFile
|
|
|
|
): ModuleFileName[] {
|
|
|
|
assert(
|
|
|
|
this._runQueue.length === 0,
|
|
|
|
"Cannot get dependencies with modules queued to be run."
|
|
|
|
);
|
|
|
|
const moduleMetaData = this.resolveModule(moduleSpecifier, containingFile);
|
|
|
|
assert(
|
|
|
|
!moduleMetaData.hasRun,
|
|
|
|
"Cannot get dependencies for a module that has already been run."
|
|
|
|
);
|
|
|
|
this._gatherDependencies(moduleMetaData);
|
|
|
|
const dependencies = this._runQueue.map(
|
2018-09-02 18:50:46 -04:00
|
|
|
moduleMetaData => moduleMetaData.moduleId
|
2018-08-28 00:22:57 -04:00
|
|
|
);
|
|
|
|
// empty the run queue, to free up references to factories we have collected
|
|
|
|
// and to ensure that if there is a further invocation of `.run()` the
|
|
|
|
// factories don't get called
|
|
|
|
this._runQueue = [];
|
|
|
|
return dependencies;
|
2018-08-22 17:17:26 -04:00
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Given a `moduleSpecifier` and `containingFile`, resolve the module and
|
2018-08-22 17:17:26 -04:00
|
|
|
* return the `ModuleMetaData`.
|
|
|
|
*/
|
|
|
|
resolveModule(
|
|
|
|
moduleSpecifier: ModuleSpecifier,
|
|
|
|
containingFile: ContainingFile
|
|
|
|
): ModuleMetaData {
|
2018-08-28 00:27:17 -04:00
|
|
|
this._log("compiler.resolveModule", { moduleSpecifier, containingFile });
|
2018-08-22 17:17:26 -04:00
|
|
|
assert(moduleSpecifier != null && moduleSpecifier.length > 0);
|
2018-09-06 12:45:09 -04:00
|
|
|
let fileName = this._resolveFileName(moduleSpecifier, containingFile);
|
2018-08-22 17:17:26 -04:00
|
|
|
if (fileName && this._moduleMetaDataMap.has(fileName)) {
|
|
|
|
return this._moduleMetaDataMap.get(fileName)!;
|
|
|
|
}
|
2018-10-21 22:14:27 -04:00
|
|
|
let moduleId: ModuleId | undefined;
|
|
|
|
let mediaType = MediaType.Unknown;
|
|
|
|
let sourceCode: SourceCode | undefined;
|
|
|
|
let outputCode: OutputCode | undefined;
|
2018-10-28 19:41:10 -04:00
|
|
|
let sourceMap: SourceMap | undefined;
|
2018-08-22 17:17:26 -04:00
|
|
|
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
|
2018-09-08 16:40:16 -04:00
|
|
|
moduleId = moduleSpecifier.split("/").pop()!;
|
2018-08-22 17:17:26 -04:00
|
|
|
const assetName = moduleId.includes(".") ? moduleId : `${moduleId}.d.ts`;
|
|
|
|
assert(assetName in assetSourceCode, `No such asset "${assetName}"`);
|
2018-10-21 22:14:27 -04:00
|
|
|
mediaType = MediaType.TypeScript;
|
2018-08-22 17:17:26 -04:00
|
|
|
sourceCode = assetSourceCode[assetName];
|
|
|
|
fileName = `${ASSETS}/${assetName}`;
|
2018-09-08 16:40:16 -04:00
|
|
|
outputCode = "";
|
2018-10-28 19:41:10 -04:00
|
|
|
sourceMap = "";
|
2018-08-22 17:17:26 -04:00
|
|
|
} 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.
|
2018-09-08 16:40:16 -04:00
|
|
|
const fetchResponse = this._os.codeFetch(moduleSpecifier, containingFile);
|
2018-10-21 22:14:27 -04:00
|
|
|
moduleId = fetchResponse.moduleName;
|
|
|
|
fileName = fetchResponse.filename;
|
|
|
|
mediaType = fetchResponse.mediaType;
|
|
|
|
sourceCode = fetchResponse.sourceCode;
|
|
|
|
outputCode = fetchResponse.outputCode;
|
2018-10-28 19:41:10 -04:00
|
|
|
sourceMap = fetchResponse.sourceMap;
|
2018-08-22 17:17:26 -04:00
|
|
|
}
|
2018-10-21 22:14:27 -04:00
|
|
|
assert(moduleId != null, "No module ID.");
|
|
|
|
assert(fileName != null, "No file name.");
|
|
|
|
assert(sourceCode ? sourceCode.length > 0 : false, "No source code.");
|
|
|
|
assert(
|
|
|
|
mediaType !== MediaType.Unknown,
|
|
|
|
`Unknown media type for: "${moduleSpecifier}" from "${containingFile}".`
|
|
|
|
);
|
|
|
|
this._log(
|
|
|
|
"resolveModule sourceCode length:",
|
|
|
|
sourceCode && sourceCode.length
|
|
|
|
);
|
|
|
|
this._log("resolveModule has outputCode:", outputCode != null);
|
2018-10-28 19:41:10 -04:00
|
|
|
this._log("resolveModule has source map:", sourceMap != null);
|
2018-10-21 22:14:27 -04:00
|
|
|
this._log("resolveModule has media type:", MediaType[mediaType]);
|
|
|
|
// fileName is asserted above, but TypeScript does not track so not null
|
|
|
|
this._setFileName(moduleSpecifier, containingFile, fileName!);
|
2018-08-22 17:17:26 -04:00
|
|
|
if (fileName && this._moduleMetaDataMap.has(fileName)) {
|
|
|
|
return this._moduleMetaDataMap.get(fileName)!;
|
|
|
|
}
|
2018-09-02 18:50:46 -04:00
|
|
|
const moduleMetaData = new ModuleMetaData(
|
2018-10-21 22:14:27 -04:00
|
|
|
moduleId!,
|
|
|
|
fileName!,
|
|
|
|
mediaType,
|
2018-09-02 18:50:46 -04:00
|
|
|
sourceCode,
|
2018-10-28 19:41:10 -04:00
|
|
|
outputCode,
|
|
|
|
sourceMap
|
2018-09-02 18:50:46 -04:00
|
|
|
);
|
2018-10-21 22:14:27 -04:00
|
|
|
this._moduleMetaDataMap.set(fileName!, moduleMetaData);
|
2018-08-22 17:17:26 -04:00
|
|
|
return moduleMetaData;
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Load and run a module and all of its dependencies based on a module
|
2018-08-28 00:22:57 -04:00
|
|
|
* specifier and a containing file
|
2018-08-22 17:17:26 -04:00
|
|
|
*/
|
|
|
|
run(
|
|
|
|
moduleSpecifier: ModuleSpecifier,
|
|
|
|
containingFile: ContainingFile
|
|
|
|
): ModuleMetaData {
|
2018-08-28 00:22:57 -04:00
|
|
|
this._log("compiler.run", { moduleSpecifier, containingFile });
|
2018-08-22 17:17:26 -04:00
|
|
|
const moduleMetaData = this.resolveModule(moduleSpecifier, containingFile);
|
2018-08-28 00:22:57 -04:00
|
|
|
this._scriptFileNames = [moduleMetaData.fileName];
|
|
|
|
if (!moduleMetaData.deps) {
|
|
|
|
this._gatherDependencies(moduleMetaData);
|
2018-08-22 17:17:26 -04:00
|
|
|
}
|
2018-08-28 00:22:57 -04:00
|
|
|
this._drainRunQueue();
|
|
|
|
return moduleMetaData;
|
2018-08-22 17:17:26 -04:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
// which would be what is set during the `.run()`
|
|
|
|
return this._scriptFileNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
getScriptKind(fileName: ModuleFileName): ts.ScriptKind {
|
|
|
|
this._log("getScriptKind()", fileName);
|
2018-10-21 22:14:27 -04:00
|
|
|
const moduleMetaData = this._getModuleMetaData(fileName);
|
|
|
|
if (moduleMetaData) {
|
|
|
|
switch (moduleMetaData.mediaType) {
|
|
|
|
case MediaType.TypeScript:
|
|
|
|
return ts.ScriptKind.TS;
|
|
|
|
case MediaType.JavaScript:
|
|
|
|
return ts.ScriptKind.JS;
|
|
|
|
case MediaType.Json:
|
|
|
|
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 {
|
|
|
|
this._log("getScriptVersion()", fileName);
|
|
|
|
const moduleMetaData = this._getModuleMetaData(fileName);
|
|
|
|
return (moduleMetaData && moduleMetaData.scriptVersion) || "";
|
|
|
|
}
|
|
|
|
|
|
|
|
getScriptSnapshot(fileName: ModuleFileName): ts.IScriptSnapshot | undefined {
|
|
|
|
this._log("getScriptSnapshot()", fileName);
|
2018-08-24 00:07:01 -04: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;
|
2018-08-22 17:17:26 -04:00
|
|
|
const moduleMetaData = this.resolveModule(moduleSpecifier, ASSETS);
|
|
|
|
return moduleMetaData.fileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
const moduleMetaData = this._getModuleMetaData(fileName);
|
|
|
|
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 });
|
2018-08-22 17:17:26 -04:00
|
|
|
return moduleNames.map(name => {
|
2018-10-21 22:14:27 -04:00
|
|
|
let moduleMetaData: ModuleMetaData;
|
2018-10-05 10:13:22 -04:00
|
|
|
if (name === "deno") {
|
2018-10-11 17:23:13 -04:00
|
|
|
// builtin modules are part of the runtime lib
|
2018-10-21 22:14:27 -04:00
|
|
|
moduleMetaData = this.resolveModule(LIB_RUNTIME, ASSETS);
|
2018-08-22 17:17:26 -04:00
|
|
|
} else if (name === "typescript") {
|
2018-10-21 22:14:27 -04:00
|
|
|
moduleMetaData = this.resolveModule("typescript.d.ts", ASSETS);
|
2018-08-22 17:17:26 -04:00
|
|
|
} else {
|
2018-10-21 22:14:27 -04:00
|
|
|
moduleMetaData = this.resolveModule(name, 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);
|
2018-10-21 22:14:27 -04:00
|
|
|
return {
|
|
|
|
resolvedFileName,
|
|
|
|
isExternalLibraryImport,
|
|
|
|
extension: getExtension(resolvedFileName, moduleMetaData.mediaType)
|
|
|
|
};
|
2018-08-22 17:17:26 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deno specific static properties and methods
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Built in modules which can be returned to external modules
|
2018-08-22 17:17:26 -04:00
|
|
|
*
|
|
|
|
* Placed as a private static otherwise we get use before
|
|
|
|
* declared with the `DenoCompiler`
|
|
|
|
*/
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
private static _builtins: { [mid: string]: any } = {
|
|
|
|
typescript: ts,
|
2018-10-05 10:13:22 -04:00
|
|
|
deno
|
2018-08-22 17:17:26 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
private static _instance: DenoCompiler | undefined;
|
|
|
|
|
2018-10-21 22:14:27 -04:00
|
|
|
/** Returns the instance of `DenoCompiler` or creates a new instance. */
|
2018-08-22 17:17:26 -04:00
|
|
|
static instance(): DenoCompiler {
|
|
|
|
return (
|
|
|
|
DenoCompiler._instance || (DenoCompiler._instance = new DenoCompiler())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|