Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 8bab80a

Browse files
committed
Clean up
1 parent b1d539f commit 8bab80a

File tree

6 files changed

+32
-38
lines changed

6 files changed

+32
-38
lines changed

cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
22
import { readLines } from "https://deno.land/[email protected]/io/mod.ts";
33
import { writeAll } from "https://deno.land/[email protected]/streams/conversion.ts";
44
import { basename, resolve } from "https://deno.land/[email protected]/path/mod.ts";
5-
import { readImportMap } from "./server/helpers.ts";
5+
import { parseImportMap } from "./server/helpers.ts";
66
import { findFile } from "./lib/fs.ts";
77
import log, { bold, dim, stripColor } from "./lib/log.ts";
88
import { serveDir } from "./lib/serve.ts";
@@ -112,7 +112,7 @@ async function main() {
112112
if (runOptions.importMapFile) {
113113
try {
114114
let update: boolean | null = null;
115-
const importMap = await readImportMap(runOptions.importMapFile);
115+
const importMap = await parseImportMap(runOptions.importMapFile);
116116
for (const key in importMap.imports) {
117117
const url = importMap.imports[key];
118118
if (

server/helpers.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,7 @@ import { createGenerator } from "https://esm.sh/@unocss/[email protected]";
55
import log from "../lib/log.ts";
66
import util from "../lib/util.ts";
77
import { isCanary, VERSION } from "../version.ts";
8-
import type { AlephConfig, ImportMap, ModuleLoader } from "./types.ts";
9-
10-
export type JSXConfig = {
11-
jsxRuntime?: "react" | "preact";
12-
jsxImportSource?: string;
13-
jsxRuntimeVersion?: string;
14-
jsxRuntimeCdnVersion?: string;
15-
};
8+
import type { AlephConfig, ImportMap, JSXConfig, ModuleLoader } from "./types.ts";
169

1710
export const regFullVersion = /@\d+\.\d+\.\d+/;
1811
export const builtinModuleExts = ["tsx", "ts", "mts", "jsx", "js", "mjs"];
@@ -131,7 +124,7 @@ export async function loadJSXConfig(importMap: ImportMap): Promise<JSXConfig> {
131124
}
132125
}
133126

134-
let fuzzReactUrl: string | null = null;
127+
let fuzzRuntimeUrl: string | null = null;
135128

136129
for (const url of Object.values(importMap.imports)) {
137130
let m = url.match(/^https?:\/\/esm\.sh\/(p?react)@(\d+\.\d+\.\d+(-[a-z\d.]+)*)(\?|$)/);
@@ -152,16 +145,16 @@ export async function loadJSXConfig(importMap: ImportMap): Promise<JSXConfig> {
152145
jsxConfig.jsxImportSource = `https://esm.sh/${jsxConfig.jsxRuntime}@${m[2]}`;
153146
}
154147
} else {
155-
fuzzReactUrl = url;
148+
fuzzRuntimeUrl = url;
156149
}
157150
break;
158151
}
159152
}
160153

161154
// get acctual react version from esm.sh
162-
if (fuzzReactUrl) {
155+
if (fuzzRuntimeUrl) {
163156
log.info(`Checking ${jsxConfig.jsxRuntime} version...`);
164-
const text = await fetch(fuzzReactUrl).then((resp) => resp.text());
157+
const text = await fetch(fuzzRuntimeUrl).then((resp) => resp.text());
165158
const m = text.match(/https?:\/\/cdn\.esm\.sh\/(v\d+)\/p?react@(\d+\.\d+\.\d+(-[a-z\d.]+)*)\//);
166159
if (m) {
167160
jsxConfig.jsxRuntimeCdnVersion = m[1].slice(1);
@@ -182,7 +175,7 @@ export async function loadImportMap(): Promise<ImportMap> {
182175
if (Deno.env.get("ALEPH_DEV")) {
183176
const alephPkgUri = Deno.env.get("ALEPH_PKG_URI") || `http://localhost:${Deno.env.get("ALEPH_DEV_PORT")}`;
184177
const importMapFile = join(Deno.env.get("ALEPH_DEV_ROOT")!, "import_map.json");
185-
const { __filename, imports, scopes } = await readImportMap(importMapFile);
178+
const { __filename, imports, scopes } = await parseImportMap(importMapFile);
186179
Object.assign(importMap, {
187180
__filename,
188181
imports: {
@@ -202,7 +195,7 @@ export async function loadImportMap(): Promise<ImportMap> {
202195
);
203196
if (importMapFile) {
204197
try {
205-
const { __filename, imports, scopes } = await readImportMap(importMapFile);
198+
const { __filename, imports, scopes } = await parseImportMap(importMapFile);
206199
Object.assign(importMap, { __filename });
207200
Object.assign(importMap.imports, imports);
208201
Object.assign(importMap.scopes, scopes);
@@ -219,6 +212,7 @@ export async function initModuleLoaders(importMap: ImportMap): Promise<ModuleLoa
219212
if (loaders.length > 0) {
220213
return loaders;
221214
}
215+
// only init loaders in `CLI` mode
222216
if (Deno.env.get("ALEPH_CLI")) {
223217
for (const key in importMap.imports) {
224218
if (/^\*\.[a-z0-9]+$/i.test(key)) {
@@ -263,7 +257,7 @@ export async function parseJSONFile(jsonFile: string): Promise<Record<string, un
263257
return JSON.parse(raw);
264258
}
265259

266-
export async function readImportMap(importMapFile: string): Promise<ImportMap> {
260+
export async function parseImportMap(importMapFile: string): Promise<ImportMap> {
267261
const importMap: ImportMap = { __filename: importMapFile, imports: {}, scopes: {} };
268262
const data = await parseJSONFile(importMapFile);
269263
const imports: Record<string, string> = toStringMap(data.imports);

server/transformer.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { readCode } from "../lib/fs.ts";
55
import log from "../lib/log.ts";
66
import util from "../lib/util.ts";
77
import { bundleCSS } from "./bundle_css.ts";
8-
import type { JSXConfig } from "./helpers.ts";
98
import {
109
builtinModuleExts,
1110
getAlephPkgUri,
@@ -16,7 +15,7 @@ import {
1615
} from "./helpers.ts";
1716
import { isRouteFile } from "./routing.ts";
1817
import { DependencyGraph } from "./graph.ts";
19-
import type { ImportMap, ModuleLoaderContent } from "./types.ts";
18+
import type { ImportMap, JSXConfig, ModuleLoaderContent } from "./types.ts";
2019

2120
export type TransformerOptions = {
2221
buildHash: string;

server/types.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,11 @@ export type FetchHandler = {
3232
(request: Request): Promise<Response> | Response;
3333
};
3434

35-
export type MiddlewareCallback = () => Promise<void> | void;
36-
3735
export interface Middleware {
3836
fetch(
3937
request: Request,
4038
context: Record<string, unknown>,
41-
): Promise<Response | MiddlewareCallback | void> | Response | MiddlewareCallback | void;
39+
): Promise<Response | CallableFunction | void> | Response | CallableFunction | void;
4240
}
4341

4442
export type ImportMap = {
@@ -47,6 +45,13 @@ export type ImportMap = {
4745
readonly scopes: Record<string, Record<string, string>>;
4846
};
4947

48+
export type JSXConfig = {
49+
jsxRuntime?: "react" | "preact";
50+
jsxImportSource?: string;
51+
jsxRuntimeVersion?: string;
52+
jsxRuntimeCdnVersion?: string;
53+
};
54+
5055
export type ModuleLoader = {
5156
test(pathname: string): boolean;
5257
load(pathname: string, env: ModuleLoaderEnv): Promise<ModuleLoaderContent> | ModuleLoaderContent;

types.d.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,11 @@ declare interface Data<T = unknown> {
5858
delete?(request: Request, context: Context): Promise<Response> | Response;
5959
}
6060

61-
type MiddlewareCallback = () => Promise<void> | void;
62-
6361
declare interface Middleware {
6462
fetch(
6563
request: Request,
6664
context: Context,
67-
): Promise<Response | MiddlewareCallback | void> | Response | MiddlewareCallback | void;
65+
): Promise<Response | CallableFunction | void> | Response | CallableFunction | void;
6866
}
6967

7068
declare interface ImportMeta {

version.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,16 @@ export const VERSION = "1.0.0-alpha.27";
55
export const isCanary = false;
66

77
/** `prepublish` will be invoked before publish. */
8-
export async function prepublish(): Promise<boolean> {
9-
if (!window.confirm("Build compiler wasm?")) {
10-
return true;
8+
export async function prepublish(): Promise<boolean | void> {
9+
if (window.confirm("Build compiler wasm?")) {
10+
const p = Deno.run({
11+
cmd: ["deno", "run", "-A", "build.ts"],
12+
cwd: "./compiler",
13+
stdout: "inherit",
14+
stderr: "inherit",
15+
});
16+
const { success } = await p.status();
17+
p.close();
18+
return success;
1119
}
12-
13-
const p = Deno.run({
14-
cmd: ["deno", "run", "-A", "build.ts"],
15-
cwd: "./compiler",
16-
stdout: "inherit",
17-
stderr: "inherit",
18-
});
19-
const { success } = await p.status();
20-
p.close();
21-
return success;
2220
}

0 commit comments

Comments
 (0)