Support `bun` (#4916)

Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
This commit is contained in:
Muspi Merol 2024-07-14 16:46:33 +08:00 committed by GitHub
parent 262f1c4a85
commit c03b2dcd02
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 69 additions and 2 deletions

View File

@ -225,6 +225,36 @@ jobs:
paths: "test-results/*.xml"
if: always()
test-bun:
runs-on: ${{ matrix.os }}
needs: [build-core]
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v4
- name: Download build artifact
uses: actions/download-artifact@v4
with:
name: core-build-${{ runner.os }}
path: ./dist/
- uses: oven-sh/setup-bun@v1
- name: install test requirements
working-directory: src/test-bun
run: |
bun --version
bun install
- name: run bun tests
working-directory: src/test-bun
run: bun smoke-test.js
test-deno:
runs-on: ${{ matrix.os }}

View File

@ -262,6 +262,10 @@ export async function calculateDirname(): Promise<string> {
}
let fileName = ErrorStackParser.parse(err)[0].fileName!;
if (IN_NODE && !fileName.startsWith("file://")) {
fileName = `file://${fileName}`; // Error stack filenames are not starting with `file://` in `Bun`
}
if (IN_NODE_ESM) {
const nodePath = await import("node:path");
const nodeUrl = await import("node:url");

View File

@ -5,8 +5,7 @@ export const IN_NODE =
typeof process === "object" &&
typeof process.versions === "object" &&
typeof process.versions.node === "string" &&
typeof process.browser ===
"undefined"; /* This last condition checks if we run the browser shim of process */
!process.browser; /* This last condition checks if we run the browser shim of process */
/** @private */
export const IN_NODE_COMMONJS =
@ -19,6 +18,9 @@ export const IN_NODE_COMMONJS =
/** @private */
export const IN_NODE_ESM = IN_NODE && !IN_NODE_COMMONJS;
/** @private */
export const IN_BUN = typeof globalThis.Bun !== "undefined";
/** @private */
export const IN_DENO = typeof Deno !== "undefined"; // just in case...
@ -55,6 +57,7 @@ export function detectEnvironment(): Record<string, boolean> {
IN_NODE: IN_NODE,
IN_NODE_COMMONJS: IN_NODE_COMMONJS,
IN_NODE_ESM: IN_NODE_ESM,
IN_BUN: IN_BUN,
IN_DENO: IN_DENO,
IN_BROWSER: IN_BROWSER,
IN_BROWSER_MAIN_THREAD: IN_BROWSER_MAIN_THREAD,

17
src/test-bun/package.json Normal file
View File

@ -0,0 +1,17 @@
{
"name": "test-bun",
"module": "smoke-test.js",
"type": "module",
"scripts": {
"dev": "bun --watch smoke-test.js"
},
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"pyodide": "../../dist/"
}
}

View File

@ -0,0 +1,13 @@
import { loadPyodide } from "pyodide";
console.time("[load pyodide]");
const pyodide = await loadPyodide();
console.timeEnd("[load pyodide]");
console.time("[run pyodide]");
const result = await pyodide.runPythonAsync(`
3+4
`);
console.timeEnd("[run pyodide]");
console.log("result:", result.toString());