more prep

This commit is contained in:
Seth Trowbridge 2024-08-13 10:25:16 -04:00
parent 2f21977fca
commit c1045a3cb2
7 changed files with 82 additions and 79 deletions

View File

@ -1,77 +1,68 @@
import { parse as JSONC } from "https://deno.land/x/jsonct@v0.1.0/mod.ts"; import { parse as JSONC } from "jsr:@std/jsonc";
type JsonLike = { [key: string]: string | string[] | JsonLike; }; type JsonLike = { [key: string]: string | string[] | JsonLike; };
/** A `file://` url version of Deno.cwd() (contains trailing slash) */ /** A `file://` URL version of Deno.cwd() (contains trailing slash) */
export const Root = new URL(`file://${Deno.cwd().replaceAll("\\", "/")}`).toString() + "/"; export const Root = new URL(`file://${Deno.cwd().replaceAll("\\", "/")}/`).toString();
export default async function HuntConfig(directory=Root)
{ export default async function HuntConfig(directory = Root) {
let path:string, json:JsonLike|undefined; let path = "";
console.log("searchig in directory", directory) let json: JsonLike | undefined;
const loadJSON =async(inPath:string)=>{
console.log("Searching in directory", directory);
const loadJSON = async (inPath: string): Promise<JsonLike | undefined> => {
try { try {
const path = new URL(inPath, directory); const pathUrl = new URL(inPath, directory);
console.log("looking at", path.href); console.log("Looking at", pathUrl.href);
const resp = await fetch(path);
if(inPath.endsWith("./.jsonc")) let text: string;
{ if (inPath.endsWith(".jsonc")) {
const text = await resp.text(); text = await Deno.readTextFile(pathUrl);
json = JSONC(text) as JsonLike; json = JSONC(text) as JsonLike;
} else {
text = await Deno.readTextFile(pathUrl);
json = JSON.parse(text) as JsonLike;
} }
else
{
json = await resp.json();
}
return json; return json;
} catch (e) {
console.error(`Error loading JSON from ${inPath}:`, e.message);
return undefined;
} }
catch(_e) };
{
return {};
}
}
try// look for `deno.json` try {
{
json = await loadJSON("./deno.json"); json = await loadJSON("./deno.json");
} } catch (_e) {
catch(_e) try {
{
try // look for `deno.jsonc`
{
json = await loadJSON("./deno.jsonc"); json = await loadJSON("./deno.jsonc");
} } catch (_e) {
catch(_e) try {
{ json = await loadJSON("./.vscode/settings.json");
try // look in the vscode plugin settings path = json?.["deno.config"] as string || "";
{
json = await loadJSON("./.vscode/settings.json")
path = json ? json["deno.config"] as string : "";
json = undefined; json = undefined;
if(path) if (path) {
{ json = await loadJSON(path);
json = await loadJSON(path)
} }
} } catch (_e) {
catch(_e) console.warn("Cannot find a config using the VSCode plugin");
{
// cant find a config using the vscode plugin
} }
} }
} }
if(!json) if (!json) {
{
json = {}; json = {};
} }
path = json.importMap as string || "";
path = json.importMap as string; if (!json.imports && path) {
if(!json.imports && path)
{
json.imports = await loadJSON(path) as JsonLike; json.imports = await loadJSON(path) as JsonLike;
} }
return json as {imports:JsonLike, compilerOptions:JsonLike}; // Ensure the return type always includes imports and compilerOptions
return {
imports: json.imports || {},
compilerOptions: json.compilerOptions || {}
} as { imports: JsonLike, compilerOptions: JsonLike };
} }

View File

