Skip to content

Commit

Permalink
allow importing bun.lock (+ types for it)
Browse files Browse the repository at this point in the history
  • Loading branch information
RiskyMH committed Jan 8, 2025
1 parent 76800b0 commit b65a7f3
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 10 deletions.
5 changes: 5 additions & 0 deletions packages/bun-types/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ declare module "*.toml" {
var contents: any;
export = contents;
}

declare module '*bun.lock' {
var contents: import("bun").BunLockFile;
export = contents;
}
71 changes: 71 additions & 0 deletions packages/bun-types/bun.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6417,6 +6417,77 @@ declare module "bun" {
*/
timestamp?: number | Date,
): Buffer;

/**
* Types for `bun.lock`
*/
type BunLockFile = {
lockfileVersion: 0;
workspaces: {
[workspace: string]: {
name?: string;
version?: string;
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
optionalDependencies?: Record<string, string>;
peerDependencies?: Record<string, string>;
};
};
trustedDependencies?: string[];

/**
* ```
* INFO = { prod/dev/optional/peer dependencies, os, cpu, libc (TODO), bin, binDir }
*
* npm -> [ "name@version", registry (TODO: remove if default), INFO, integrity]
* symlink -> [ "name@link:path", INFO ]
* folder -> [ "name@file:path", INFO ]
* workspace -> [ "name@workspace:path", INFO ]
* tarball -> [ "name@tarball", INFO ]
* root -> [ "name@root:", { bin, binDir } ]
* git -> [ "name@git+repo", INFO, .bun-tag string (TODO: remove this) ]
* github -> [ "name@github:user/repo", INFO, .bun-tag string (TODO: remove this) ]
* ```
* */
packages: {
[package: string]:
| {
/** npm */
1: string;
2: string;
3: BunLockFilePackageInfo;
4: string;
}
| {
/** symlink, folder, tarball, workspace */
1: string;
2: BunLockFilePackageInfo;
}
| {
/** git, github */
1: string;
2: BunLockFilePackageInfo;
3: string;
}
| {
/** root */
1: string;
2: Pick<BunLockFilePackageInfo, 'bin' | 'binDir'>
}
| ({} & []);
};
};

type BunLockFilePackageInfo = {
dependencies?: Record<string, string>;
optionalDependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
peerDependencies?: Record<string, string>;
os?: string | string[];
cpu?: string | string[];
bin?: Record<string, string>;
binDir?: string;
};
}

// extends lib.dom.d.ts
Expand Down
2 changes: 2 additions & 0 deletions src/bun.js/module_loader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2326,6 +2326,8 @@ pub const ModuleLoader = struct {
loader = .ts;
} else if (attribute.eqlComptime("tsx")) {
loader = .tsx;
} else if (attribute.eqlComptime("bun.lock")) {
loader = .json;
}
}

Expand Down
15 changes: 12 additions & 3 deletions src/fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1608,8 +1608,13 @@ pub const PathName = struct {

// Strip off the extension
if (strings.lastIndexOfChar(base, '.')) |dot| {
ext = base[dot..];
base = base[0..dot];
if (strings.endsWith(base, "bun.lock")) {
ext = "bun.lock";
base = "";
} else {
ext = base[dot..];
base = base[0..dot];
}
} else {
ext = "";
}
Expand Down Expand Up @@ -1713,7 +1718,11 @@ pub const Path = struct {

pub fn isJSONCFile(this: *const Path) bool {
const str = this.name.filename;
if (strings.eqlComptime(str, "package.json")) {
if (strings.eqlComptime(str, "package.json") or strings.eqlComptime(str, "bun.lock")) {
return true;
}

if (strings.hasSuffixComptime(str, ".jsonc")) {
return true;
}

Expand Down
24 changes: 17 additions & 7 deletions src/options.zig
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,7 @@ pub const Loader = enum(u8) {
.{ "css", .css },
.{ "file", .file },
.{ "json", .json },
.{ "jsonc", .json },
.{ "toml", .toml },
.{ "wasm", .wasm },
.{ "node", .napi },
Expand All @@ -769,6 +770,7 @@ pub const Loader = enum(u8) {
.{ "sqlite", .sqlite },
.{ "sqlite_embedded", .sqlite_embedded },
.{ "html", .html },
.{ "bun.lock", .json },
});

pub const api_names = bun.ComptimeStringMap(Api.Loader, .{
Expand All @@ -783,6 +785,7 @@ pub const Loader = enum(u8) {
.{ "css", .css },
.{ "file", .file },
.{ "json", .json },
.{ "jsonc", .json },
.{ "toml", .toml },
.{ "wasm", .wasm },
.{ "node", .napi },
Expand All @@ -793,6 +796,7 @@ pub const Loader = enum(u8) {
.{ "sh", .file },
.{ "sqlite", .sqlite },
.{ "html", .html },
.{ "bun.lock", .json },
});

pub fn fromString(slice_: string) ?Loader {
Expand Down Expand Up @@ -908,6 +912,8 @@ const default_loaders_posix = .{
.{ ".txt", .text },
.{ ".text", .text },
.{ ".html", .html },
.{ ".jsonc", .json },
.{ "bun.lock", .json },
};
const default_loaders_win32 = default_loaders_posix ++ .{
.{ ".sh", .bunsh },
Expand Down Expand Up @@ -1286,16 +1292,18 @@ pub fn definesFromTransformOptions(

const default_loader_ext_bun = [_]string{".node"};
const default_loader_ext = [_]string{
".jsx", ".json",
".js", ".mjs",
".cjs", ".css",
".jsx", ".json",
".js", ".mjs",
".cjs", ".css",

// https://devblogs.microsoft.com/typescript/announcing-typescript-4-5-beta/#new-file-extensions
".ts", ".tsx",
".mts", ".cts",
".ts", ".tsx",
".mts", ".cts",

".toml", ".wasm",
".txt", ".text",
".toml", ".wasm",
".txt", ".text",

"bun.lock", ".jsonc",
};

// Only set it for browsers by default.
Expand All @@ -1314,12 +1322,14 @@ const node_modules_default_loader_ext = [_]string{
".toml",
".txt",
".json",
".jsonc",
".css",
".tsx",
".cts",
".wasm",
".text",
".html",
"bun.lock",
};

pub const ResolveFileExtensions = struct {
Expand Down

0 comments on commit b65a7f3

Please sign in to comment.