lucacasonato / esbuild_deno_loader

Deno module resolution for `esbuild`
https://deno.land/x/esbuild_deno_loader
MIT License
160 stars 43 forks source link

[plugin deno-loader] Module not found - preact/hook #87

Closed ynwd closed 12 months ago

ynwd commented 12 months ago

ERROR:

✘ [ERROR] Module not found "file:///Users/pro/ken/preact/hooks". [plugin deno-loader]

    preact.tsx:2:25:
      2 │ import { useState } from "preact/hooks";
        ╵                          ~~~~~~~~~~~~~~

error: Uncaught (in promise) Error: Build failed with 1 error:
preact.tsx:2:25: ERROR: [plugin: deno-loader] Module not found "file:///Users/pro/ken/preact/hooks".
  let error = new Error(`${text}${summary}`);
              ^
    at failureErrorWithLog (https://deno.land/x/esbuild@v0.17.19/mod.js:1612:15)
    at https://deno.land/x/esbuild@v0.17.19/mod.js:1024:25
    at runOnEndCallbacks (https://deno.land/x/esbuild@v0.17.19/mod.js:1447:45)
    at buildResponseToResult (https://deno.land/x/esbuild@v0.17.19/mod.js:1022:7)
    at https://deno.land/x/esbuild@v0.17.19/mod.js:1051:16
    at responseCallbacks.<computed> (https://deno.land/x/esbuild@v0.17.19/mod.js:673:9)
    at handleIncomingPacket (https://deno.land/x/esbuild@v0.17.19/mod.js:728:9)
    at readFromStdout (https://deno.land/x/esbuild@v0.17.19/mod.js:649:7)
    at https://deno.land/x/esbuild@v0.17.19/mod.js:1898:11
    at eventLoopTick (ext:core/01_core.js:183:11)

preact.tsx

import { h } from "preact";
import { useState } from "preact/hooks";

export default function UUID(props: { data: string }) {
  const [data, setD] = useState({
    uuid: "e90d45df-4132-41ad-aec8-e0e19f7647a9",
  });

  const handleClick = async () => {
    const res = await fetch("/api");
    setD(await res.json());
  };

  return (
    <div>
      <h1>Hello {props.data}</h1>
      <button onClick={handleClick}>
        Click me to get the fresh UUID from API: {data.uuid}
      </button>
    </div>
  );
}

Import map:

{
  "imports": {
    "react": "https://esm.sh/preact@10.17.1/compat",
    "preact": "https://esm.sh/preact@10.17.1",
    "preact/": "https://esm.sh/preact@10.17.1/"
  }
}

build.ts

import * as esbuild from "https://deno.land/x/esbuild@v0.17.19/mod.js";
// Import the WASM build on platforms where running subprocesses is not
// permitted, such as Deno Deploy, or when running without `--allow-run`.
// import * as esbuild from "https://deno.land/x/esbuild@v0.17.19/wasm.js";

import { denoPlugins } from "https://deno.land/x/esbuild_deno_loader@0.8.1/mod.ts";

const result = await esbuild.build({
  plugins: [...denoPlugins()],
  entryPoints: ["preact.tsx"],
  outfile: "./dist/preact.js",
  bundle: true,
  format: "esm",
});

console.log(result.outputFiles);

esbuild.stop();
ynwd commented 12 months ago

Solved! https://github.com/denoland/fresh/issues/1395