Repository navigation
bun-types: add json5 and md to the Loader union, document both loaders #38267
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
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
51e4b61
bun-types: add json5 and md to the Loader union, document both loaders
robobun a8cfb97
docs: add json5, xml and md to the bundler page's Loader snippet and …
robobun 7fd86a2
docs: list .markdown in the default extension lines
robobun 7446ea2
bun-types: declare *.md and *.markdown modules as strings
robobun d48fc32
Revert "bun-types: declare *.md and *.markdown modules as strings"
robobun abbd310
docs: explain why bun-types declares no *.md module, pin that in the …
robobun 0966967
docs: cover *.markdown in the md typing note, pin both extensions in …
robobun 8acc997
fixture: pin the missing *.md declarations with static imports
robobun 21cabf4
docs: list .text next to .txt for the text loader
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
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
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,65 @@ | ||
| import type { Loader } from "bun"; | ||
| // Deliberately no `declare module` for "*.md" or "*.markdown" in extensions.d.ts: frameworks declare | ||
| // both themselves (astro/client.d.ts gives them a component default export), and a bun-types | ||
| // `export =` declaration merged into theirs silently turns their default export into a string. | ||
| // Projects that want the md loader's imports typed declare the modules themselves; here both imports | ||
| // fail to resolve and stay untyped. | ||
| // @ts-expect-error | ||
| import markdown from "./README.markdown"; | ||
| // @ts-expect-error | ||
| import html from "./README.md"; | ||
| import { expectAssignable, expectType } from "./utilities"; | ||
|
|
||
| // The bundler implements json5 and md and accepts them by name (`bun build --loader`, | ||
| // `Bun.build({ loader })`, plugin onLoad results), so they are members of the union. | ||
| expectAssignable<Loader>("json5"); | ||
| expectAssignable<Bun.Loader>("md"); | ||
| // @ts-expect-error | ||
| expectAssignable<Loader>("bogus"); | ||
|
|
||
| expectType(html).is<any>(); | ||
| expectType(markdown).is<any>(); | ||
|
|
||
| Bun.build({ | ||
| entrypoints: ["hey"], | ||
| loader: { | ||
| ".json5": "json5", | ||
| ".md": "md", | ||
| }, | ||
| }); | ||
|
|
||
| Bun.build({ | ||
| entrypoints: ["hey"], | ||
| plugins: [ | ||
| { | ||
| name: "loader-names", | ||
| setup(build) { | ||
| build.onLoad({ filter: /\.(json5|md)$/ }, args => { | ||
| // `args.loader` is the default loader for the file, so it is "json5" or "md" here; | ||
| // comparing against a name missing from the union is a no-overlap error. | ||
| if (args.loader === "json5") { | ||
| return { contents: "{ a: 1, }", loader: "json5" }; | ||
| } | ||
|
|
||
| return { contents: "# heading", loader: "md" }; | ||
| }); | ||
|
|
||
| build.onEnd(result => { | ||
| for (const output of result.outputs) { | ||
| if (output.loader === "json5" || output.loader === "md") { | ||
| expectType(output).is<Bun.BuildArtifact>(); | ||
| } | ||
| } | ||
| }); | ||
| }, | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| // Runtime plugins return the same OnLoadResult. | ||
| Bun.plugin({ | ||
| name: "markdown", | ||
| setup(build) { | ||
| build.onLoad({ filter: /\.md$/ }, () => ({ contents: "# heading", loader: "md" })); | ||
| }, | ||
| }); |
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.