From a09b99565138bb4bc73a5328397428fb5025817b Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Wed, 19 Jan 2022 23:07:03 -0800 Subject: Bun.Transpiler – API for scanning imports/exports of JSX/TSX/TS/JS files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bench/snippets/scanner/bun.lockb | Bin 0 -> 6186 bytes bench/snippets/scanner/package.json | 6 +++++ bench/snippets/scanner/remix-route.ts | 15 +++++++++++ bench/snippets/scanner/scan-imports-only.js | 15 +++++++++++ bench/snippets/scanner/scan.bun.js | 23 +++++++++++++++++ bench/snippets/scanner/scan.node-esbuild.mjs | 37 +++++++++++++++++++++++++++ 6 files changed, 96 insertions(+) create mode 100755 bench/snippets/scanner/bun.lockb create mode 100644 bench/snippets/scanner/package.json create mode 100644 bench/snippets/scanner/remix-route.ts create mode 100644 bench/snippets/scanner/scan-imports-only.js create mode 100644 bench/snippets/scanner/scan.bun.js create mode 100644 bench/snippets/scanner/scan.node-esbuild.mjs (limited to 'bench/snippets') diff --git a/bench/snippets/scanner/bun.lockb b/bench/snippets/scanner/bun.lockb new file mode 100755 index 000000000..90bb78996 Binary files /dev/null and b/bench/snippets/scanner/bun.lockb differ diff --git a/bench/snippets/scanner/package.json b/bench/snippets/scanner/package.json new file mode 100644 index 000000000..7bb4e8990 --- /dev/null +++ b/bench/snippets/scanner/package.json @@ -0,0 +1,6 @@ +{ + "name": "scan", + "dependencies": { + "esbuild": "^0.14.11" + } +} \ No newline at end of file diff --git a/bench/snippets/scanner/remix-route.ts b/bench/snippets/scanner/remix-route.ts new file mode 100644 index 000000000..dbacf3a4c --- /dev/null +++ b/bench/snippets/scanner/remix-route.ts @@ -0,0 +1,15 @@ +import { useParams } from "remix"; +import type { LoaderFunction, ActionFunction } from "remix"; + +export const loader: LoaderFunction = async ({ params }) => { + console.log(params.postId); +}; + +export const action: ActionFunction = async ({ params }) => { + console.log(params.postId); +}; + +export default function PostRoute() { + const params = useParams(); + console.log(params.postId); +} diff --git a/bench/snippets/scanner/scan-imports-only.js b/bench/snippets/scanner/scan-imports-only.js new file mode 100644 index 000000000..53222d57c --- /dev/null +++ b/bench/snippets/scanner/scan-imports-only.js @@ -0,0 +1,15 @@ +import { readFileSync } from "fs"; +const fixture = ["action", "default", "loader"]; + +const transpiler = new Bun.Transpiler({ + loader: "ts", +}); + +console.time("Get exports"); +const ITERATIONS = parseInt(process.env.ITERATIONS || "1") || 1; +for (let i = 0; i < ITERATIONS; i++) { + const imports = transpiler.scanImports( + readFileSync("remix-route.ts", "utf8") + ); +} +console.timeEnd("Get exports"); diff --git a/bench/snippets/scanner/scan.bun.js b/bench/snippets/scanner/scan.bun.js new file mode 100644 index 000000000..7a34f056e --- /dev/null +++ b/bench/snippets/scanner/scan.bun.js @@ -0,0 +1,23 @@ +import { readFileSync } from "fs"; +const fixture = ["action", "default", "loader"]; +const ITERATIONS = parseInt(process.env.ITERATIONS || "1") || 1; + +const transpiler = new Bun.Transpiler({ + loader: "ts", +}); + +console.time("Get exports"); + +for (let i = 0; i < ITERATIONS; i++) { + const { imports, exports } = transpiler.scan( + readFileSync("remix-route.ts", "utf8") + ); + + for (let j = 0; j < fixture.length; j++) { + if (fixture[j] !== exports[j]) { + throw new Error("Mismatch"); + } + } +} + +console.timeEnd("Get exports"); diff --git a/bench/snippets/scanner/scan.node-esbuild.mjs b/bench/snippets/scanner/scan.node-esbuild.mjs new file mode 100644 index 000000000..946250d1f --- /dev/null +++ b/bench/snippets/scanner/scan.node-esbuild.mjs @@ -0,0 +1,37 @@ +import { build, buildSync } from "esbuild"; +const fixture = ["action", "default", "loader"]; +const ITERATIONS = parseInt(process.env.ITERATIONS || "1") || 1; + +const opts = { + metafile: true, + format: "esm", + platform: "neutral", + write: false, + logLevel: "silent", + entryPoints: ["remix-route.ts"], +}; + +const getExports = ({ metafile }) => { + for (let i = 0; i < fixture.length; i++) { + if (fixture[i] !== metafile.outputs["remix-route.js"].exports[i]) { + throw new Error("Mismatch"); + } + } +}; + +console.time("Get exports"); + +if (!process.env.SYNC) { + var promises = new Array(ITERATIONS); + for (let i = 0; i < ITERATIONS; i++) { + promises[i] = build(opts).then(getExports); + } + + await Promise.all(promises); +} else { + for (let i = 0; i < ITERATIONS; i++) { + getExports(buildSync(opts)); + } +} + +console.timeEnd("Get exports"); -- cgit v1.2.3