Repository navigation
bun-types: declare the values that exports.replace of Bun.Transpiler accepts #44193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robobun
wants to merge
7
commits into
main
Choose a base branch
from
robobun/7c4d240e/transpiler-exports-replace-type
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
33cf480
bun-types: declare the values exports.replace of Bun.Transpiler accepts
robobun ded39d0
bun-types: say how to write the keys and names of exports.replace
robobun c92c229
bun-types: say how to keep a pair of exports.replace in a variable
robobun 0184e75
Merge remote-tracking branch 'origin/main' into robobun/7c4d240e/tran…
robobun d40aaa4
bun-types: let the default key of exports.replace take a pair
robobun 71310c5
bun-types: say which exports a key of exports.replace names
robobun c6bc384
bun-types: state the limits of export lists and of pair names for exp…
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,150 @@ | ||
| import { expectType } from "./utilities"; | ||
|
|
||
| type ExportsReplace = NonNullable<NonNullable<Bun.TranspilerOptions["exports"]>["replace"]>; | ||
|
|
||
| expectType<ExportsReplace[string]>().is< | ||
| | string | ||
| | number | ||
| | boolean | ||
| | null | ||
| | undefined | ||
| | readonly [name: string, value: string | number | boolean | null | undefined] | ||
| >(); | ||
|
|
||
| // The example in the JSDoc of `exports.replace`. | ||
| { | ||
| const transpiler = new Bun.Transpiler({ | ||
| loader: "ts", | ||
| exports: { | ||
| replace: { | ||
| revalidate: 60, | ||
| getStaticProps: ["__N_SSG", true], | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| expectType( | ||
| transpiler.transformSync(` | ||
| export const revalidate = readConfig().revalidate; | ||
| export const getStaticProps = async () => ({ props: await load() }); | ||
| `), | ||
| ).is<string>(); | ||
| } | ||
|
|
||
| // A plain value becomes the value of the export. | ||
| new Bun.Transpiler({ | ||
| exports: { | ||
| replace: { | ||
| aString: "bar", | ||
| aNumber: 60, | ||
| aBoolean: true, | ||
| aNull: null, | ||
| anUndefined: undefined, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| // `[name, value]` replaces the export with an export called `name`. | ||
| new Bun.Transpiler({ | ||
| exports: { | ||
| replace: { | ||
| withString: ["__NAME", "bar"], | ||
| withNumber: ["__NAME", 60], | ||
| withBoolean: ["__N_SSG", true], | ||
| withNull: ["__NAME", null], | ||
| withUndefined: ["__NAME", undefined], | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| // The runtime only reads the pair, so a readonly tuple is accepted. | ||
| // Two keys can take one name, when each file exports only one of the two keys. | ||
| { | ||
| const entry = ["__N_SSG", true] as const; | ||
| new Bun.Transpiler({ exports: { replace: { getStaticProps: entry, getStaticPaths: entry } } }); | ||
| } | ||
|
|
||
| // A map that is built before the constructor call. | ||
| { | ||
| const replace: ExportsReplace = {}; | ||
| for (const name of ["getStaticProps", "getStaticPaths"]) { | ||
| replace[name] = ["__N_SSG", true]; | ||
| } | ||
| replace.revalidate = 60; | ||
| new Bun.Transpiler({ exports: { eliminate: ["loader"], replace } }); | ||
| } | ||
|
|
||
| // Maps with a type of their own, such as the type of the declaration before this one. | ||
| { | ||
| const strings: Record<string, string> = { version: "1.0.0" }; | ||
| new Bun.Transpiler({ exports: { replace: strings } }); | ||
|
|
||
| const pairs: Record<string, number | [string, boolean]> = { revalidate: 60, getStaticProps: ["__N_SSG", true] }; | ||
| new Bun.Transpiler({ exports: { replace: pairs } }); | ||
| } | ||
|
|
||
| // A pair or a map in a variable. The call gives no type to a literal that is written before it. | ||
| { | ||
| const constMap = { revalidate: 60, getStaticProps: ["__N_SSG", true] } as const; | ||
| new Bun.Transpiler({ exports: { replace: constMap } }); | ||
|
|
||
| const mapWithConstPair = { revalidate: 60, getStaticProps: ["__N_SSG", true] as const }; | ||
| new Bun.Transpiler({ exports: { replace: mapWithConstPair } }); | ||
|
|
||
| const checkedMap = { revalidate: 60, getStaticProps: ["__N_SSG", true] } satisfies ExportsReplace; | ||
| new Bun.Transpiler({ exports: { replace: checkedMap } }); | ||
|
|
||
| const typedPair: [string, boolean] = ["__N_SSG", true]; | ||
| new Bun.Transpiler({ exports: { replace: { getStaticProps: typedPair } } }); | ||
|
|
||
| const options: Bun.TranspilerOptions = { exports: { replace: { getStaticProps: ["__N_SSG", true] } } }; | ||
| new Bun.Transpiler(options); | ||
|
|
||
| const inferredPair = ["__N_SSG", true]; | ||
| // @ts-expect-error - without a type, TypeScript infers (string | boolean)[] for the pair | ||
| new Bun.Transpiler({ exports: { replace: { getStaticProps: inferredPair } } }); | ||
|
|
||
| const inferredMap = { revalidate: 60, getStaticProps: ["__N_SSG", true] }; | ||
| // @ts-expect-error - without a type, TypeScript infers (string | boolean)[] for the pair | ||
| new Bun.Transpiler({ exports: { replace: inferredMap } }); | ||
| } | ||
|
|
||
| // The `default` key takes a value or a pair. | ||
| { | ||
| new Bun.Transpiler({ exports: { replace: { default: 60 } } }); | ||
| new Bun.Transpiler({ exports: { replace: { default: "bar", revalidate: 60 } } }); | ||
| new Bun.Transpiler({ exports: { replace: { default: ["__N_SSG", true] } } }); | ||
|
|
||
| const entry = ["__N_SSG", true] as const; | ||
| new Bun.Transpiler({ exports: { replace: { default: entry } } }); | ||
|
|
||
| const replace: ExportsReplace = {}; | ||
| replace.default = 60; | ||
| replace.default = ["__N_SSG", true]; | ||
| } | ||
|
|
||
| // The constructor throws for each of these values. | ||
| new Bun.Transpiler({ | ||
| exports: { | ||
| replace: { | ||
| // @ts-expect-error - bigint is not a replacement value | ||
| aBigInt: 1n, | ||
| // @ts-expect-error - symbol is not a replacement value | ||
| aSymbol: Symbol("s"), | ||
| // @ts-expect-error - function is not a replacement value | ||
| aFunction: () => 1, | ||
| // @ts-expect-error - object is not a replacement value | ||
| anObject: { value: 1 }, | ||
| // @ts-expect-error - the pair needs a value | ||
| nameOnly: ["__NAME"], | ||
| // @ts-expect-error - the pair has two elements | ||
| threeElements: ["__NAME", true, 1], | ||
| // @ts-expect-error - the value of the pair is one of the plain values | ||
| objectInPair: ["__NAME", { value: 1 }], | ||
| // @ts-expect-error - the value of the pair cannot be another pair | ||
| pairInPair: ["__NAME", ["__OTHER", 1]], | ||
| // @ts-expect-error - the name of the pair is a string | ||
| numberAsName: [1, true], | ||
| }, | ||
| }, | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.