oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
71.82k stars 2.56k forks source link

Add CommonJS support for Bun.Transpiler #8168

Open Kleywalker opened 5 months ago

Kleywalker commented 5 months ago

What is the problem this feature would solve?

In this issue #5105 a workaround is provided to eval typescript code by transpiling it with with Bun.Transpiler. Like in this repository https://github.com/Kleywalker/bun-eval the Bun.Transpiler does not result in an evaluable code. In this example the code

import { kebab } from "case"; 
const result = kebab('Test Content'); 
export default result;

transpiles to the code

import {kebab} from "case";
const result = kebab("Test Content");
export default result;

which does not work with eval.

What is the feature you are proposing to solve the problem?

With tsc this code

import * as tsc from "typescript";
const content = `import { kebab } from "case"; 
const result = kebab('Test Content'); 
export default result;`;
const tscTranspiled = tsc.transpile(content);
console.log(tscTranspiled);

transpiles to this code

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var case_1 = require("case");
var result = (0, case_1.kebab)('Test Content');
exports.default = result;

with works with eval just fine.

What alternatives have you considered?

An alternative would be to create a Bun.eval function that evals code including imports and exports.

No response

Kleywalker commented 5 months ago

Related to issue #7636

Kleywalker commented 2 months ago

Are there any news on this topic yet?

Kleywalker commented 2 days ago

Will this be a feature in the near future?