Skip to content

Commit

Permalink
Generate ESM modules
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcw committed Jun 12, 2024
1 parent 7019a58 commit 8cdbbd4
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
dist
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
13 changes: 6 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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 = `/// <reference types="npm:@types/[email protected]" />\n`;
Expand All @@ -26,8 +25,8 @@ export function setupTypeAcquisition(
let downloaded = 0;

// Used for debugging
let debugLinks: [string, string][] = [];
let debugIds = new Map<string, number>();
const debugLinks: [string, string][] = [];
const debugIds = new Map<string, number>();
let debugCounter = 0;

function getDebugId(url: string) {
Expand Down Expand Up @@ -56,7 +55,7 @@ ${debugLinks
.join("\n")}
`)*/
} catch (e) {
Sentry.captureException(e);
console.error(e);
}
},
/**
Expand Down Expand Up @@ -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}`);
Expand Down
22 changes: 22 additions & 0 deletions src/tsUrlInterop.ts
Original file line number Diff line number Diff line change
@@ -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/"));
}
15 changes: 15 additions & 0 deletions src/vfs_utils.ts
Original file line number Diff line number Diff line change
@@ -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;
}
9 changes: 5 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}

0 comments on commit 8cdbbd4

Please sign in to comment.