MyGit

bun-v0.0.68

oven-sh/bun

版本发布时间: 2022-01-23 15:30:39

oven-sh/bun最新发布版本:bun-v1.1.38(2024-11-29 19:55:48)

To upgrade:

bun upgrade

bun v0.0.68

This release is mostly focused on bun.js, bun's JavaScript runtime environment. Fixes to bun install will be part of the next release, but the infrastructure work from this release will help with bun install in the next release.

TLDR:

Bun.Transpiler - API access to Bun

const bun = new Bun.Transpiler({
  loader: "tsx", // set default loader
});

// logs transpiled code without resolving imports
console.log(bun.transformSync("export default <div />"));

// return list of imports & exports without resolving imports or printing code
const { imports, exports } = bun.scan(`
  import { Component } from "react";
  export const foo: boolean = true;
`);

console.log({ exports, imports });

Bun.Transpiler exposes part of Bun's JavaScript & TypeScript transpiler from native code to JavaScript, and it's fast.

End-to-end, transpiling this JSX file inside JavaScript via Bun.Transpiler runs:

image

See benchmark code

Bun.Transpiler supports JavaScript plugins with AST access via macros. Macros are not entirely done yet, but simple ones work. There will be docs on this.

Bun.Transpiler is builtin to bun.js, there's nothing extra to import or install. Eventually there will be a WASM build for some of this, but not sure when

However, a transpiler API is not very useful without a way to read/write files to disk.

Node.js fs module implementation

// This works in bun.js now
import {readFileSync} from 'fs';

// require("fs") also works in both .mjs files and .js/.cjs files
require("fs").writeFileSync("foo.txt", "bar!")

// you can also use the node: namespace if you want
import {readlinkSync} from 'node:fs';

You can now use most of the sync functions from Node.js' fs module inside bun.js. These are implemented from scratch in Zig & exposed to JS. Buffer & streams are not implemented yet, but you can pass a Uint8Array or ArrayBuffer where Node accepts a Buffer. The async versions of the functions will come in a future release (this already was a lot of stuff for one release), but generally sync outperforms async for local file access.

fs.realpathSync is about 7x faster in bun.js (50,000 iterations)

image

fs.existsSync runs about 30% faster in bun.js (100,000 iterations)

image

The following functions are implemented:

Bun also includes an implementation of Node's SystemError with pretty printing. Note that since source maps are not implemented yet, sometimes the line:column will be off by a little.

image

This is what the same error looks like in Node

image

Node.js process object

bun.js has more support for the process object from Node.js.

// These work now in bun.js
process.chdir("insert-dir-name-here");
process.cwd();
// arguments used to launch, excluding "run" if via "bun run" for compatibility with npm packages
process.argv;
// current process id
process.pid;
// parent process pid
process.ppid;

// returns bun's version
process.version

process.versions
{
  // fake version for compatibility with npm packages potentially looking this up
  "node": "17.0.0",
  "modules": "67",
  // bun version
  "bun": "0.0.68",
  // git shas/tag of bun dependencies
  "webkit": "96e77eccfde8dc9c207520d8ced856d8bdb8d386",
  "mimalloc": "f412df7a2b64421e1f1d61fde6055a6ea288e8f5",
  "libarchive": "dc321febde83dd0f31158e1be61a7aedda65e7a2",
  "picohttpparser": "066d2b1e9ab820703db0837a7255d92d30f0c9f5",
  "boringssl": "b3ed071ecc4efb77afd0a025ea1078da19578bfd",
  "zlib": "959b4ea305821e753385e873ec4edfaa9a5d49b7",
  "zig": "0.10.0-dev.315+4d05f2ae5"
}

// process.nextTick() is now implemented (currently only supports up to 4 arguments)
process.nextTick(() => console.log("second"));
console.log("first");

More stuff

import.meta in bun.js returns an object with file and dir

const {
  // import.meta.dir returns absolute path to the directory the script is in. sort of like __dirname
  dir,
  // import.meta.file returns absolute path to the script
  file,
} = import.meta;

If you look hard enough, you'll also find a new subcommand for a very incomplete but fast Jest-like test runner. Hopefully will talk more about that next release or the one after.

Thanks

相关地址:原始地址 下载(tar) 下载(zip)

1、 bun-darwin-aarch64.dSYM.tar.gz 14.63MB

2、 bun-darwin-aarch64.zip 9.84MB

3、 bun-darwin-x64.dSYM.tar.gz 111.82MB

4、 bun-darwin-x64.zip 11.48MB

5、 bun-linux-x64.zip 23.55MB

查看:2022-01-23发行的版本