Skip to content

Commit 667a688

Browse files
committed
To polyfill crypto on CJS only - `this.resolve()` returns `null`
1 parent c9719aa commit 667a688

File tree

3 files changed

+6
-17
lines changed

3 files changed

+6
-17
lines changed

dist/index.cjs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,4 @@
11
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2+
let crypto = require("crypto");
23

3-
//#region src/crypto-polyfill.ts
4-
var _ref, _ref$crypto, _crypto, _crypto$randomUUID;
5-
function hex(byte) {
6-
return byte.toString(16).padStart(2, "0");
7-
}
8-
(_ref$crypto = (_ref = globalThis).crypto) !== null && _ref$crypto !== void 0 || (_ref.crypto = {});
9-
(_crypto$randomUUID = (_crypto = globalThis.crypto).randomUUID) !== null && _crypto$randomUUID !== void 0 || (_crypto.randomUUID = function randomUUID() {
10-
const bytes = new Uint8Array(16);
11-
for (let i = 0; i < 16; i++) bytes[i] = Math.trunc(Math.random() * 256);
12-
return hex(bytes[0]) + hex(bytes[1]) + hex(bytes[2]) + hex(bytes[3]) + "-" + hex(bytes[4]) + hex(bytes[5]) + "-" + hex(bytes[6]) + hex(bytes[7]) + "-" + hex(bytes[8]) + hex(bytes[9]) + "-" + hex(bytes[10]) + hex(bytes[11]) + hex(bytes[12]) + hex(bytes[13]) + hex(bytes[14]) + hex(bytes[15]);
13-
});
14-
const randomUUID = globalThis.crypto.randomUUID.bind(globalThis.crypto);
15-
var crypto_polyfill_default = globalThis.crypto;
16-
17-
//#endregion
18-
exports.randomUUID = randomUUID;
4+
exports.randomUUID = crypto.randomUUID;

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"verbatimModuleSyntax": true,
2121

2222
"paths": {
23-
"crypto": ["./src/crypto-polyfill.ts"]
23+
// "crypto": ["./src/crypto-polyfill.ts"]
2424
}
2525
},
2626
"include": ["src/**/*"],

tsdown.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ const config = defineConfig([
1919
exports: true,
2020
fixedExtension: true,
2121
shims: true,
22+
alias: {
23+
"crypto": "./src/crypto-polyfill.ts",
24+
},
2225
},
2326
]);
2427

0 commit comments

Comments
 (0)