Compare commits

...

2 Commits

Author SHA1 Message Date
0cc2d914e1 cleanup 2024-06-10 16:23:31 -04:00
fe33072cd5 ./ and ../ for file and http 2024-06-10 14:49:31 -04:00
6 changed files with 70 additions and 64 deletions

View File

@ -1,13 +1,8 @@
import B from "./mod.ts"; import Bundler from "./mod.ts";
const results = await B({ const results = await Bundler();
imports: {
"react": "https://esm.sh/preact@10.22.0/compat",
"react/": "https://esm.sh/preact@10.22.0/compat/"
}
});
for await(const file of results.outputFiles) for await(const file of results.outputFiles)
{ {
console.log(file.path); console.log(file.text);
} }

2
deep/deep.tsx Normal file
View File

@ -0,0 +1,2 @@
import "../app.tsx";
console.log("we deep!");

View File

@ -6,14 +6,16 @@ type JsonLike = JsonDeep
type Configuration = {imports:JsonDeep, compilerOptions:JsonDeep}; type Configuration = {imports:JsonDeep, compilerOptions:JsonDeep};
export const Root = new URL(`file://${Deno.cwd().replaceAll("\\", "/")}`).toString();
/** A `file://` url version of Deno.cwd() */
export const Root = new URL(`file://${Deno.cwd().replaceAll("\\", "/")}`).toString() + "/";
export default async function HuntConfig() export default async function HuntConfig()
{ {
let path:string, json:JsonLike|undefined; let path:string, json:JsonLike|undefined;
const loadJSON =async(inPath:string)=>{ const loadJSON =async(inPath:string)=>{
try{ try{
const resp = await fetch(Root + "/" + inPath); const resp = await fetch(Root + inPath);
if(inPath.endsWith(".jsonc")) if(inPath.endsWith(".jsonc"))
{ {
const text = await resp.text(); const text = await resp.text();

85
mod.ts
View File

@ -1,65 +1,64 @@
import * as ESBuild from "https://deno.land/x/esbuild@v0.19.2/wasm.js"; 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, {Root} from "./introspect.ts";
export const Root = new URL(`file://${Deno.cwd().replaceAll("\\", "/")}`).toString()+"/";
console.log("running at", Root); const prefix = "/_dot_importer_/";
const resolvePlugin:ESBuild.Plugin = { const resolvePlugin:ESBuild.Plugin = {
name: "resolve-plugin", name: "resolve-plugin",
setup(build) { setup(build) {
build.onResolve({ filter: /^\.\/.*$/ }, args => { build.onResolve({ filter: /^(\.\/|\.\.\/).*/ }, (args)=>
console.log("RESOLVE") {
console.log("in:", args) let resolveRoot = args.importer||Root;
const path = new URL(args.path, Root).pathname if(resolveRoot.startsWith(prefix))
const output = { {
path, resolveRoot = resolveRoot.substring(prefix.length);
namespace: 'file', }
}; const output:ESBuild.OnResolveResult = {
console.log("out:", output) path:prefix + new URL(args.path, resolveRoot).href,
namespace:"http",
}
return output; return output;
}); });
build.onLoad({ filter: /.*/, namespace:"http" }, async(args)=> {
build.onLoad({ filter: /.*/, namespace:"file" }, async (args) => { const fetchPath = args.path.substring(prefix.length);
console.log("LOAD") const result = await fetch(fetchPath);
console.log(args) const contents = await result.text();
let contents; return { contents, loader: `tsx` };
try
{
const result = await fetch("file://"+args.path);
contents = await result.text();
}
catch(e)
{
console.log("deno fs access error:", e)
}
return {
contents,
loader: args.path.endsWith('.tsx') ? 'tsx' : 'default',
};
}); });
}, },
}; };
type ImportMap = Parameters<typeof Mapper.importmapPlugin>[0]; export type ImportMap = Parameters<typeof Mapper.importmapPlugin>[0];
export type BuildOptions = ESBuild.BuildOptions;
export default async function(buildOptions={} as BuildOptions, importMap:ImportMap|false = false)
{
if(importMap === false)
{
importMap = await Introspect() as ImportMap;
}
await ESBuild.initialize({ worker: false }); const configuration:ESBuild.BuildOptions = {
const result = await ESBuild.build({ //entryPoints: ["./app.tsx"],
entryPoints: ["./app.tsx"], entryPoints: ["entry"],
//entryPoints: ["./deep/deep.tsx"],
//entryPoints: ["http://localhost:4507/deep/deep.tsx"],
bundle: true, bundle: true,
minify: true, minify: true,
format: "esm", format: "esm",
jsx: "automatic", jsx: "automatic",
jsxImportSource: "react", jsxImportSource: "react",
plugins:[ ...buildOptions,
plugins: [
resolvePlugin, resolvePlugin,
Mapper.importmapPlugin({ Mapper.importmapPlugin(importMap) as ESBuild.Plugin,
imports:{"react":"https://esm.sh/preact@10.22.0/compat"} ...buildOptions.plugins||[]
}) as ESBuild.Plugin
] ]
}); };
await ESBuild.initialize({ worker: false });
const result = await ESBuild.build(configuration);
ESBuild.stop(); ESBuild.stop();
return result;
for(const item of result.outputFiles)
{
console.log(item.text)
} }
export { ESBuild };

View File

@ -1,3 +1,3 @@
import React from "react"; //import React from "react";
console.log(React.createElement("div", {}, "hey")); //console.log(React.createElement("div", {}, "hey"));
export default "TEST STRING"; export default "TEST STRING";

View File

@ -1,9 +1,4 @@
import bundler from "./mod.ts"; import bundler from "./mod.ts";
import Introspect, {Root} from "./introspect.tsx";
const Config = await Introspect();
console.log(Config.imports);
const transpiled:Map<string, string> = new Map(); const transpiled:Map<string, string> = new Map();
@ -13,18 +8,31 @@ Deno.serve(async(req)=>{
if(index > -1) if(index > -1)
{ {
const ext = url.pathname.substring(index+1, index+3); const ext = url.pathname.substring(index+1, index+3);
console.log(ext);
console.log(url.pathname);
if(ext === "ts") if(ext === "ts")
{ {
const results = await bundler({imports:Config.imports}, { const results = await bundler({
entryPoints:[Root+url.pathname], entryPoints:["."+url.pathname],
outfile:"bundle.js", outfile:"bundle.js",
entryNames: `[dir][name]`, entryNames: `[dir][name]`,
}); });
results.outputFiles?.forEach(output=>{ results.outputFiles?.forEach(output=>{
transpiled.set(output.path, output.text); transpiled.set(output.path, output.text);
}) })
return new Response(results.outputFiles[0].text, {headers:{"content-type":"application/javascript"}}); return new Response(`
path: ${results.outputFiles[0].path}
hash: ${results.outputFiles[0].hash}
`, {headers:{"content-type":"application/javascript"}});
} }
if(ext == "js")
{
return new Response(transpiled.get(url.pathname)||"---", {headers:{"content-type":"application/javascript"}});
}
} }
return new Response(Root+url.pathname); return new Response(url.pathname);
}); });