diff --git a/.gitignore b/.gitignore index 3c3629e..f06235c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +dist diff --git a/package.json b/package.json index c79986e..6b596c8 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,18 @@ { "name": "@valtown/deno-ata", "version": "1.0.0", + "type": "module", "description": "An ATA fork for Deno’s import behavior", "main": "index.js", "scripts": { "test": "vitest run", "lint": "biome lint", - "tsc": "tsc --noEmit" + "tsc": "tsc --noEmit", + "build": "tsc --build" }, + "files": [ + "dist" + ], "keywords": [ "typescript", "ata", diff --git a/src/index.ts b/src/index.ts index 60907d2..f055149 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,8 @@ -import { Sentry } from "../sentry.worker"; import { loadDependency } from "./loadDependency"; import { extractReferences } from "./extractReferences"; import { resolveImportSpecifier } from "./resolveSpecifiers"; -import { VirtualTypeScriptEnvironment } from "../vfs/vfs_utils"; -import { urlToFilepath } from "app/lib/tsUrlInterop"; +import type { VirtualTypeScriptEnvironment } from "./vfs_utils"; +import { urlToFilepath } from "./tsUrlInterop"; const NODE_FILE_PATH = "node_types.d.ts"; const NODE_FILE = `/// \n`; @@ -26,8 +25,8 @@ export function setupTypeAcquisition( let downloaded = 0; // Used for debugging - let debugLinks: [string, string][] = []; - let debugIds = new Map(); + const debugLinks: [string, string][] = []; + const debugIds = new Map(); let debugCounter = 0; function getDebugId(url: string) { @@ -56,7 +55,7 @@ ${debugLinks .join("\n")} `)*/ } catch (e) { - Sentry.captureException(e); + console.error(e); } }, /** @@ -102,7 +101,7 @@ ${debugLinks if (downloadedPaths.has(resolvedUrl.url)) return; downloadedPaths.add(resolvedUrl.url); total++; - let dep = await loadDependency(resolvedUrl.url); + const dep = await loadDependency(resolvedUrl.url); downloaded++; if (dep === null) { console.error(`Failed to get ${importSpecifier}`); diff --git a/src/tsUrlInterop.ts b/src/tsUrlInterop.ts new file mode 100644 index 0000000..36be2ac --- /dev/null +++ b/src/tsUrlInterop.ts @@ -0,0 +1,22 @@ +export function ensureTsExtension(fileName: string) { + // If we're looking at an HTTP import that doesn't have + // an extension like cjs, mjs, jsx, tsx, or ts, + // add one. + if (!fileName.match(/\.(t|[mc]?j)sx?$/)) { + return `${fileName}.tsx`; + } + return fileName; +} + +/** + * All right, so as other comments talk about, Deno uses https:// + * imports and TypeScript doesn't support them. TypeScript doesn't + * support setting a 'file' with a URL as the path. + * + * Note: this does normalize both https and http to https, but + * it doesn't matter that much: in reality, we are never + * going to be loading http content. + */ +export function urlToFilepath(path: string) { + return ensureTsExtension(path.replace(/https?:\/\//, "/https/")); +} diff --git a/src/vfs_utils.ts b/src/vfs_utils.ts new file mode 100644 index 0000000..6414a8d --- /dev/null +++ b/src/vfs_utils.ts @@ -0,0 +1,15 @@ +import type { System } from "typescript"; + +export interface VirtualTypeScriptEnvironment { + sys: System; + languageService: import("typescript").LanguageService; + getSourceFile: ( + fileName: string, + ) => import("typescript").SourceFile | undefined; + createFile: (fileName: string, content: string) => void; + updateFile: ( + fileName: string, + content: string, + replaceTextSpan?: import("typescript").TextSpan, + ) => void; +} diff --git a/tsconfig.json b/tsconfig.json index 9084afe..3d25f65 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,15 +6,16 @@ "forceConsistentCasingInFileNames": true, "inlineSources": true, "jsx": "react", - "module": "nodenext", - "moduleResolution": "nodenext", + "module": "esnext", + "moduleResolution": "bundler", "noUncheckedIndexedAccess": true, "resolveJsonModule": true, "skipLibCheck": true, "sourceMap": true, "strict": true, "target": "es2022", - "rootDir": "./src" + "rootDir": "./src", + "outDir": "./dist" }, - "exclude": ["./demo"] + "exclude": ["./demo", "./dist"] }