aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--packages/bun-types/README.md28
-rw-r--r--packages/bun-types/scripts/bundle.ts27
-rw-r--r--src/cli/init_command.zig22
-rw-r--r--src/cli/tsconfig-for-init.json6
4 files changed, 63 insertions, 20 deletions
diff --git a/packages/bun-types/README.md b/packages/bun-types/README.md
index fc4128388..19964c6f8 100644
--- a/packages/bun-types/README.md
+++ b/packages/bun-types/README.md
@@ -1,4 +1,4 @@
-# Bun TypeScript type definitions
+# TypeScript types for Bun
<p align="center">
<a href="https://bun.sh"><img src="https://bun.sh/logo@2x.png" alt="Logo"></a>
@@ -21,31 +21,25 @@ Add this to your `tsconfig.json` or `jsconfig.json`:
```jsonc-diff
{
- "compilerOptions": {
- // ...
-+ "types": ["bun-types"]
- }
+
++ "types": ["bun-types"],
+
+ // other options...
}
```
# Contributing
-`bun-types` is generated via [./bundle.ts](./scripts/bundle.ts).
+`bun-types` is generated via [./scripts/bundle.ts](./scripts/bundle.ts).
-## Adding a new file
+To add a new file, add it under `packages/bun-types`. Then add a [triple-slash directive](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html) pointing to it inside [./index.d.ts](./index.d.ts).
-1. Add it to [./index.d.ts](./index.d.ts)
-
-## How to generate types.d.ts
-
-[`./bundle.ts`](./bundle.ts) merges the types in this folder into a single file.
+```diff
++ /// <reference path="./newfile.d.ts" />
+```
-To run it:
+[`./bundle.ts`](./bundle.ts) merges the types in this folder into a single file. To run it:
```bash
bun build
```
-
-# Generated docs
-
-**[📚 See here for docs](https://oven-sh.github.io/bun-types/)**
diff --git a/packages/bun-types/scripts/bundle.ts b/packages/bun-types/scripts/bundle.ts
index 1f9403513..aa91a3dfa 100644
--- a/packages/bun-types/scripts/bundle.ts
+++ b/packages/bun-types/scripts/bundle.ts
@@ -50,7 +50,7 @@ const packageJSON = {
description:
"Type definitions for Bun, an incredibly fast JavaScript runtime",
types: "types.d.ts",
- files: ["types.d.ts", "README.md"],
+ files: ["types.d.ts", "README.md", "tsconfig.json"],
private: false,
keywords: ["bun", "bun.js", "types"],
repository: "https://github.com/oven-sh/bun",
@@ -62,6 +62,31 @@ await write(
JSON.stringify(packageJSON, null, 2) + "\n",
);
+const tsConfig = {
+ compilerOptions: {
+ lib: ["ESNext"],
+ target: "ESNext",
+ module: "ESNext",
+ moduleResolution: "bundler",
+ moduleDetection: "force",
+ resolveJsonModule: true,
+ strict: true,
+ downlevelIteration: true,
+ skipLibCheck: true,
+ jsx: "react-jsx",
+ allowImportingTsExtensions: true,
+ allowSyntheticDefaultImports: true,
+ forceConsistentCasingInFileNames: true,
+ allowJs: true,
+ types: ["bun-types"],
+ },
+};
+
+await write(
+ resolve(folder, "tsconfig.json"),
+ JSON.stringify(tsConfig, null, 2) + "\n",
+);
+
await write(
resolve(folder, "README.md"),
file(resolve(import.meta.dir, "..", "README.md")),
diff --git a/src/cli/init_command.zig b/src/cli/init_command.zig
index a7abd02e8..1b95d47fa 100644
--- a/src/cli/init_command.zig
+++ b/src/cli/init_command.zig
@@ -282,6 +282,22 @@ pub const InitCommand = struct {
break :brk true;
};
+ const needs_typescript_dependency = brk: {
+ if (fields.object.get("devDependencies")) |deps| {
+ if (deps.hasAnyPropertyNamed(&.{"typescript"})) {
+ break :brk false;
+ }
+ }
+
+ if (fields.object.get("peerDependencies")) |deps| {
+ if (deps.hasAnyPropertyNamed(&.{"typescript"})) {
+ break :brk false;
+ }
+ }
+
+ break :brk true;
+ };
+
if (needs_dev_dependencies) {
var dev_dependencies = fields.object.get("devDependencies") orelse js_ast.Expr.init(js_ast.E.Object, js_ast.E.Object{}, logger.Loc.Empty);
const version = comptime brk: {
@@ -293,6 +309,12 @@ pub const InitCommand = struct {
try dev_dependencies.data.e_object.putString(alloc, "bun-types", comptime std.fmt.comptimePrint("^{any}", .{version.fmt("")}));
try fields.object.put(alloc, "devDependencies", dev_dependencies);
}
+
+ if (needs_typescript_dependency) {
+ var peer_dependencies = fields.object.get("peer_dependencies") orelse js_ast.Expr.init(js_ast.E.Object, js_ast.E.Object{}, logger.Loc.Empty);
+ try peer_dependencies.data.e_object.putString(alloc, "typescript", "^5.0.0");
+ try fields.object.put(alloc, "peerDependencies", peer_dependencies);
+ }
}
write_package_json: {
diff --git a/src/cli/tsconfig-for-init.json b/src/cli/tsconfig-for-init.json
index 5c0ced989..b8cfbf636 100644
--- a/src/cli/tsconfig-for-init.json
+++ b/src/cli/tsconfig-for-init.json
@@ -6,10 +6,12 @@
"module": "esnext",
"target": "esnext",
"moduleResolution": "bundler",
+ "moduleDetection": "force",
+ "allowImportingTsExtensions": true,
"strict": true,
"downlevelIteration": true,
"skipLibCheck": true,
- "jsx": "react-jsx",
+ "jsx": "preserve",
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"allowJs": true,
@@ -17,4 +19,4 @@
"bun-types" // add Bun global
]
}
-} \ No newline at end of file
+}