summaryrefslogtreecommitdiff
path: root/cli/tsc/06_util.js
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2020-07-22 12:03:46 -0400
committerGitHub <noreply@github.com>2020-07-22 12:03:46 -0400
commitbf9930066d3a5d26baad22fb1766de26be49c2ae (patch)
treee6176c72fdf2d89bb3e45855aba03d8a535b599c /cli/tsc/06_util.js
parent9d13b539b5dfc96e2b5a0e89fc8e0312d6500fff (diff)
Reduce size of TypeScript Compiler snapshot (#6809)
This PR is intentionally ugly. It duplicates all of the code in cli/js2/ into cli/tsc/ ... because it's very important that we all understand that this code is unnecessarily duplicated in our binary. I hope this ugliness provides the motivation to clean it up. The typescript git submodule is removed, because it's a very large repo and contains all sorts of stuff we don't need. Instead the necessary files are copied directly into the deno repo. Hence +200k lines. COMPILER_SNAPSHOT.bin size ``` master 3448139 this branch 3320972 ``` Fixes #6812
Diffstat (limited to 'cli/tsc/06_util.js')
-rw-r--r--cli/tsc/06_util.js154
1 files changed, 154 insertions, 0 deletions
diff --git a/cli/tsc/06_util.js b/cli/tsc/06_util.js
new file mode 100644
index 000000000..086275bd8
--- /dev/null
+++ b/cli/tsc/06_util.js
@@ -0,0 +1,154 @@
+// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+
+((window) => {
+ const { build } = window.__bootstrap.build;
+ const internals = window.__bootstrap.internals;
+ let logDebug = false;
+ let logSource = "JS";
+
+ function setLogDebug(debug, source) {
+ logDebug = debug;
+ if (source) {
+ logSource = source;
+ }
+ }
+
+ function log(...args) {
+ if (logDebug) {
+ // if we destructure `console` off `globalThis` too early, we don't bind to
+ // the right console, therefore we don't log anything out.
+ globalThis.console.log(`DEBUG ${logSource} -`, ...args);
+ }
+ }
+
+ class AssertionError extends Error {
+ constructor(msg) {
+ super(msg);
+ this.name = "AssertionError";
+ }
+ }
+
+ function assert(cond, msg = "Assertion failed.") {
+ if (!cond) {
+ throw new AssertionError(msg);
+ }
+ }
+
+ function createResolvable() {
+ let resolve;
+ let reject;
+ const promise = new Promise((res, rej) => {
+ resolve = res;
+ reject = rej;
+ });
+ promise.resolve = resolve;
+ promise.reject = reject;
+ return promise;
+ }
+
+ function notImplemented() {
+ throw new Error("not implemented");
+ }
+
+ function immutableDefine(
+ o,
+ p,
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ value,
+ ) {
+ Object.defineProperty(o, p, {
+ value,
+ configurable: false,
+ writable: false,
+ });
+ }
+
+ function pathFromURLWin32(url) {
+ const hostname = url.hostname;
+ const pathname = decodeURIComponent(url.pathname.replace(/\//g, "\\"));
+
+ if (hostname !== "") {
+ //TODO(actual-size) Node adds a punycode decoding step, we should consider adding this
+ return `\\\\${hostname}${pathname}`;
+ }
+
+ const validPath = /^\\(?<driveLetter>[A-Za-z]):\\/;
+ const matches = validPath.exec(pathname);
+
+ if (!matches?.groups?.driveLetter) {
+ throw new TypeError("A URL with the file schema must be absolute.");
+ }
+
+ // we don't want a leading slash on an absolute path in Windows
+ return pathname.slice(1);
+ }
+
+ function pathFromURLPosix(url) {
+ if (url.hostname !== "") {
+ throw new TypeError(`Host must be empty.`);
+ }
+
+ return decodeURIComponent(url.pathname);
+ }
+
+ function pathFromURL(pathOrUrl) {
+ if (pathOrUrl instanceof URL) {
+ if (pathOrUrl.protocol != "file:") {
+ throw new TypeError("Must be a file URL.");
+ }
+
+ return build.os == "windows"
+ ? pathFromURLWin32(pathOrUrl)
+ : pathFromURLPosix(pathOrUrl);
+ }
+ return pathOrUrl;
+ }
+
+ internals.exposeForTest("pathFromURL", pathFromURL);
+
+ function writable(value) {
+ return {
+ value,
+ writable: true,
+ enumerable: true,
+ configurable: true,
+ };
+ }
+
+ function nonEnumerable(value) {
+ return {
+ value,
+ writable: true,
+ configurable: true,
+ };
+ }
+
+ function readOnly(value) {
+ return {
+ value,
+ enumerable: true,
+ };
+ }
+
+ function getterOnly(getter) {
+ return {
+ get: getter,
+ enumerable: true,
+ };
+ }
+
+ window.__bootstrap.util = {
+ log,
+ setLogDebug,
+ notImplemented,
+ createResolvable,
+ assert,
+ AssertionError,
+ immutableDefine,
+ pathFromURL,
+ writable,
+ nonEnumerable,
+ readOnly,
+ getterOnly,
+ };
+})(this);