-
Notifications
You must be signed in to change notification settings - Fork 774
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prioritise
workerd
condition when bundling (#2629)
* Prioritise `workerd` condition when bundling Ref: https://runtime-keys.proposal.wintercg.org/#workerd * fixup! Prioritise `workerd` condition when bundling * fixup! Prioritise `workerd` condition when bundling * fixup! Prioritise `workerd` condition when bundling
- Loading branch information
Showing
9 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
"wrangler": minor | ||
--- | ||
|
||
Prefer the `workerd` `exports` condition when bundling. | ||
|
||
This can be used to build isomorphic libraries that have different implementations depending on the JavaScript runtime they're running in. | ||
When bundling, Wrangler will try to load the [`workerd` key](https://runtime-keys.proposal.wintercg.org/#workerd). | ||
This is the [standard key](https://runtime-keys.proposal.wintercg.org/#workerd) for the Cloudflare Workers runtime. | ||
Learn more about the [conditional `exports` field here](https://nodejs.org/api/packages.html#conditional-exports). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Isomorphic Package Example | ||
|
||
This package implements an isomorphic library that generates cryptographically-strong pseudorandom numbers. | ||
What this package does isn't really important here, the key part is the [`package.json`](./package.json)'s `exports` field. | ||
Conditional exports provide a way to load a different file depending on where the module is being imported from. | ||
By default, Wrangler will try to look for a [**`workerd`** key](https://runtime-keys.proposal.wintercg.org/#workerd). | ||
This allows you as a library developer to implement different behaviour for different JavaScript runtimes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "isomorphic-random-example", | ||
"version": "0.0.1", | ||
"private": true, | ||
"description": "Isomorphic secure-random library, demonstrating `exports` use with Workers", | ||
"exports": { | ||
"node": "./src/node.js", | ||
"workerd": "./src/workerd.mjs" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const crypto = require("node:crypto"); | ||
|
||
module.exports.randomBytes = function (length) { | ||
return new Uint8Array(crypto.randomBytes(length)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function randomBytes(length) { | ||
return crypto.getRandomValues(new Uint8Array(length)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters