oven-sh / bun

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

Bun elides function names expected by Lighthouse #4958

Open zsharp-gls opened 1 year ago

zsharp-gls commented 1 year ago

What version of Bun is running?

1.0.0+822a00c4d508b54f650933a73ca5f4a3af9a7983

What platform is your computer?

Linux 5.15.90.1-microsoft-standard-WSL2 x86_64 x86_64

What steps can reproduce the bug?

  1. Initialize a new bun project
  2. Install Google's Lighthouse package bun add lighthouse
  3. Create a basic index.ts script to run lighthouse
    
    import lighthouse from "lighthouse";

const result = await lighthouse("https://google.com", { disableStorageReset: true });

console.log(result);

4. `bun run index.ts`
5. lighthouse throws an error that it could not find the function name for `truncate`

### What is the expected behavior?

When running with node, `truncate.toString()` returns

'function truncate(string, characterLimit) { return Util.truncate(string, characterLimit); }'


### What do you see instead?

Lighthouse throws the error:

error: could not find function name for: function(string, characterLimit) { return Util.truncate(string, characterLimit); }


`truncate.toString()` returns

function(string, characterLimit) { return Util.truncate(string, characterLimit); }



### Additional information

The relevant code in lighthouse is [here](https://github.com/GoogleChrome/lighthouse/blob/1732f765738e2ee8053f98b102345b73d662e6b1/core/lib/page-functions.js#L573-L591). It looks like lighthouse is using some trickery to find the function name, expecting certain outputs from bundling.

I understand that there are a number of factors at play here with the different projects, but I wanted to raise it here since this issue is preventing bun from being a drop-in npm replacement for me right now.
ardelato commented 1 year ago

Seconded. I am also running into the same issue.


I attempted to manually modify the lighthouse line in the node_modules directory to the following and that seemed to allow bun to run lighthouse.

function getRuntimeFunctionName(fn) {
  if (!fn.name) throw new Error(`could not find function name for: ${fn}`);
  return fn.name;
}

This approach uses the name property of the function object, which should be more reliable than parsing the function's string representation.


However, I am wondering if there is a way to modify the bun runtime configuration or the tsconfig.json to work with lighthouse as is.

Thebarda commented 11 months ago

Is this a bug related to lighthouse or bun?

zsharp-gls commented 11 months ago

I believe one of bun's goals is to be a drop-in replacement for node.js. This is an instance where it isn't (because Lighthouse is doing something outside of the norm). So, I could see it being resolved from either end