Skip to content
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

Sveltekit + Vite unable to import function #36

Open
Seancheey opened this issue Oct 26, 2023 · 3 comments
Open

Sveltekit + Vite unable to import function #36

Seancheey opened this issue Oct 26, 2023 · 3 comments

Comments

@Seancheey
Copy link

Environment

Sveltekit + Vite + NPM

Attempt 1

I tried to import with

import { sha3_512 } from "js-sha3";

but npm run build gives me an error message:

import { sha3_512 } from "js-sha3";
         ^^^^^^^^
SyntaxError: Named export 'sha3_512' not found. The requested module 'js-sha3' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'js-sha3';
const { sha3_512 } = pkg;

Attempt 2

However, when I switch to

import pkg from 'js-sha3';
const { sha3_512 } = pkg;

I found this runtime error:

...
Runtime.UnhandledPromiseRejection","errorMessage":"TypeError: sha3_512 is not a function","reason":{"errorType":"TypeError","errorMessage":"sha3_512 is not a function","stack":["TypeError: sha3_512 is not a function","
...

My Guess

I doubt the sha3_512 function is tree-shaked by vite compiler, but I'm not sure why.

Possible fix

  1. Try to make the package a non-CommonJS module by setting "type": "module" in package.json?
  2. Make sure to export the sha3_512 function in a way that vite build won't tree-shake the function?
@emn178
Copy link
Owner

emn178 commented Oct 27, 2023

I tried this

import pkg from 'js-sha3';
const { sha3_512 } = pkg;

it works

Can you give an example project?

@Seancheey
Copy link
Author

I tried this

import pkg from 'js-sha3';
const { sha3_512 } = pkg;

it works

Can you give an example project?

Thanks for quick response, yes,

vite is working fine for that method, but vite build then executing production package will raise an error.

@emn178
Copy link
Owner

emn178 commented Oct 27, 2023

I use npm run build and got no error.

mergify bot pushed a commit to enso-org/enso that referenced this issue Nov 2, 2023
The first part of #8158. No progress indication for the user implemented.


https://github.com/enso-org/enso/assets/6566674/2d8157d4-748f-4442-a9c3-a96ba0029056

# Important Notes
A few notable changes:
- A fix for `DataServer/writeBytes`
- Using `@noble/hashes` instead of `sha3` because the latter only works with node. I also tried using `js-sha3`, but it does not work well with Vite (see emn178/js-sha3#36)
- Fixed initialization of the ID map for new nodes. Also, all new nodes are prepended by four spaces to have proper indentation inside the `main` function.
- Fixed random pattern name generation because the previous approach sometimes produces identifiers starting with a number.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants