summaryrefslogtreecommitdiff
path: root/cli/js
diff options
context:
space:
mode:
Diffstat (limited to 'cli/js')
-rw-r--r--cli/js/compiler.ts17
-rw-r--r--cli/js/compiler_bootstrap.ts34
-rw-r--r--cli/js/compiler_bundler.ts14
-rw-r--r--cli/js/compiler_host.ts10
-rw-r--r--cli/js/compiler_sourcefile.ts2
-rw-r--r--cli/js/dispatch.ts5
-rw-r--r--cli/js/dispatch_json.ts2
7 files changed, 59 insertions, 25 deletions
diff --git a/cli/js/compiler.ts b/cli/js/compiler.ts
index 3cebf24b2..54861f713 100644
--- a/cli/js/compiler.ts
+++ b/cli/js/compiler.ts
@@ -6,6 +6,7 @@ import "./globals.ts";
import "./ts_global.d.ts";
import { TranspileOnlyResult } from "./compiler_api.ts";
+import { oldProgram } from "./compiler_bootstrap.ts";
import { setRootExports } from "./compiler_bundler.ts";
import {
defaultBundlerOptions,
@@ -142,7 +143,12 @@ self.bootstrapTsCompiler = function tsCompilerMain(): void {
// 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);
+ const program = ts.createProgram({
+ rootNames,
+ options,
+ host,
+ oldProgram
+ });
diagnostics = ts
.getPreEmitDiagnostics(program)
@@ -220,11 +226,12 @@ self.bootstrapTsCompiler = function tsCompilerMain(): void {
}
host.mergeOptions(...compilerOptions);
- const program = ts.createProgram(
+ const program = ts.createProgram({
rootNames,
- host.getCompilationSettings(),
- host
- );
+ options: host.getCompilationSettings(),
+ host,
+ oldProgram
+ });
if (bundle) {
setRootExports(program, rootNames[0]);
diff --git a/cli/js/compiler_bootstrap.ts b/cli/js/compiler_bootstrap.ts
new file mode 100644
index 000000000..6de978750
--- /dev/null
+++ b/cli/js/compiler_bootstrap.ts
@@ -0,0 +1,34 @@
+// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+
+import { ASSETS, Host } from "./compiler_host.ts";
+import { core } from "./core.ts";
+import * as dispatch from "./dispatch.ts";
+import { sendSync } from "./dispatch_json.ts";
+
+// This registers ops that are available during the snapshotting process.
+const ops = core.ops();
+for (const [name, opId] of Object.entries(ops)) {
+ const opName = `OP_${name.toUpperCase()}`;
+ // TODO This type casting is dangerous, and should be improved when the same
+ // code in `os.ts` is done.
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ (dispatch as any)[opName] = opId;
+}
+
+const host = new Host({ writeFile(): void {} });
+const options = host.getCompilationSettings();
+
+/** Used to generate the foundational AST for all other compilations, so it can
+ * be cached as part of the snapshot and available to speed up startup */
+export const oldProgram = ts.createProgram({
+ rootNames: [`${ASSETS}/bootstrap.ts`],
+ options,
+ host
+});
+
+/** A module loader which is concatenated into bundle files. We read all static
+ * assets during the snapshotting process, which is why this is located in
+ * compiler_bootstrap. */
+export const bundleLoader = sendSync(dispatch.OP_FETCH_ASSET, {
+ name: "bundle_loader.js"
+});
diff --git a/cli/js/compiler_bundler.ts b/cli/js/compiler_bundler.ts
index a4e4557ca..8fb68cc7a 100644
--- a/cli/js/compiler_bundler.ts
+++ b/cli/js/compiler_bundler.ts
@@ -1,7 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
-import * as dispatch from "./dispatch.ts";
-import { sendSync } from "./dispatch_json.ts";
+import { bundleLoader } from "./compiler_bootstrap.ts";
import {
assert,
commonPath,
@@ -9,11 +8,6 @@ import {
CHAR_FORWARD_SLASH
} from "./util.ts";
-const BUNDLE_LOADER = "bundle_loader.js";
-
-/** A loader of bundled modules that we will inline into any bundle outputs. */
-let bundleLoader: string;
-
/** Local state of what the root exports are of a root module. */
let rootExports: string[] | undefined;
@@ -40,12 +34,6 @@ export function buildBundle(
data: string,
sourceFiles: readonly ts.SourceFile[]
): string {
- // we can only do this once we are bootstrapped and easiest way to do it is
- // inline here
- if (!bundleLoader) {
- bundleLoader = sendSync(dispatch.OP_FETCH_ASSET, { name: BUNDLE_LOADER });
- }
-
// when outputting to AMD and a single outfile, TypeScript makes up the module
// specifiers which are used to define the modules, and doesn't expose them
// publicly, so we have to try to replicate
diff --git a/cli/js/compiler_host.ts b/cli/js/compiler_host.ts
index f8921a352..3e6df4485 100644
--- a/cli/js/compiler_host.ts
+++ b/cli/js/compiler_host.ts
@@ -18,7 +18,7 @@ export interface ConfigureResponse {
diagnostics?: ts.Diagnostic[];
}
-const ASSETS = "$asset$";
+export const ASSETS = "$asset$";
/** Options that need to be used when generating a bundle (either trusted or
* runtime). */
@@ -129,11 +129,11 @@ export class Host implements ts.CompilerHost {
private _writeFile: WriteFileCallback;
private _getAsset(filename: string): SourceFile {
- const sourceFile = SourceFile.get(filename);
+ const url = filename.split("/").pop()!;
+ const sourceFile = SourceFile.get(url);
if (sourceFile) {
return sourceFile;
}
- const url = filename.split("/").pop()!;
const name = url.includes(".") ? url : `${url}.d.ts`;
const sourceCode = sendSync(dispatch.OP_FETCH_ASSET, { name });
return new SourceFile({
@@ -238,13 +238,15 @@ export class Host implements ts.CompilerHost {
: SourceFile.get(fileName);
assert(sourceFile != null);
if (!sourceFile.tsSourceFile) {
+ assert(sourceFile.sourceCode != null);
sourceFile.tsSourceFile = ts.createSourceFile(
fileName,
sourceFile.sourceCode,
languageVersion
);
+ delete sourceFile.sourceCode;
}
- return sourceFile!.tsSourceFile;
+ return sourceFile.tsSourceFile;
} catch (e) {
if (onError) {
onError(String(e));
diff --git a/cli/js/compiler_sourcefile.ts b/cli/js/compiler_sourcefile.ts
index 087891c99..8e81cdb45 100644
--- a/cli/js/compiler_sourcefile.ts
+++ b/cli/js/compiler_sourcefile.ts
@@ -61,7 +61,7 @@ export class SourceFile {
mediaType!: MediaType;
processed = false;
- sourceCode!: string;
+ sourceCode?: string;
tsSourceFile?: ts.SourceFile;
url!: string;
diff --git a/cli/js/dispatch.ts b/cli/js/dispatch.ts
index 073c32f7a..f5049cca8 100644
--- a/cli/js/dispatch.ts
+++ b/cli/js/dispatch.ts
@@ -70,12 +70,15 @@ export let OP_TRUNCATE: number;
export let OP_MAKE_TEMP_DIR: number;
export let OP_CWD: number;
export let OP_CONNECT_TLS: number;
-export let OP_FETCH_ASSET: number;
export let OP_HOSTNAME: number;
export let OP_OPEN_PLUGIN: number;
export let OP_COMPILE: number;
export let OP_TRANSPILE: number;
+/** **WARNING:** This is only available during the snapshotting process and is
+ * unavailable at runtime. */
+export let OP_FETCH_ASSET: number;
+
const PLUGIN_ASYNC_HANDLER_MAP: Map<number, AsyncHandler> = new Map();
export function setPluginAsyncHandler(
diff --git a/cli/js/dispatch_json.ts b/cli/js/dispatch_json.ts
index 104a7e564..adccb69c6 100644
--- a/cli/js/dispatch_json.ts
+++ b/cli/js/dispatch_json.ts
@@ -62,7 +62,7 @@ export function sendSync(
const resUi8 = core.dispatch(opId, argsUi8, zeroCopy);
util.assert(resUi8 != null);
- const res = decode(resUi8!);
+ const res = decode(resUi8);
util.assert(res.promiseId == null);
return unwrapResponse(res);
}