New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
import of particular client side javascript dependency fails #401
Comments
Hi. The main change was to switch from esm.sh to unpkg.com as CDN to serve the NPM dependencies. This ensure the code processed by esbuild is the same code provided by NPM instead of a ES module version transpiled by esm.sh. With this change I could finally swich to development mode in React. For example, the following code bundles React JSX code to production: site.use(esbuild({
extensions: [".jsx"],
options: {
jsxDev: false,
define: {
"process.env.NODE_ENV": "'production'",
}
}
})) And this code is for development: site.use(esbuild({
extensions: [".jsx"],
options: {
jsxDev: true,
}
})) So this change should fix #400. The only problem is unpkg.com doesn't resolve the dependencies versions. For example, the code:
It doesn't contain the react version, so by default it goes to unpkg.com/react, returning always the latest version. A solution could be provide an option to define the dependencies versions manually, for example: site.use(esbuild({
dependencies: {
react: "18.2.0"
}
})); But not sure if this is a good idea.
esbuild uses the same import map as Deno to resolve the dependencies. |
This is the typical error when converting a CJS module to ESM. Probably this doesn work: import { useExpanded } from 'npm:react-table'; but this does: import reactTable from 'npm:react-table';
const { useExpanded } = reactTable; |
I tried this and got :
The line is half way in the generated bundle:
I suppose the code should be stripped away, but it isn't, or maby a shim or polyfill needs to be added. |
Are you using the Deno namespace in your client-side javascript code? |
Nothing on the client refers to deno, the only thing in the whole project related to deno (indirectly) is this line :
|
I managed to get the import working with this :
as recomended on https://esm.sh/#docs
But I still get the error "Uncaught ReferenceError: Deno is not defined" |
Ok, it seems it comes from |
As commented in #400 (comment) the new |
Great, I think this issue can be closed, as it's a esm.sh problem that has the "?cjs-exports" workaround. Thanks ! |
I'm now getting a strane error related to "?cjs-exports", the following import : import { useExpanded, useTable } from 'npm:react-table?cjs-exports=useTable,useExpanded'; works file if I add it while lume is serving (deno task lume -s ) I tried reproducing the problem in this repo https://github.com/max-l/react-todo/blob/main/app/TestTable.jsx but unfortunately I'm not able to get the bug to occur in this repo.
|
Looking at the error message:
The error comes from the It looks like you're importing |
ProteinTable.jsx is a file in my repo, I was trying to reproduce the problem in https://github.com/max-l/react-todo I will put my Lume project in a public repo so you can see the whole thing. I can assure you there are no "react" without the "npm:" prefix, but perhaps I'm doing something else just as dumb ;-) |
@oscarotero I managed to extract the code to reproduce the problem, in this repo: https://github.com/max-l/lume-cjs-exports-test-case |
@max-l Ok thanks. I discovered the problem is this line https://github.com/max-l/lume-cjs-exports-test-case/blob/master/app/ProteinTable.jsx#L25 (if I remove it, it works fine). Moving the line to after the <tr
key={`${rowProps.key}-expanded-${i}`}
{...rowProps}
> Don't ask me why :D |
Interesting ! I'm a bit puzzled that Deno is involved here, because that code never runs on the server (or at least it shouldn't), if esm does the compilation, the only thing Deno should do is to send the code (as inert data) to the client. I noticed something interesting: if I remove the offending code, start Lume, and then add the code again, the code reloads and it works correctly. It seems as if the offending code prevents something on the server to initialize correctly, because once you remove it, and the initialisation gets done, the offending code has lost it's corrupting effect ! |
The to-do app repository (that you used as a template) pre-renders the code at buildtime in order to be hydrated later on client side. At buildtime, the App is imported in the On the client side, the app is imported and hydrated here: https://github.com/max-l/lume-cjs-exports-test-case/blob/master/main.jsx#L6-L11 So if you don't need buildtime pre-rendering, you can remove the code in the index.tmpl.ts file. |
Interesting, there was much more magic going on than I thought ! I suppose I should use useEffect to switch on/off the prerendering strategicaly ? |
The prerendering is handled in the Lume template, before any React code is executed. There's no much magic: just render the root component The hydration step on the browser side just complete the HTML adding the event listeners (onclick, etc). It only makes sense if the component's data is available at buildtime, so the output HTML is similar to the code generated on the client side. In the to-do example, it didn't make sense because the data is stored in the localStorage on the browser side, so at build time only generates an empty to-do list. But it's just an example to demostrate that this is possible. |
Version
1.16.0
Platform
deno 1.30.3 (release, x86_64-unknown-linux-gnu) v8 10.9.194.5 typescript 4.9.4
What steps will reproduce the bug?
When importing a package from "deno:https://esm.sh/react-table@7.8.0"
with the following code:
import { useExpanded, useTable } from 'npm:react-table';
I get the following error:
✘ [ERROR] No matching export in "deno:https://esm.sh/react-table@7.8.0" for import "useExpanded"
The exports are defined, as shown here (in the same version, i.e. 7.8.0): https://react-table-v7.tanstack.com/docs/examples/sub-components-lazy
This is probably an issue with the library not being packaged to Deno's (or esbuild's) taste, but I was wondering if there are
other means to declare dependencies, where more "dependency incantation tweaking" is possible, I'm guessing
import_map.json would probably be the place to do that, but I'm wondering if this map file only concerns imports for Lume's static/build time rendering, or if I can do some dependency tweaking for esbuild in there.
How often does it reproduce? Is there a required condition?
reproduces with:
What is the expected behavior?
proper import of module
What do you see instead?
error message in stdout
Additional information
No response
The text was updated successfully, but these errors were encountered: