2018-05-28 21:50:44 -04:00
|
|
|
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
|
|
|
// All rights reserved. MIT License.
|
2018-05-17 09:47:09 -04:00
|
|
|
// Glossary
|
|
|
|
// outputCode = generated javascript code
|
|
|
|
// sourceCode = typescript code (or input javascript code)
|
|
|
|
// moduleName = a resolved module name
|
2018-05-19 04:47:40 -04:00
|
|
|
// fileName = an unresolved raw fileName.
|
|
|
|
// for http modules , its the path to the locally downloaded
|
|
|
|
// version.
|
2018-05-17 09:47:09 -04:00
|
|
|
|
|
|
|
import * as ts from "typescript";
|
|
|
|
import * as util from "./util";
|
|
|
|
import { log } from "./util";
|
|
|
|
import * as os from "./os";
|
2018-05-21 09:54:37 -04:00
|
|
|
import * as sourceMaps from "./v8_source_maps";
|
2018-05-21 18:53:53 -04:00
|
|
|
import { _global, globalEval } from "./globals";
|
2018-05-30 12:59:50 -04:00
|
|
|
import * as deno from "./deno";
|
2018-05-17 09:47:09 -04:00
|
|
|
|
2018-05-18 12:18:42 -04:00
|
|
|
const EOL = "\n";
|
|
|
|
|
2018-05-19 07:06:23 -04:00
|
|
|
// tslint:disable-next-line:no-any
|
2018-06-04 03:06:42 -04:00
|
|
|
export type AmdFactory = (...args: any[]) => undefined | object;
|
|
|
|
export type AmdDefine = (deps: string[], factory: AmdFactory) => void;
|
2018-05-19 07:06:23 -04:00
|
|
|
|
2018-05-27 00:15:52 -04:00
|
|
|
// Uncaught exceptions are sent to window.onerror by v8worker2.
|
2018-05-29 03:08:01 -04:00
|
|
|
// https://git.io/vhOsf
|
2018-05-27 03:46:18 -04:00
|
|
|
window.onerror = (message, source, lineno, colno, error) => {
|
2018-05-28 11:16:38 -04:00
|
|
|
// TODO Currently there is a bug in v8_source_maps.ts that causes a segfault
|
|
|
|
// if it is used within window.onerror. To workaround we uninstall the
|
|
|
|
// Error.prepareStackTrace handler. Users will get unmapped stack traces on
|
|
|
|
// uncaught exceptions until this issue is fixed.
|
|
|
|
Error.prepareStackTrace = null;
|
2018-05-27 00:15:52 -04:00
|
|
|
console.log(error.message, error.stack);
|
|
|
|
os.exit(1);
|
|
|
|
};
|
|
|
|
|
2018-05-21 17:33:33 -04:00
|
|
|
export function setup(mainJs: string, mainMap: string): void {
|
|
|
|
sourceMaps.install({
|
|
|
|
installPrepareStackTrace: true,
|
|
|
|
getGeneratedContents: (filename: string): string => {
|
|
|
|
if (filename === "/main.js") {
|
|
|
|
return mainJs;
|
|
|
|
} else if (filename === "/main.map") {
|
|
|
|
return mainMap;
|
|
|
|
} else {
|
|
|
|
const mod = FileModule.load(filename);
|
|
|
|
if (!mod) {
|
|
|
|
console.error("getGeneratedContents cannot find", filename);
|
|
|
|
}
|
|
|
|
return mod.outputCode;
|
|
|
|
}
|
2018-05-21 09:54:37 -04:00
|
|
|
}
|
2018-05-21 17:33:33 -04:00
|
|
|
});
|
|
|
|
}
|
2018-05-21 09:54:37 -04:00
|
|
|
|
2018-05-17 09:47:09 -04:00
|
|
|
// This class represents a module. We call it FileModule to make it explicit
|
|
|
|
// that each module represents a single file.
|
|
|
|
// Access to FileModule instances should only be done thru the static method
|
2018-05-19 04:47:40 -04:00
|
|
|
// FileModule.load(). FileModules are NOT executed upon first load, only when
|
|
|
|
// compileAndRun is called.
|
2018-05-17 09:47:09 -04:00
|
|
|
export class FileModule {
|
|
|
|
scriptVersion: string = undefined;
|
|
|
|
readonly exports = {};
|
|
|
|
|
|
|
|
private static readonly map = new Map<string, FileModule>();
|
2018-05-19 04:47:40 -04:00
|
|
|
constructor(
|
|
|
|
readonly fileName: string,
|
|
|
|
readonly sourceCode = "",
|
|
|
|
public outputCode = ""
|
|
|
|
) {
|
2018-05-26 15:21:15 -04:00
|
|
|
util.assert(
|
|
|
|
!FileModule.map.has(fileName),
|
|
|
|
`FileModule.map already has ${fileName}`
|
|
|
|
);
|
2018-05-17 09:47:09 -04:00
|
|
|
FileModule.map.set(fileName, this);
|
2018-05-19 04:47:40 -04:00
|
|
|
if (outputCode !== "") {
|
|
|
|
this.scriptVersion = "1";
|
|
|
|
}
|
2018-05-17 09:47:09 -04:00
|
|
|
}
|
|
|
|
|
2018-05-19 04:47:40 -04:00
|
|
|
compileAndRun(): void {
|
2018-05-17 09:47:09 -04:00
|
|
|
if (!this.outputCode) {
|
|
|
|
// If there is no cached outputCode, the compile the code.
|
2018-05-19 04:47:40 -04:00
|
|
|
util.assert(
|
|
|
|
this.sourceCode != null && this.sourceCode.length > 0,
|
|
|
|
`Have no source code from ${this.fileName}`
|
|
|
|
);
|
2018-05-17 09:47:09 -04:00
|
|
|
const compiler = Compiler.instance();
|
|
|
|
this.outputCode = compiler.compile(this.fileName);
|
2018-05-25 17:21:06 -04:00
|
|
|
os.codeCache(this.fileName, this.sourceCode, this.outputCode);
|
2018-05-17 09:47:09 -04:00
|
|
|
}
|
|
|
|
util.log("compileAndRun", this.sourceCode);
|
|
|
|
execute(this.fileName, this.outputCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
static load(fileName: string): FileModule {
|
2018-05-19 04:47:40 -04:00
|
|
|
return this.map.get(fileName);
|
2018-05-17 09:47:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static getScriptsWithSourceCode(): string[] {
|
|
|
|
const out = [];
|
|
|
|
for (const fn of this.map.keys()) {
|
|
|
|
const m = this.map.get(fn);
|
|
|
|
if (m.sourceCode) {
|
|
|
|
out.push(fn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function makeDefine(fileName: string): AmdDefine {
|
|
|
|
const localDefine = (deps: string[], factory: AmdFactory): void => {
|
|
|
|
const localRequire = (x: string) => {
|
|
|
|
log("localRequire", x);
|
|
|
|
};
|
|
|
|
const currentModule = FileModule.load(fileName);
|
|
|
|
const localExports = currentModule.exports;
|
|
|
|
log("localDefine", fileName, deps, localExports);
|
|
|
|
const args = deps.map(dep => {
|
|
|
|
if (dep === "require") {
|
|
|
|
return localRequire;
|
|
|
|
} else if (dep === "exports") {
|
|
|
|
return localExports;
|
2018-05-30 12:47:26 -04:00
|
|
|
} else if (dep === "typescript") {
|
|
|
|
return ts;
|
2018-05-23 16:45:01 -04:00
|
|
|
} else if (dep === "deno") {
|
|
|
|
return deno;
|
2018-05-17 09:47:09 -04:00
|
|
|
} else {
|
2018-05-18 20:39:20 -04:00
|
|
|
const resolved = resolveModuleName(dep, fileName);
|
|
|
|
const depModule = FileModule.load(resolved);
|
2018-05-17 09:47:09 -04:00
|
|
|
depModule.compileAndRun();
|
|
|
|
return depModule.exports;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
factory(...args);
|
|
|
|
};
|
|
|
|
return localDefine;
|
|
|
|
}
|
|
|
|
|
2018-05-19 04:47:40 -04:00
|
|
|
export function resolveModule(
|
|
|
|
moduleSpecifier: string,
|
|
|
|
containingFile: string
|
2018-05-26 15:21:15 -04:00
|
|
|
): null | FileModule {
|
|
|
|
//util.log("resolveModule", { moduleSpecifier, containingFile });
|
2018-05-19 05:38:51 -04:00
|
|
|
util.assert(moduleSpecifier != null && moduleSpecifier.length > 0);
|
2018-05-19 04:47:40 -04:00
|
|
|
// We ask golang to sourceCodeFetch. It will load the sourceCode and if
|
|
|
|
// there is any outputCode cached, it will return that as well.
|
2018-05-30 13:30:06 -04:00
|
|
|
let fetchResponse;
|
|
|
|
try {
|
|
|
|
fetchResponse = os.codeFetch(moduleSpecifier, containingFile);
|
2018-06-01 14:33:16 -04:00
|
|
|
} catch (e) {
|
2018-05-30 13:30:06 -04:00
|
|
|
// TODO Only catch "no such file or directory" errors. Need error codes.
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const { filename, sourceCode, outputCode } = fetchResponse;
|
2018-05-27 03:46:18 -04:00
|
|
|
if (sourceCode.length === 0) {
|
2018-05-26 15:21:15 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
util.log("resolveModule sourceCode length ", sourceCode.length);
|
2018-05-27 03:46:18 -04:00
|
|
|
const m = FileModule.load(filename);
|
2018-05-26 15:21:15 -04:00
|
|
|
if (m != null) {
|
|
|
|
return m;
|
|
|
|
} else {
|
|
|
|
return new FileModule(filename, sourceCode, outputCode);
|
|
|
|
}
|
2018-05-19 04:47:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function resolveModuleName(
|
|
|
|
moduleSpecifier: string,
|
|
|
|
containingFile: string
|
2018-05-30 13:30:06 -04:00
|
|
|
): string | undefined {
|
2018-05-19 04:47:40 -04:00
|
|
|
const mod = resolveModule(moduleSpecifier, containingFile);
|
2018-05-30 13:30:06 -04:00
|
|
|
if (mod) {
|
|
|
|
return mod.fileName;
|
|
|
|
} else {
|
|
|
|
return undefined;
|
|
|
|
}
|
2018-05-17 09:47:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function execute(fileName: string, outputCode: string): void {
|
|
|
|
util.assert(outputCode && outputCode.length > 0);
|
2018-05-21 18:53:53 -04:00
|
|
|
_global["define"] = makeDefine(fileName);
|
2018-05-19 07:06:23 -04:00
|
|
|
outputCode += "\n//# sourceURL=" + fileName;
|
2018-05-21 18:53:53 -04:00
|
|
|
globalEval(outputCode);
|
|
|
|
_global["define"] = null;
|
2018-05-17 09:47:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is a singleton class. Use Compiler.instance() to access.
|
|
|
|
class Compiler {
|
|
|
|
options: ts.CompilerOptions = {
|
|
|
|
allowJs: true,
|
|
|
|
module: ts.ModuleKind.AMD,
|
2018-05-19 07:06:23 -04:00
|
|
|
outDir: "$deno$",
|
|
|
|
inlineSourceMap: true,
|
2018-05-26 15:21:15 -04:00
|
|
|
lib: ["es2017"],
|
|
|
|
inlineSources: true,
|
|
|
|
target: ts.ScriptTarget.ES2017
|
2018-05-17 09:47:09 -04:00
|
|
|
};
|
|
|
|
/*
|
|
|
|
allowJs: true,
|
|
|
|
module: ts.ModuleKind.AMD,
|
|
|
|
noEmit: false,
|
|
|
|
outDir: '$deno$',
|
|
|
|
*/
|
|
|
|
private service: ts.LanguageService;
|
|
|
|
|
|
|
|
private constructor() {
|
|
|
|
const host = new TypeScriptHost(this.options);
|
|
|
|
this.service = ts.createLanguageService(host);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static _instance: Compiler;
|
|
|
|
static instance(): Compiler {
|
|
|
|
return this._instance || (this._instance = new this());
|
|
|
|
}
|
|
|
|
|
|
|
|
compile(fileName: string): string {
|
|
|
|
const output = this.service.getEmitOutput(fileName);
|
|
|
|
|
|
|
|
// Get the relevant diagnostics - this is 3x faster than
|
|
|
|
// `getPreEmitDiagnostics`.
|
|
|
|
const diagnostics = this.service
|
|
|
|
.getCompilerOptionsDiagnostics()
|
|
|
|
.concat(this.service.getSyntacticDiagnostics(fileName))
|
|
|
|
.concat(this.service.getSemanticDiagnostics(fileName));
|
|
|
|
if (diagnostics.length > 0) {
|
2018-05-18 12:18:42 -04:00
|
|
|
const errMsg = ts.formatDiagnosticsWithColorAndContext(
|
|
|
|
diagnostics,
|
|
|
|
formatDiagnosticsHost
|
|
|
|
);
|
|
|
|
console.log(errMsg);
|
|
|
|
os.exit(1);
|
2018-05-17 09:47:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
util.assert(!output.emitSkipped);
|
|
|
|
|
|
|
|
const outputCode = output.outputFiles[0].text;
|
|
|
|
// let sourceMapCode = output.outputFiles[0].text;
|
|
|
|
return outputCode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the compiler host for type checking.
|
|
|
|
class TypeScriptHost implements ts.LanguageServiceHost {
|
|
|
|
constructor(readonly options: ts.CompilerOptions) {}
|
|
|
|
|
|
|
|
getScriptFileNames(): string[] {
|
|
|
|
const keys = FileModule.getScriptsWithSourceCode();
|
|
|
|
util.log("getScriptFileNames", keys);
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
|
|
|
getScriptVersion(fileName: string): string {
|
|
|
|
util.log("getScriptVersion", fileName);
|
|
|
|
const m = FileModule.load(fileName);
|
|
|
|
return m.scriptVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
getScriptSnapshot(fileName: string): ts.IScriptSnapshot | undefined {
|
|
|
|
util.log("getScriptSnapshot", fileName);
|
2018-05-26 15:21:15 -04:00
|
|
|
const m = resolveModule(fileName, ".");
|
|
|
|
if (m == null) {
|
|
|
|
util.log("getScriptSnapshot", fileName, "NOT FOUND");
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
//const m = resolveModule(fileName, ".");
|
2018-05-19 04:47:40 -04:00
|
|
|
util.assert(m.sourceCode.length > 0);
|
|
|
|
return ts.ScriptSnapshot.fromString(m.sourceCode);
|
2018-05-17 09:47:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fileExists(fileName: string): boolean {
|
2018-05-26 15:21:15 -04:00
|
|
|
const m = resolveModule(fileName, ".");
|
|
|
|
const exists = m != null;
|
|
|
|
util.log("fileExist", fileName, exists);
|
|
|
|
return exists;
|
2018-05-17 09:47:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
readFile(path: string, encoding?: string): string | undefined {
|
|
|
|
util.log("readFile", path);
|
|
|
|
throw Error("not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
getNewLine() {
|
|
|
|
return EOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
getCurrentDirectory() {
|
|
|
|
util.log("getCurrentDirectory");
|
|
|
|
return ".";
|
|
|
|
}
|
|
|
|
|
|
|
|
getCompilationSettings() {
|
|
|
|
util.log("getCompilationSettings");
|
|
|
|
return this.options;
|
|
|
|
}
|
|
|
|
|
|
|
|
getDefaultLibFileName(options: ts.CompilerOptions): string {
|
2018-05-19 04:47:40 -04:00
|
|
|
const fn = ts.getDefaultLibFileName(options);
|
2018-05-26 15:21:15 -04:00
|
|
|
util.log("getDefaultLibFileName", fn);
|
2018-05-19 04:47:40 -04:00
|
|
|
const m = resolveModule(fn, "/$asset$/");
|
|
|
|
return m.fileName;
|
2018-05-17 09:47:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
resolveModuleNames(
|
|
|
|
moduleNames: string[],
|
|
|
|
containingFile: string,
|
|
|
|
reusedNames?: string[]
|
|
|
|
): Array<ts.ResolvedModule | undefined> {
|
2018-05-26 15:21:15 -04:00
|
|
|
//util.log("resolveModuleNames", { moduleNames, reusedNames });
|
2018-05-17 09:47:09 -04:00
|
|
|
return moduleNames.map((name: string) => {
|
2018-05-23 16:45:01 -04:00
|
|
|
let resolvedFileName;
|
|
|
|
if (name === "deno") {
|
|
|
|
resolvedFileName = resolveModuleName("deno.d.ts", "/$asset$/");
|
2018-05-30 12:47:26 -04:00
|
|
|
} else if (name === "typescript") {
|
|
|
|
resolvedFileName = resolveModuleName("typescript.d.ts", "/$asset$/");
|
2018-05-23 16:45:01 -04:00
|
|
|
} else {
|
|
|
|
resolvedFileName = resolveModuleName(name, containingFile);
|
2018-05-30 13:30:06 -04:00
|
|
|
if (resolvedFileName == null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2018-05-23 16:45:01 -04:00
|
|
|
}
|
2018-05-18 20:39:20 -04:00
|
|
|
const isExternalLibraryImport = false;
|
|
|
|
return { resolvedFileName, isExternalLibraryImport };
|
2018-05-17 09:47:09 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-05-18 12:18:42 -04:00
|
|
|
|
|
|
|
const formatDiagnosticsHost: ts.FormatDiagnosticsHost = {
|
|
|
|
getCurrentDirectory(): string {
|
|
|
|
return ".";
|
|
|
|
},
|
|
|
|
getCanonicalFileName(fileName: string): string {
|
|
|
|
return fileName;
|
|
|
|
},
|
|
|
|
getNewLine(): string {
|
|
|
|
return EOL;
|
|
|
|
}
|
|
|
|
};
|