Skip to content

[Feature] buildSync returns outputFiles[].content as Uint8Array, expected String #496

Closed
@Jarred-Sumner

Description

If you run buildSync, it returns the outputFiles[].contents as Uint8Array. This is a little unexpected for source code where you don't intend to write to disk, but it makes a lot of sense if you're about to upload the file to a CDN

It would be slightly easier if either:

  • .toString() function defined on the prototype of the outputFiles[]. object.
  • Default to returning a String based on the file type (code/plaintext? string. images/other? Uint8Array)

A .toString() function or .toCode() would be cool, since changing the return type might cause weird bugs in tools and decoding isn't free. And, if it was something like.toCode(), it would be natural for that to maybe be pretty printed later on

Example code:

const result = require("esbuild").buildSync({
    stdin: {
      contents: "module.exports = console.log("HI!!")",
      resolveDir: path.resolve("."),
      sourcefile: "file.ts",
      loader: "ts",
    },
    format: "cjs",
    target: [`node12`],
    sourcemap: "inline",
    bundle: true,
    write: false,
  })

> result.outputFiles[0].contents.constructor.name
'Uint8Array'

If anyone searches how to get the outputFiles contents as a string, here's the code:

new TextDecoder("utf-8").decode(result.outputFiles[0].contents)

Sidenote: esbuild is really cool. I'm using it in a webpack loader for a build tool. I first tried implementing this webpack loader as a webpack plugin via createChildCompiler and spent much of the day trying to get it to work. It took about 15 minutes with esbuild and now I wish I just tried esbuild to start with.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions