Closed jacogr closed 2 years ago
How does this work with deno
?
I am not a deno expert :) Having said that, looking at it while doing this, since the mod.ts
still imports index.ts
(no changes/movements there), I believed it was still correct and would work without any additions.
I may be wrong in that is may base a breakdown where it requires @noble/secp256k1/crypto
, not sure how to solve that one. (Would be solvable no doubt, just not familiar with the deno environment)
I don't think it's solvable. We need to test it, since it seems to be the main lib for secp on deno
Just testing with rollup, we do have "browser": { "crypto": false }
in package.json
-
crypto.ts
& cryptoBrowser.ts
index.ts
-// Global symbol available in browsers only
declare const self: Record<string, any> | undefined;
import nodeCrypto from 'crypto';
export const crypto: { node?: any; web?: any } = {
node: nodeCrypto,
web: typeof self === 'object' && 'crypto' in self ? self.crypto : undefined,
};
The build output via npm build-release
is as follows -
...
const nodeCrypto = {};
const crypto = {
node: nodeCrypto,
web: typeof self === 'object' && 'crypto' in self ? self.crypto : undefined,
};
...
Since randombytes checks web first, it works as expected. So the crypto.ts
looks like complete noise that should not be needed since have the crypto: false
flag anyway.
PS: sorry for the close/re-open, really was a slip of the finger on the trackpad over here.
There are other bundlers besides rollup like Browserify, webpack. They need to be considered also.
Build-release only builds single file for standalone in-browser inclusion btw, it is not used when node.js-required.
Yes, I do understand the role of build-release
very well :) The "crypto": false
in package.json
is not a rollup-specific flag, it is a field for bundlers.
For webpack it ends up with an empty crypto require as well (CJS inputs) -
var __webpack_modules__ = ({
// my comment - the module id for @noble/secp256k1
656: (function ...
...
const crypto_1 = __importDefault(__webpack_require__(159));
... /// more secp256k1 code
const crypto = {
node: crypto_1.default,
web: typeof self === 'object' && 'crypto' in self ? self.crypto : undefined,
};
... // more secp256k1 code
/***/ }), // send 656
/***/ 159:
/***/ (() => {
/* (ignored) */
/***/ })
... // more webpack code, here for the require impl
Steps to replicate -
EDIT: gist here with actual outputs - https://gist.github.com/jacogr/7691c4aa8c94c1c2faf680cca56bcee9 (empty crypto import on these lines - https://gist.github.com/jacogr/7691c4aa8c94c1c2faf680cca56bcee9#file-webpack-secp256k1-L993-L998)
For browserify, the browser
filed is also explicitly mentioned in the handbook for ignores, e.g. https://github.com/browserify/browserify-handbook#browser-field (last example containing "winston": false
)
Could you resolve conflicts?
@jacogr the tests are failing
My attempt on following up on a similar PR in
@noble/hashes
to build this via ESM and CJS.Additions:
While there are now 3 files in the root (index.ts, crypto.ts & cryptoBrowser.ts) I didn't move these to a src folder.
Any comments welcome as well as insights in to where I may have missed the ball.