From 023b9a647463096999bc46a47dcdc8c288cf9191 Mon Sep 17 00:00:00 2001 From: Seth Trowbridge Date: Sat, 8 Jun 2024 08:09:56 -0400 Subject: [PATCH] resolve local file paths --- app.tsx | 9 +--- bundler-inc.tsx | 4 +- deno.lock | 1 + mod.ts | 111 +++++++++++++++++++++++------------------------- other.tsx | 9 ++-- 5 files changed, 60 insertions(+), 74 deletions(-) diff --git a/app.tsx b/app.tsx index d776be6..bc9feff 100644 --- a/app.tsx +++ b/app.tsx @@ -1,7 +1,2 @@ -import React from "react"; - -//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")); +import Other from "./other.tsx"; +console.log(Other); diff --git a/bundler-inc.tsx b/bundler-inc.tsx index 8ffb5ab..5369074 100644 --- a/bundler-inc.tsx +++ b/bundler-inc.tsx @@ -3,9 +3,7 @@ import B from "./mod.ts"; const results = await B({ imports: { "react": "https://esm.sh/preact@10.22.0/compat", - "react/": "https://esm.sh/preact@10.22.0/compat/", - "other": "./other.tsx", - "entry": "./app.tsx" + "react/": "https://esm.sh/preact@10.22.0/compat/" } }); diff --git a/deno.lock b/deno.lock index 7c28539..be18be8 100644 --- a/deno.lock +++ b/deno.lock @@ -186,6 +186,7 @@ "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-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/stable/preact@10.22.0/denonext/compat.js": "7c0b206984707cfef58efae492ea8d5212b8f620dd8c83294a5c832fb422c766", "https://esm.sh/stable/preact@10.22.0/denonext/compat/jsx-runtime.js": "fecfa3df69d580507801575175087de9a2a9fc23bb4900004a1f4cbd5b362634", diff --git a/mod.ts b/mod.ts index 3496468..fdd0784 100644 --- a/mod.ts +++ b/mod.ts @@ -1,70 +1,65 @@ -import * as ESBuildWASM from "https://deno.land/x/esbuild@v0.21.4/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 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 { 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 { - const map = new ImportMap({ - map: importmap, - }); +type ImportMap = Parameters[0]; - return { - name: "importmap-url", - setup(build) { - 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"], + await ESBuild.initialize({ worker: false }); + const result = await ESBuild.build({ + entryPoints: ["./app.tsx"], bundle: true, minify: true, format: "esm", - outfile:"bundle.js", - entryNames: `[dir][name]`, jsx: "automatic", jsxImportSource: "react", - ...buildOptions, plugins:[ - ...buildOptions?.plugins??[], - importmapPlugin(importMap) as ESBuildWASM.Plugin + resolvePlugin, + Mapper.importmapPlugin({ + imports:{"react":"https://esm.sh/preact@10.22.0/compat"} + }) as ESBuild.Plugin ] }); - ESBuildWASM.stop(); - return result; -}; \ No newline at end of file + ESBuild.stop(); + +for(const item of result.outputFiles) +{ + console.log(item.text) +} \ No newline at end of file diff --git a/other.tsx b/other.tsx index 526f5cd..0104b81 100644 --- a/other.tsx +++ b/other.tsx @@ -1,6 +1,3 @@ -export default "TEST STRING"; -import {useState} from "react"; -export const app =()=> { - const [stateGet, stateSet] = useState(7) - return

lolidk

; -} \ No newline at end of file +import React from "react"; +console.log(React.createElement("div", {}, "hey")); +export default "TEST STRING"; \ No newline at end of file