resolve local file paths

This commit is contained in:
Seth Trowbridge 2024-06-08 08:09:56 -04:00
parent eaa0c4c328
commit 023b9a6474
5 changed files with 60 additions and 74 deletions

View File

@ -1,7 +1,2 @@
import React from "react"; import Other from "./other.tsx";
console.log(Other);
//const other = import("./app-dynamic.tsx");
import * as Other from "./other.tsx";
console.log(Other.default, Other.app);
React.render(React.createElement(Other.app, null), document.querySelector("#app"));

View File

@ -3,9 +3,7 @@ import B from "./mod.ts";
const results = await B({ const results = await B({
imports: { imports: {
"react": "https://esm.sh/preact@10.22.0/compat", "react": "https://esm.sh/preact@10.22.0/compat",
"react/": "https://esm.sh/preact@10.22.0/compat/", "react/": "https://esm.sh/preact@10.22.0/compat/"
"other": "./other.tsx",
"entry": "./app.tsx"
} }
}); });

View File

@ -186,6 +186,7 @@
"https://esm.sh/@jspm/import-map@1.0.8": "61a89e4acc2b123bf9e97f65107a2a4cd9db4d939951e140e94bff3f1afa6c5f", "https://esm.sh/@jspm/import-map@1.0.8": "61a89e4acc2b123bf9e97f65107a2a4cd9db4d939951e140e94bff3f1afa6c5f",
"https://esm.sh/esbuild-plugin-import-map@2.1.0": "591fd6ee133417fba19ed9fa95fd2ddf74a43f96b840c16f2b8ff4b3d6e2da93", "https://esm.sh/esbuild-plugin-import-map@2.1.0": "591fd6ee133417fba19ed9fa95fd2ddf74a43f96b840c16f2b8ff4b3d6e2da93",
"https://esm.sh/esbuild-plugin-importmaps@1.0.0": "ab9e79660ff4d57d2ed7ef5e8516fbe0e79305267f22e1e7270d0a17ae0c2029", "https://esm.sh/esbuild-plugin-importmaps@1.0.0": "ab9e79660ff4d57d2ed7ef5e8516fbe0e79305267f22e1e7270d0a17ae0c2029",
"https://esm.sh/preact@10.22.0/compat": "88368f5ecd284b0666d5ff8fbf7bb69ea131e7e947ad54f2fa24f5035b674b41",
"https://esm.sh/preact@10.22.0/compat/jsx-runtime": "a2f6ddc2ce374813df1c13826a9ad010e90b5a70a989f1069a367ef60dd52eb0", "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.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/compat/jsx-runtime.js": "fecfa3df69d580507801575175087de9a2a9fc23bb4900004a1f4cbd5b362634",

111
mod.ts
View File

@ -1,70 +1,65 @@
import * as ESBuildWASM from "https://deno.land/x/esbuild@v0.21.4/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 | https://github.com/jspm/import-map import * as Mapper from "https://esm.sh/esbuild-plugin-importmaps@1.0.0"; // https://github.com/andstellar/esbuild-plugin-importmaps
import { ImportMap, type IImportMap } from "https://esm.sh/@jspm/import-map"; export const Root = new URL(`file://${Deno.cwd().replaceAll("\\", "/")}`).toString()+"/";
console.log("running at", Root);
const resolvePlugin:ESBuild.Plugin = {
name: "resolve-plugin",
setup(build) {
build.onResolve({ filter: /^\.\/.*$/ }, args => {
console.log("RESOLVE")
console.log("in:", args)
const path = new URL(args.path, Root).pathname
const output = {
path,
namespace: 'file',
};
console.log("out:", output)
return output;
});
export const Root = new URL(`file://${Deno.cwd().replaceAll("\\", "/")}`).toString(); build.onLoad({ filter: /.*/, namespace:"file" }, async (args) => {
console.log("LOAD")
console.log(args)
let contents;
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',
};
});
},
};
function importmapPlugin(importmap:IImportMap):ESBuildWASM.Plugin { type ImportMap = Parameters<typeof Mapper.importmapPlugin>[0];
const map = new ImportMap({
map: importmap,
});
return { await ESBuild.initialize({ worker: false });
name: "importmap-url", const result = await ESBuild.build({
setup(build) { entryPoints: ["./app.tsx"],
build.onResolve({ filter: /^[^.].*$/ }, (args) => {
let path;
try
{
path = map.resolve(args.path, args.importer);
console.log("resolved path:", path);
return { path, namespace: "importmap-url" };
}
catch(e)
{
console.log("e r r o r:", args);
return { path: args.importer };
}
});
build.onLoad(
{ filter: /.*/, namespace: "importmap-url" },
async (args) => {
const response = await fetch(args.path);
const contents = await response.text();
const ext = args.path.split(".").pop();
const loader = /** @type {import('esbuild').Loader} */ (
ext?.match(/"j|tsx?$/) ? ext : "js"
);
console.log("loaded", args);
return { contents, loader } as ESBuildWASM.OnLoadResult;
},
);
},
};
}
export default async(importMap:IImportMap, buildOptions?:ESBuildWASM.BuildOptions)=>
{
await ESBuildWASM.initialize({ worker: false });
const result = await ESBuildWASM.build({
entryPoints: ["entry"],
bundle: true, bundle: true,
minify: true, minify: true,
format: "esm", format: "esm",
outfile:"bundle.js",
entryNames: `[dir][name]`,
jsx: "automatic", jsx: "automatic",
jsxImportSource: "react", jsxImportSource: "react",
...buildOptions,
plugins:[ plugins:[
...buildOptions?.plugins??[], resolvePlugin,
importmapPlugin(importMap) as ESBuildWASM.Plugin Mapper.importmapPlugin({
imports:{"react":"https://esm.sh/preact@10.22.0/compat"}
}) as ESBuild.Plugin
] ]
}); });
ESBuildWASM.stop(); ESBuild.stop();
return result;
}; for(const item of result.outputFiles)
{
console.log(item.text)
}

View File

@ -1,6 +1,3 @@
export default "TEST STRING"; import React from "react";
import {useState} from "react"; console.log(React.createElement("div", {}, "hey"));
export const app =()=> { export default "TEST STRING";
const [stateGet, stateSet] = useState(7)
return <div><h1>lolidk <button onClick={_=>stateSet(stateGet + 1)}>{stateGet}</button></h1></div>;
}