feat: support embedded import map in deno.json (#1188)
One can now inline the `imports` and `scopes` from the import map directly into deno.json. For new projects, `import_map.json` is not generated anymore. Co-authored-by: Luca Casonato <hello@lcas.dev>
This commit is contained in:
co-authored by
Luca Casonato
parent
3857a49f1c
commit
1b6678951a
+3
-3
@@ -2,8 +2,8 @@
|
||||
export {
|
||||
fromFileUrl,
|
||||
toFileUrl,
|
||||
} from "https://deno.land/std@0.189.0/path/mod.ts";
|
||||
export { escape as regexpEscape } from "https://deno.land/std@0.189.0/regexp/escape.ts";
|
||||
} from "https://deno.land/std@0.190.0/path/mod.ts";
|
||||
export { escape as regexpEscape } from "https://deno.land/std@0.190.0/regexp/escape.ts";
|
||||
|
||||
// -- esbuild --
|
||||
// @deno-types="https://deno.land/x/esbuild@v0.17.11/mod.d.ts"
|
||||
@@ -17,4 +17,4 @@ const esbuild: typeof esbuildWasm = Deno.run === undefined
|
||||
const esbuildWasmURL = new URL("./esbuild_v0.17.11.wasm", import.meta.url).href;
|
||||
export { esbuild, esbuildWasm as esbuildTypes, esbuildWasmURL };
|
||||
|
||||
export { denoPlugins } from "https://deno.land/x/esbuild_deno_loader@0.7.0/mod.ts";
|
||||
export { denoPlugins } from "https://deno.land/x/esbuild_deno_loader@0.7.2/mod.ts";
|
||||
|
||||
@@ -16,8 +16,8 @@ export interface EsbuildBuilderOptions {
|
||||
entrypoints: Record<string, string>;
|
||||
/** Whether or not this is a dev build. */
|
||||
dev: boolean;
|
||||
/** The import map URL. */
|
||||
importMapURL: URL;
|
||||
/** The path to the deno.json / deno.jsonc config file. */
|
||||
configPath: string;
|
||||
/** The JSX configuration. */
|
||||
jsxConfig: JSXConfig;
|
||||
}
|
||||
@@ -74,7 +74,7 @@ export class EsbuildBuilder implements Builder {
|
||||
|
||||
plugins: [
|
||||
buildIdPlugin(opts.buildID),
|
||||
...denoPlugins({ importMapURL: opts.importMapURL.href }),
|
||||
...denoPlugins({ configPath: opts.configPath }),
|
||||
],
|
||||
});
|
||||
|
||||
|
||||
+1
-36
@@ -1,12 +1,4 @@
|
||||
import {
|
||||
dirname,
|
||||
fromFileUrl,
|
||||
gte,
|
||||
join,
|
||||
relative,
|
||||
toFileUrl,
|
||||
walk,
|
||||
} from "./deps.ts";
|
||||
import { dirname, fromFileUrl, gte, join, toFileUrl, walk } from "./deps.ts";
|
||||
import { error } from "./error.ts";
|
||||
|
||||
const MIN_DENO_VERSION = "1.25.0";
|
||||
@@ -60,31 +52,6 @@ export async function collect(directory: string): Promise<Manifest> {
|
||||
return { routes, islands };
|
||||
}
|
||||
|
||||
async function findDenoConfig(directory: string) {
|
||||
let dir = directory;
|
||||
while (true) {
|
||||
const path = join(dir, "deno.json");
|
||||
try {
|
||||
await Deno.stat(path);
|
||||
break;
|
||||
} catch (err) {
|
||||
if (!(err instanceof Deno.errors.NotFound)) {
|
||||
throw err;
|
||||
}
|
||||
const parent = dirname(dir);
|
||||
if (parent === dir) {
|
||||
error(
|
||||
`Could not find a deno.json file in the current directory or any parent directory.`,
|
||||
);
|
||||
}
|
||||
dir = parent;
|
||||
}
|
||||
}
|
||||
|
||||
// convert into import path, would be cool if there is a stdlib function
|
||||
return `${relative(directory, dir).replace(/\\/g, "/") || "."}/deno.json`;
|
||||
}
|
||||
|
||||
export async function generate(directory: string, manifest: Manifest) {
|
||||
const { routes, islands } = manifest;
|
||||
|
||||
@@ -92,7 +59,6 @@ export async function generate(directory: string, manifest: Manifest) {
|
||||
// This file SHOULD be checked into source version control.
|
||||
// This file is automatically updated during development when running \`dev.ts\`.
|
||||
|
||||
import config from "${await findDenoConfig(directory)}" assert { type: "json" };
|
||||
${
|
||||
routes.map((file, i) => `import * as $${i} from "./routes${file}";`).join(
|
||||
"\n",
|
||||
@@ -117,7 +83,6 @@ const manifest = {
|
||||
}
|
||||
},
|
||||
baseUrl: import.meta.url,
|
||||
config,
|
||||
};
|
||||
|
||||
export default manifest;
|
||||
|
||||
+50
-8
@@ -1,7 +1,10 @@
|
||||
import {
|
||||
ConnInfo,
|
||||
dirname,
|
||||
extname,
|
||||
fromFileUrl,
|
||||
join,
|
||||
JSONC,
|
||||
RequestHandler,
|
||||
Status,
|
||||
toFileUrl,
|
||||
@@ -10,7 +13,7 @@ import {
|
||||
} from "./deps.ts";
|
||||
import { h } from "preact";
|
||||
import * as router from "./router.ts";
|
||||
import { Manifest } from "./mod.ts";
|
||||
import { DenoConfig, Manifest } from "./mod.ts";
|
||||
import { ALIVE_URL, JS_PREFIX, REFRESH_JS_URL } from "./constants.ts";
|
||||
import { BUILD_ID } from "./build_id.ts";
|
||||
import DefaultErrorHandler from "./default_error_page.ts";
|
||||
@@ -47,6 +50,12 @@ interface RouterState {
|
||||
state: Record<string, unknown>;
|
||||
}
|
||||
|
||||
function isObject(value: unknown) {
|
||||
return typeof value === "object" &&
|
||||
!Array.isArray(value) &&
|
||||
value !== null;
|
||||
}
|
||||
|
||||
interface StaticFile {
|
||||
/** The URL to the static file on disk. */
|
||||
localUrl: URL;
|
||||
@@ -83,7 +92,7 @@ export class ServerContext {
|
||||
notFound: UnknownPage,
|
||||
error: ErrorPage,
|
||||
plugins: Plugin[],
|
||||
importMapURL: URL,
|
||||
configPath: string,
|
||||
jsxConfig: JSXConfig,
|
||||
) {
|
||||
this.#routes = routes;
|
||||
@@ -99,7 +108,7 @@ export class ServerContext {
|
||||
this.#builder = new EsbuildBuilder({
|
||||
buildID: BUILD_ID,
|
||||
entrypoints: collectEntrypoints(this.#dev, this.#islands, this.#plugins),
|
||||
importMapURL,
|
||||
configPath,
|
||||
dev: this.#dev,
|
||||
jsxConfig,
|
||||
});
|
||||
@@ -115,11 +124,14 @@ export class ServerContext {
|
||||
// Get the manifest' base URL.
|
||||
const baseUrl = new URL("./", manifest.baseUrl).href;
|
||||
|
||||
const config = manifest.config || { importMap: "./import_map.json" };
|
||||
if (typeof config.importMap !== "string") {
|
||||
throw new Error("deno.json must contain an 'importMap' property.");
|
||||
const { config, path: configPath } = await readDenoConfig(
|
||||
fromFileUrl(baseUrl),
|
||||
);
|
||||
if (typeof config.importMap !== "string" && !isObject(config.imports)) {
|
||||
throw new Error(
|
||||
"deno.json must contain an 'importMap' or 'imports' property.",
|
||||
);
|
||||
}
|
||||
const importMapURL = new URL(config.importMap, manifest.baseUrl);
|
||||
|
||||
config.compilerOptions ??= {};
|
||||
|
||||
@@ -318,7 +330,7 @@ export class ServerContext {
|
||||
notFound,
|
||||
error,
|
||||
opts.plugins ?? [],
|
||||
importMapURL,
|
||||
configPath,
|
||||
jsxConfig,
|
||||
);
|
||||
}
|
||||
@@ -937,3 +949,33 @@ function collectEntrypoints(
|
||||
|
||||
return entryPoints;
|
||||
}
|
||||
|
||||
async function readDenoConfig(
|
||||
directory: string,
|
||||
): Promise<{ config: DenoConfig; path: string }> {
|
||||
let dir = directory;
|
||||
while (true) {
|
||||
for (const name of ["deno.json", "deno.jsonc"]) {
|
||||
const path = join(dir, name);
|
||||
try {
|
||||
const file = await Deno.readTextFile(path);
|
||||
if (name.endsWith(".jsonc")) {
|
||||
return { config: JSONC.parse(file) as DenoConfig, path };
|
||||
} else {
|
||||
return { config: JSON.parse(file), path };
|
||||
}
|
||||
} catch (err) {
|
||||
if (!(err instanceof Deno.errors.NotFound)) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
const parent = dirname(dir);
|
||||
if (parent === dir) {
|
||||
throw new Error(
|
||||
`Could not find a deno.json file in the current directory or any parent directory.`,
|
||||
);
|
||||
}
|
||||
dir = parent;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
// -- std --
|
||||
export {
|
||||
dirname,
|
||||
extname,
|
||||
fromFileUrl,
|
||||
join,
|
||||
toFileUrl,
|
||||
} from "https://deno.land/std@0.190.0/path/mod.ts";
|
||||
export { walk } from "https://deno.land/std@0.190.0/fs/walk.ts";
|
||||
@@ -17,6 +19,7 @@ export {
|
||||
} from "https://deno.land/std@0.190.0/media_types/mod.ts";
|
||||
export { toHashString } from "https://deno.land/std@0.190.0/crypto/to_hash_string.ts";
|
||||
export { escape } from "https://deno.land/std@0.190.0/regexp/escape.ts";
|
||||
export * as JSONC from "https://deno.land/std@0.190.0/jsonc/mod.ts";
|
||||
|
||||
// -- esbuild --
|
||||
// @deno-types="https://deno.land/x/esbuild@v0.17.11/mod.d.ts"
|
||||
|
||||
+2
-2
@@ -47,11 +47,11 @@ export interface Manifest {
|
||||
>;
|
||||
islands: Record<string, IslandModule>;
|
||||
baseUrl: string;
|
||||
config?: DenoConfig;
|
||||
}
|
||||
|
||||
export interface DenoConfig {
|
||||
importMap: string;
|
||||
imports?: Record<string, string>;
|
||||
importMap?: string;
|
||||
compilerOptions?: {
|
||||
jsx?: string;
|
||||
jsxImportSource?: string;
|
||||
|
||||
Reference in New Issue
Block a user