Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: Allow input from memory for building #212

Closed
remko opened this issue Jul 1, 2020 · 7 comments
Closed

API: Allow input from memory for building #212

remko opened this issue Jul 1, 2020 · 7 comments

Comments

@remko
Copy link

remko commented Jul 1, 2020

The build CLI allows to take input from stdin and dump the output to stdout. This doesn't seem to be possible using the (Go) API?

Possibly related to #139

@evanw
Copy link
Owner

evanw commented Jul 1, 2020

The esbuild CLI is literally implemented using the public Go API, so if you want to do something that the CLI does you should be able to just reference that. Here's the part where it takes input from stdin and dumps the output to stdout:

// Read the input from stdin
bytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
  logging.PrintErrorToStderr(osArgs, fmt.Sprintf(
    "Could not read from stdin: %s", err.Error()))
  return 1
}

// Run the transform and stop if there were errors
result := api.Transform(string(bytes), *transformOptions)
if len(result.Errors) > 0 {
  return 1
}

// Write the output to stdout
os.Stdout.Write(result.JS)

Where the input comes from is intentionally not part of the API. There are many different potential sources of input (stdin, file, network, output of another command, etc.) and I don't think it makes sense to encode all of that in the API itself. It's trivial to read the input string from stdin first, as shown in the code example above.

@evanw evanw closed this as completed Jul 1, 2020
@remko
Copy link
Author

remko commented Jul 2, 2020

Seems I was wrong: you can’t pass a file for bundling (not transforming) via stdin via the CLI. I guess you’ll also need an option to specify the file name. Or am I missing something?

@evanw
Copy link
Owner

evanw commented Jul 2, 2020

Ah, I see. What's the use case? I'm asking because bundling usually involves multiple files, but you could only really pass one file via stdin.

I could see wanting an API where you could pass an array of multiple in-memory files to bundle. Perhaps that's what you meant by saying it's related to #139. The way I plan to address both of those requests is with the future plugin API.

@remko
Copy link
Author

remko commented Jul 2, 2020

I'm looking at using ESBuild as a Hugo pipe stage.
This particular case always takes 1 (in-memory) file as input.

@remko
Copy link
Author

remko commented Jul 7, 2020

@evanw Given that #139 is closed, but didn't solve this issue, can this one be reopened? Or is there another issue for tracking allowing input from memory?

@remko remko changed the title API: Allow input from stdin API: Allow input from memory for building Jul 7, 2020
@evanw
Copy link
Owner

evanw commented Jul 7, 2020

Let's reopen this one.

@CEbbinghaus
Copy link

Does this work with module resolution? Is it possible to provide all the files in memory and build them out into a single bundle?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants