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

Use package entry points to avoid building of node builtins #323

Open
Aaronius opened this issue Dec 13, 2021 · 3 comments
Open

Use package entry points to avoid building of node builtins #323

Aaronius opened this issue Dec 13, 2021 · 3 comments

Comments

@Aaronius
Copy link

Aaronius commented Dec 13, 2021

When using a bundler like Vite (which does not polyfill node builtins) to build an app that has @ensdomains/address-encoder, compilation fails because @ensdomains/address-encoder depends on node's crypto module:

image

This can fixed by using package entry points for address-encoder to be more specific about the target environments:

https://nodejs.org/api/packages.html#package-entry-points
https://webpack.js.org/guides/package-exports/#providing-commonjs-and-esm-version-stateless

More context can be found here:

vitejs/vite#6085

@cctanfujun
Copy link

same err

@imsys
Copy link

imsys commented Jan 29, 2022

I did some tests using the files in the lib folder:
index.js -- works
index.modern.js -- doesn't work
index.module.js -- doesn't work
index.umd.js -- works

As the package.json has:

"main": "lib/index.js",
"module": "lib/index.module.js",

Vite ends up using what is set in the module field. If we delete the module field line, it uses the main and it works.

We could also set up:

"main": "lib/index.js",
"module": "lib/index.module.js",
"types": "lib/index.d.ts",
"exports": {
  "require": "./lib/index.js",
  "import": "./lib/index.umd.js"
},

And it would use the import line. That index.module.js probably would work on node running as type:module, but UMD should probably work with that too. I would suggest to drop the module field at the root. But maybe leave the "main" for backward compatibility.

"source": "src/index.ts",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"exports": {
  "node": {
    "require": "./lib/index.js",
    "import": "./lib/index.module.js"
  },
  "browser": "./lib/index.umd.js"
},

This also works, and Vite/RollUp picks what is in the "browser" field.

@imsys
Copy link

imsys commented Jan 29, 2022

As a workaround for now, there are two options:
Point to the UMD file in Vite:

resolve:{
    alias:{
        // ↓ see https://github.com/vitejs/vite/issues/6085
        "@ensdomains/address-encoder": "@ensdomains/address-encoder/lib/index.umd.js",
    },
}

Or use a polyfill:
npm i -D crypto-browserify

resolve: {
  alias: {
    'crypto': 'crypto-browserify',
  },
}

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