Replies: 1 comment 1 reply
-
You could try with a dynamic import like so: // use user-passed file
const givenPath = process.argv[2];
// dynamic import
// warning: configured like so it only runs files in the current project folder
// you should probably consider converting relative paths to absolute when importing
const { default: importedFn } = await import(`./${givenPath}`);
importedFn(); Compile your code: bun build ./index.ts --compile --outfile index Run JS/TS files that export a default function: export default function() {
console.log('Hello, world!');
} $ ./index example.ts
Hello, world! Warning While this might be a solution, you should always remember you're not actually creating a different process but you're importing and executing a function directly on your main process, so make sure to only run trusted code in this way. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Imagine I compile a CLI using Bun into a standalone executable (https://bun.sh/docs/bundler/executables).
Users install my binary by downloading it on their machine.
Then, I want my CLI to execute a JS file using the Bun runtime that is bundled in this executable.
E.g. instead of running:
it would look like this:
my-cli-containing-bun # this script runs "bun ./script.js" with "bun" being the engine from my CLI
Is that possible? If yes, how?
I would guess that I need to spawn a subprocess, but instead of spawning
bun run
I need to run the internal Bun engine…Beta Was this translation helpful? Give feedback.
All reactions