FredKSchott / rollup-plugin-polyfill-node

A modern Node.js polyfill for your Rollup bundle.
Other
177 stars 57 forks source link

`crypto` and `util` not polyfilled #18

Open letmaik opened 3 years ago

letmaik commented 3 years ago

The rollup output has some things polyfilled (like buffer, global, and process) but crypto and util remain. According to the README and a quick look at the code they should be supported, but somewhere there's an issue.

Versions:

"@rollup/plugin-commonjs": "^18.1.0",
"@rollup/plugin-node-resolve": "^13.0.0",
"rollup": "^2.23.0",
"rollup-plugin-polyfill-node": "^0.6.2",
letmaik commented 3 years ago

I was able to solve the problem by calling the node-resolve plugin like so:

nodeResolve({
  preferBuiltins: false
})

Without that I believe the resolution stopped too early before the polyfills plugin has a chance to resolve it.

MathiasDeWeerdt commented 3 years ago

I seem to be having the same issue but using preferBuiltins: false doesnt resolve my issue. I am simply trying to use bip39 with rollup in my project.

rollup.config.js

import commonjs from "@rollup/plugin-commonjs";
import nodeResolve from "@rollup/plugin-node-resolve";
import nodePolyfills from "rollup-plugin-polyfill-node";

import babel from "@rollup/plugin-babel";

const extensions = [".js", ".ts"];

export default [
  {
    input: "src/index.ts",
    output: [
      {
        file: "dist/index.esm.js",
        format: "esm",
      },
    ],
    plugins: [
      nodePolyfills({
        include: ["buffer", "stream", "events"],
      }),
      nodeResolve({
        extensions,
        browser: true,
        preferBuiltins: false,
      }),
      commonjs({
        include: "node_modules/**",
        extensions,
      }),
      babel({
        exclude: "node_modules/**",
        babelHelpers: "bundled",
        extensions,
      }),
    ],
  },
];

index.ts

import * as tt from "bip39";

export function testMe() {
    const mnemonic = tt.entropyToMnemonic('00000000000000000000000000000000')
    console.log("mnemonic: ", mnemonic)
}

I keep getting "uncaught ReferenceError: Buffer is not defined".

Running everything in webpack works perfectly fine...