@ -1,7 +1,7 @@
import Bundle from "./mod.ts"; import Bundle from "./mod.ts";
import Config from "./test/deno.json" with {type:"json"}; import Config from "./test/deno.json" with {type:"json"};
const path = import.meta.resolve("./test/"); const path = await import.meta.resolve("./test/");
const {outputFiles} = await Bundle(path, {entryPoints:["./app.tsx"]}, Config); const {outputFiles} = await Bundle(path, {entryPoints:["./app.tsx"]}, Config);
if(outputFiles) if(outputFiles)
{ {

18
mod.ts
View File

@ -2,10 +2,12 @@ import * as ESBuild from "https://deno.land/x/esbuild@v0.19.2/wasm.js";
import * as Mapper from "https://esm.sh/esbuild-plugin-importmaps@1.0.0"; // https://github.com/andstellar/esbuild-plugin-importmaps import * as Mapper from "https://esm.sh/esbuild-plugin-importmaps@1.0.0"; // https://github.com/andstellar/esbuild-plugin-importmaps
import Introspect from "./introspect.ts"; import Introspect from "./introspect.ts";
const prefix = "/_dot_importer_/";
const resolvePlugin =(fullPathDir:string):ESBuild.Plugin=>({ const resolvePlugin =(fullPathDir:string):ESBuild.Plugin=>({
name: "resolve-plugin", name: "resolve-plugin",
setup(build) { setup(build) {
const namespace = "http";
const prefix = "/_dot_importer_/";
build.onResolve({ filter: /^(\.\/|\.\.\/).*/ }, (args)=> build.onResolve({ filter: /^(\.\/|\.\.\/).*/ }, (args)=>
{ {
let resolveRoot = args.importer||fullPathDir; let resolveRoot = args.importer||fullPathDir;
@ -13,15 +15,13 @@ const resolvePlugin =(fullPathDir:string):ESBuild.Plugin=>({
{ {
resolveRoot = resolveRoot.substring(prefix.length); resolveRoot = resolveRoot.substring(prefix.length);
} }
const output:ESBuild.OnResolveResult = { return {
path:prefix + new URL(args.path, resolveRoot).href, path:prefix + new URL(args.path, resolveRoot).href,
namespace:"http", namespace,
} } as ESBuild.OnResolveResult;
return output;
}); });
build.onLoad({ filter: /.*/, namespace:"http" }, async(args)=> { build.onLoad({ filter: /.*/, namespace }, async(args)=> {
const fetchPath = args.path.substring(prefix.length); const fetchPath = args.path.substring(prefix.length);
console.log("fetch path", fetchPath);
const result = await fetch(fetchPath); const result = await fetch(fetchPath);
const contents = await result.text(); const contents = await result.text();
return { contents, loader: `tsx` }; return { contents, loader: `tsx` };
@ -34,8 +34,8 @@ export type ImportMap = Parameters<typeof Mapper.importmapPlugin>[0];
export type BuildOptions = ESBuild.BuildOptions; export type BuildOptions = ESBuild.BuildOptions;
/** /**
* *
* @param directory Full file:// or http(s):// path to the directory containing assets you want to build (needed to resolve relative imports) * @param directory Full "file:///..." or "http(s)://..." url to the directory containing assets you want to build (needed to resolve relative imports)
* @param buildOptions ESBuild "build" options (will be merged with "reasonable defaults") for docs: https://esbuild.github.io/api/#general-options * @param buildOptions ESBuild "build" options (will be merged with "reasonable defaults") official docs: https://esbuild.github.io/api/#general-options
* @param importMap An object to act as the import map ({imports:Record<string, string>}). If this is left blank, a configuration will be scanned for in the "directory" * @param importMap An object to act as the import map ({imports:Record<string, string>}). If this is left blank, a configuration will be scanned for in the "directory"
* @returns build result * @returns build result
*/ */

View File

@ -1,4 +1,4 @@
import mime from "https://esm.sh/mime@4.0.3"; import * as mime from "jsr:@std/media-types";
import { parseArgs } from "jsr:@std/cli/parse-args"; import { parseArgs } from "jsr:@std/cli/parse-args";
import bundler, {type ESBuild, type ImportMap} from "./mod.ts"; import bundler, {type ESBuild, type ImportMap} from "./mod.ts";
import Introspect from "./introspect.ts"; import Introspect from "./introspect.ts";
@ -24,7 +24,7 @@ async function serve(settings:ServeArgs):Promise<void>
let type:string|null = null let type:string|null = null
if(extension) if(extension)
{ {
type = (ExtensionsJS.includes(extension)) ? "application/javascript" : mime.getType(extension); type = (ExtensionsJS.includes(extension)) ? "application/javascript" : mime.contentType(extension);
} }
return {path, extension, type}; return {path, extension, type};
@ -98,6 +98,7 @@ async function serve(settings:ServeArgs):Promise<void>
///// go ///// go
Deno.serve({port:parseInt(settings.port as string)||8000}, async(req)=> Deno.serve({port:parseInt(settings.port as string)||8000}, async(req)=>
{ {
console.log("new request", req.url)
const checkHash = req.headers.get("if-none-match") const checkHash = req.headers.get("if-none-match")
if(checkHash === ETag){ if(checkHash === ETag){
return resp304; return resp304;

View File

@ -1,7 +1,7 @@
{ {
"imports": { "imports": {
"react": "npm:preact@10.22.0/compat", "react": "https://esm.sh/preact@10.22.0/compat",
"react/": "npm:preact@10.22.0/compat/" "react/": "https://esm.sh/preact@10.22.0/compat/"
}, },
"compilerOptions": { "compilerOptions": {
"jsx": "react-jsx", "jsx": "react-jsx",

11
test/deno.lock Normal file
View File

@ -0,0 +1,11 @@
{
"version": "3",
"remote": {
"https://esm.sh/preact@10.22.0/compat/jsx-runtime": "a2f6ddc2ce374813df1c13826a9ad010e90b5a70a989f1069a367ef60dd52eb0",
"https://esm.sh/stable/preact@10.22.0/denonext/compat.js": "7c0b206984707cfef58efae492ea8d5212b8f620dd8c83294a5c832fb422c766",
"https://esm.sh/stable/preact@10.22.0/denonext/compat/jsx-runtime.js": "fecfa3df69d580507801575175087de9a2a9fc23bb4900004a1f4cbd5b362634",
"https://esm.sh/stable/preact@10.22.0/denonext/hooks.js": "09230113132c216bbc3847aaad11289771e088be1b0eb9e49cbc724faaeac205",
"https://esm.sh/stable/preact@10.22.0/denonext/jsx-runtime.js": "de60943799b1cbe6066c4f83f4ca71ef37011d7f5be7bef58ed980e8ff3f996a",
"https://esm.sh/stable/preact@10.22.0/denonext/preact.mjs": "20c9563e051dd66e053d3afb450f61b48f2fa0d0ce4f69f8f0a2f23c1ef090da"
}
}