lucacasonato / esbuild_deno_loader

Deno module resolution for `esbuild`
https://jsr.io/@luca/esbuild-deno-loader
MIT License
176 stars 46 forks source link

NPM specifier libs not working #81

Closed jordandobrev closed 10 months ago

jordandobrev commented 1 year ago

Hey :wave:

Tried bundling one of my aws lambda projects and I'm getting errors when trying to do it:

my deps.ts

....
export { SSMClient, GetParameterCommand } from "npm:@aws-sdk/client-ssm"
....

my bundle.ts

import * as esbuild from "https://deno.land/x/esbuild@v0.19.2/mod.js";

import { denoPlugins } from "https://raw.githubusercontent.com/lucacasonato/esbuild_deno_loader/main/mod.ts";

const result = await esbuild.build({
  plugins: [...denoPlugins()],
  entryPoints: [`${source}/handler.ts`],
  outfile: `${dist_folder}/handler-bundle.js`,
  bundle: true,
  format: "esm",
});

This resunts in over 30+ errors:

✘ [ERROR] NPM package not found. [plugin deno-loader]

    ../../.cache/deno/deno_esbuild/@aws-crypto/sha256-browser@3.0.0/node_modules/@aws-crypto/sha256-browser/build/webCryptoSha256.js:4:21:
      4 │ var util_1 = require("@aws-crypto/util");
        ╵                      ~~~~~~~~~~~~~~~~~~

✘ [ERROR] NPM package not found. [plugin deno-loader]

    ../../.cache/deno/deno_esbuild/@smithy/util-retry@2.0.0/node_modules/@smithy/util-retry/dist-es/DefaultRateLimiter.js:1:34:
      1 │ import { isThrottlingError } from "@smithy/service-error-classification";
        ╵                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

✘ [ERROR] NPM package not found. [plugin deno-loader]

    ../../.cache/deno/deno_esbuild/@aws-crypto/sha256-browser@3.0.0/node_modules/@aws-crypto/sha256-browser/build/webCryptoSha256.js:6:35:
      6 │ var util_locate_window_1 = require("@aws-sdk/util-locate-window");
        ╵                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

✘ [ERROR] NPM package not found. [plugin deno-loader]

    ../../.cache/deno/deno_esbuild/@aws-crypto/sha256-browser@3.0.0/node_modules/@aws-crypto/sha256-browser/build/ie11Sha256.js:6:34:
      6 │ var util_utf8_browser_1 = require("@aws-sdk/util-utf8-browser");

................
................
error: Uncaught (in promise) Error: Build failed with 30 errors:
../../.cache/deno/deno_esbuild/@aws-crypto/sha256-browser@3.0.0/node_modules/@aws-crypto/sha256-browser/build/crossPlatformSha256.js:7:36: ERROR: [plugin: deno-loader] NPM package not found.
../../.cache/deno/deno_esbuild/@aws-crypto/sha256-browser@3.0.0/node_modules/@aws-crypto/sha256-browser/build/crossPlatformSha256.js:8:31: ERROR: [plugin: deno-loader] NPM package not found.
../../.cache/deno/deno_esbuild/@aws-crypto/sha256-browser@3.0.0/node_modules/@aws-crypto/sha256-browser/build/crossPlatformSha256.js:9:35: ERROR: [plugin: deno-loader] NPM package not found.
../../.cache/deno/deno_esbuild/@aws-crypto/sha256-browser@3.0.0/node_modules/@aws-crypto/sha256-browser/build/crossPlatformSha256.js:10:21: ERROR: [plugin: deno-loader] NPM package not found.
../../.cache/deno/deno_esbuild/@aws-crypto/sha256-browser@3.0.0/node_modules/@aws-crypto/sha256-browser/build/ie11Sha256.js:6:34: ERROR: [plugin: deno-loader] NPM package not found.
...
  let error = new Error(text);

Any ideas how I can sort this out?

bpevs commented 1 year ago

I'm also seeing this behavior!

thrown from this line: https://github.com/lucacasonato/esbuild_deno_loader/blob/main/src/loader_native.ts#L161

import { build } from "npm:esbuild";
import { solidPlugin } from "npm:esbuild-plugin-solid";
import { denoPlugins } from "https://deno.land/x/esbuild_deno_loader@0.8.1/mod.ts";
import { resolve } from 'https://deno.land/std@0.198.0/path/mod.ts';

const importMapURL = new URL('file://' + resolve('./import_map.json'))

const [denoResolver, denoLoader] = [...denoPlugins({ importMapURL })];
const result = await build({
    entryPoints: ["./source/index.tsx"],
    outfile: "./public/index.js",
    bundle: true,
    format: "esm",
    treeShaking: true,
    minify: true,
    plugins: [
        denoResolver,
        solidPlugin({ solid: { moduleName: 'npm:solid-js/web' } }),
        denoLoader
    ],
});
import { render } from "solid-js/web"
import { Motion } from "npm:@motionone/solid";

function App() {
  return <Motion>Hello world</Motion>
}

render(() => <App />, document.body)
...
✘ [ERROR] NPM package not found. [plugin deno-loader]

    ../../../../../../.deno/deno_esbuild/@solid-primitives/refs@1.0.5_solid-js@1.7.11/node_modules/@solid-primitives/refs/dist/index.js:1:35:
      1 │ import { chain, arrayEquals } from '@solid-primitives/utils';
...
error: Uncaught Error: Build failed with 17 errors:
...
bpevs commented 1 year ago

FYI @jordandobrev it looks like this only happens with the deno global cache node_modules? As a workaround, I'm finding that this error seems to go away if using the nodeModulesDir: true. Not 100% if this is meant to be a public api or not, since it's not mentioned on the README, and it creates a node_modules dir in your project, but does seem to work. Probably portable works as well?

But for your example, this might work:

const result = await esbuild.build({
  plugins: [...denoPlugins({ nodeModulesDir: true })],
  entryPoints: [`${source}/handler.ts`],
  outfile: `${dist_folder}/handler-bundle.js`,
  bundle: true,
  format: "esm",
});
adoublef commented 1 year ago

@bpevs I just tried that fix with nodeModulesDir: true and seems to have helped my most recent issue

cowboyd commented 1 year ago

Note that nodeModulesDir: true will not work on Deno Deploy https://github.com/lucacasonato/esbuild_deno_loader#limitations

lucacasonato commented 10 months ago

This should be fixed by #104

bpevs commented 10 months ago

fix works for me on v0.8.4! Thanks!