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

Polyfill of crypto module fail on browser target #10079

Open
vgrunner4v opened this issue Jan 28, 2025 · 0 comments
Open

Polyfill of crypto module fail on browser target #10079

vgrunner4v opened this issue Jan 28, 2025 · 0 comments

Comments

@vgrunner4v
Copy link

vgrunner4v commented Jan 28, 2025

🐛 bug report

@parcel/resolver-default: External dependency "crypto-browserify" is not declared in package.json.

as you can see in package.json below, the "crypto-browserify": "^3.12.1" is there.

also reported here but just closed without answer: #9843

🎛 Configuration (.babelrc, package.json, cli command)

{
  "name": "libnexa-ts",
  "version": "1.0.1",
  "description": "A pure and powerful Nexa core library.",
  "type": "module",
  "source": "src/index.ts",
  "types": "dist/index.d.ts",
  "main": "dist/index.cjs",
  "module": "dist/index.mjs",
  "browser": "dist/index.web.mjs",
  "exports": {
    "types": "./dist/index.d.ts",
    "node": {
      "import": "./dist/index.mjs",
      "require": "./dist/index.cjs"
    },
    "browser": "./dist/index.web.mjs",
    "default": "./dist/index.mjs"
  },
  "targets": {
    "main": {
      "context": "node",
      "outputFormat": "commonjs",
      "distDir": "dist",
      "isLibrary": true,
      "includeNodeModules": [
        "lodash-es"
      ]
    },
    "module": {
      "context": "node",
      "outputFormat": "esmodule",
      "distDir": "dist",
      "isLibrary": true
    },
    "browser": {
      "context": "browser",
      "outputFormat": "esmodule",
      "distDir": "dist",
      "isLibrary": true,
      "includeNodeModules": [
        "buffer",
        "crypto"
      ]
    }
  },
  "files": [
    "dist"
  ],
  "scripts": {
    "check": "tsc --noEmit",
    "build": "del-cli ./dist && parcel build",
    "lint": "eslint .",
    "test": "vitest run --dir tests",
    "coverage": "del-cli ./coverage && npm test -- --coverage --reporter=verbose --reporter=junit",
    "docs": "typedoc"
  },
  "keywords": [
    "nexa",
    "blockchain"
  ],
  "repository": {
    "type": "git",
    "url": "https://gitlab.com/nexa/libnexa-ts"
  },
  "author": "vgrunner",
  "license": "MIT",
  "dependencies": {
    "bn.js": "^5.2.1",
    "bs58": "^6.0.0",
    "elliptic": "^6.6.1",
    "js-big-decimal": "^2.2.0",
    "lodash-es": "^4.17.21"
  },
  "devDependencies": {
    "@parcel/packager-ts": "^2.13.3",
    "@parcel/transformer-typescript-types": "^2.13.3",
    "@types/bn.js": "^5.1.6",
    "@types/elliptic": "^6.4.18",
    "@types/lodash-es": "^4.17.12",
    "@types/node": "^22.10.10",
    "@vitest/coverage-v8": "^3.0.4",
    "buffer": "^6.0.3",
    "crypto-browserify": "^3.12.1",
    "del-cli": "^6.0.0",
    "eslint": "^9.19.0",
    "parcel": "^2.13.3",
    "typedoc": "^0.27.6",
    "typedoc-plugin-markdown": "^4.4.1",
    "typedoc-plugin-rename-defaults": "^0.7.2",
    "typescript": "^5.7.3",
    "typescript-eslint": "^8.22.0",
    "vitest": "^3.0.4"
  },
  "@parcel/resolver-default": {
    "packageExports": true
  }
}

🤔 Expected Behavior

Build success, and browser target should produce file with crypto-browserify instead of crypto

😯 Current Behavior

The crypto-browserify module installed, but build failed with error:

🚨 Build failed.

@parcel/core: Failed to resolve 'crypto' from './src/utils/buffer.utils.ts'

  path\src\utils\buffer.utils.ts:1:20
  > 1 | import crypto from "crypto";
  >   |                    ^^^^^^^^
    2 | import ValidationUtils from "./validation.utils";
    3 |

@parcel/resolver-default: External dependency "crypto-browserify" is not declared in package.json.

💁 Possible Solution

It seems that it started to happen after v2.9.0: #9843 (comment)

🔦 Context

I want to support both nodejs and browser targets for my library, but i need to polyfill the crypto module.
the Buffer successfully polyfilled with buffer, but crypto is failing.

💻 Code Sample

https://gitlab.com/nexa/libnexa-ts/-/blob/main/src/crypto/hash.ts?ref_type=heads#L14

🌍 Your Environment

Software Version(s)
Parcel 2.13.1
Node 22.12.0
npm/Yarn 10.9.2
Operating System windows
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

1 participant