Bundling for Node.js fails with @rollup/plugin-node-resolve>=11.0.0
See original GitHub issueDescribe the bug
Your source code checks for something like this: typeof crypto !== 'undefined' && crypto.getRandomValues && crypto.getRandomValues.bind(crypto) || typeof msCrypto !== 'undefined' && typeof msCrypto.getRandomValues === 'function' && msCrypto.getRandomValues.bind(msCrypto);
to find out whether crypto is available.
I’m not sure exactly where it goes wrong, but this package inside a compiled js file with rollup, will never import crypto and only check the globals like mentioned above.
How to reproduce
With rollup and the following rollup.config, compile a file that uses “uuid”:
import typescript from "@rollup/plugin-typescript";
import commonjs from "@rollup/plugin-commonjs";
import nodeResolve from "@rollup/plugin-node-resolve";
import json from "@rollup/plugin-json";
export default {
input: ["myfile.ts"],
output: {
dir: "dist",
format: "cjs",
},
plugins: [
commonjs(),
nodeResolve(),
json(),
typescript(),
],
};
myfile.ts
import { v4 as uuidv4 } from "uuid";
console.log(uuidv4());
Expected behavior
The example should have something like const crypto = require("crypto")
somewhere in the compiled code. Currently, it only checks whether it is available globally, but in my use case I’m not in a browser environment, but a nodejs environment, so it would need to get imported to work.
As a note: I can see from the source that rng.js seems to be set-up correctly, but rng-browser.js is doing the unsupported check in a node environment. So the question really is, why I’m getting the browser package instead of the node one. Can I control this? (rollup is already set-up for node, aka cjs)
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
You need to specify
nodeResolve({exportConditions: ["node"]})
asnode
is not one of the default conditions, see the documentation: https://github.com/rollup/plugins/tree/master/packages/node-resolve#exportconditionsKeeping this open until https://github.com/rollup/plugins/issues/695 has settled.