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

Bug: importing 'crypto' polyfill leads to error exports is not defined with yarn build && yarn preview in nuxt #92

Open
clangenb opened this issue Apr 30, 2024 · 7 comments

Comments

@clangenb
Copy link

clangenb commented Apr 30, 2024

The following commit breaks yarn build && yarn preview: integritee-network/try-incognitee-app@87272d0.

However, unlike #90, I can't opt out of crypto as I need its function.

The code runs fine with yarn dev.

@Mallfurion
Copy link

@clangenb did you find any solutions for this?
I am encountering this as well.

@clangenb
Copy link
Author

I finally re-implemented our rsa crypto with the webcrypto api, so we don't need any polyfills at all. I would recommend such an approach if it is feasible in your case.

@anton-liam
Copy link

same issue , any idea?

@clangenb
Copy link
Author

I can only repeat what I mentioned above. If the crypto polyfill is needed because of a 3rd party dependency, I recommend looking for another dependency, as most maintained libraries should have switched to use the WebCrypto Api. If it is because of your own code (like in my case), I suggest rewriting the crypto stuff with WebCrypto API as I did here encointer/encointer-js#101, with a follow-up fix in encointer/encointer-js#105.

@anton-liam
Copy link

anton-liam commented Jul 15, 2024

@clangenb I can't rewrite the code because it's subpackage dependent.

After two hours of code retracing I found that the crypto-broserify dependent randomfill package was not transpiled by rollup.

sourcecode: randomfill:L39

the Error: image

Does that mean exports can't work with if by rollup?

@clangenb
Copy link
Author

I am not an JS expert, but I would assume that this is a bug in either browserify or randomfill and that this should work in general.

@anton-liam
Copy link

I used the replace plugin to change the code in question,

nuxt.config.ts

import { replaceCodePlugin } from 'vite-plugin-replace';

...
vite: {
    plugins: [
      replaceCodePlugin({
        replacements: [
          {
            from: `if ((crypto && crypto.getRandomValues) || !process.browser) {
  exports.randomFill = randomFill
  exports.randomFillSync = randomFillSync
} else {
  exports.randomFill = oldBrowser
  exports.randomFillSync = oldBrowser
}`,
            to: `exports.randomFill = randomFill
exports.randomFillSync = randomFillSync`,
          },
        ],
      }),
}

miguel-nascimento added a commit to river-build/river that referenced this issue Nov 19, 2024
miguel-nascimento added a commit to river-build/river that referenced this issue Nov 20, 2024
yarn dev was working fine, but yarn build had some issues with crypto
polyfill

davidmyersdev/vite-plugin-node-polyfills#92 (comment)
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

3 participants