jedisct1 / libsodium.js

libsodium compiled to Webassembly and pure JavaScript, with convenient wrappers.
Other
968 stars 138 forks source link

Migrate to `webpack` v5+ #294

Closed cognivore closed 2 years ago

cognivore commented 2 years ago

I'm trying to use libsodium-wrapper with an app made out of this template: \https://github.com/doma-engineering/cra-template-multi-nix without ejecting. When I run the server, I get

Compiled with problems:

ERROR in ./node_modules/libsodium/dist/modules/libsodium.js 49:23-46

Module not found: Error: Can't resolve 'path' in '/home/sweater/github/do-auth/priv/ui/node_modules/libsodium/dist/modules'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "path": require.resolve("path-browserify") }'
    - install 'path-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "path": false }

I'll try to see what can I do, but so far I'm very confused where does webpack error even come from, since neither my template, nor this repo has any webpack.config.js files in it.

jedisct1 commented 2 years ago

This project doesn't use webpack either.

cognivore commented 2 years ago

@jedisct1 fair enough, it is interesting -- however -- where does the source file here /home/sweater/github/do-auth/priv/ui/node_modules/libsodium/dist/modules expects to find path module in a browser? How do you get around it?

It's fairly hard to read the minified code, perhaps if you could point some pointers on how path module is used in libsodium-wrapper, it would help a lot.

nowrap commented 1 year ago

The same behaviour occurs when using a vue project with typescript and vue-cli version 5.

The path error is fixable by adding the mentioned path-browserify. First install it: npm i path-browserify -D

Second add it to the tsconfig.json to compilerOptions.paths as: "path": [ "./node_modules/path-browserify" ]

Than the error is gone but a new warning is created:

warning in ./node_modules/libsodium/dist/modules/libsodium.js Module not found: Error: Can't resolve 'crypto' in

Trying to fix this the same way with crypto-browserify doesn't work.

// EDIT With crypto-js it works: "crypto": [ "./node_modules/crypto-js" ]