Repository: evanw/esbuild
File: cmd/esbuild/main.go (Go)

Code before:
		}
	}

	// Write out the results
	for _, item := range result {
		// Special-case writing to stdout
		if args.bundleOptions.WriteToStdout {
			_, err := os.Stdout.Write(item.JsContents)
			if err != nil {
				exitWithError(fmt.Sprintf("Failed to write to stdout: %s", err.Error()))
			}
			continue
		}

		// Write out the JavaScript file
		err := ioutil.WriteFile(item.JsAbsPath, []byte(item.JsContents), 0644)
		path := resolver.PrettyPath(item.JsAbsPath)
		if err != nil {
			exitWithError(fmt.Sprintf("Failed to write to %s (%s)", path, err.Error()))
		}
		args.logInfo(fmt.Sprintf("Wrote to %s (%s)", path, toSize(len(item.JsContents))))

		// Also write the source map
		if item.SourceMapAbsPath != "" {
			err := ioutil.WriteFile(item.SourceMapAbsPath, item.SourceMapContents, 0644)
			path := resolver.PrettyPath(item.SourceMapAbsPath)
			if err != nil {
				exitWithError(fmt.Sprintf("Failed to write to %s: (%s)", path, err.Error()))
			}
			args.logInfo(fmt.Sprintf("Wrote to %s (%s)", path, toSize(len(item.SourceMapContents))))
		}
	}
}

func toSize(bytes int) string {
	if bytes < 1024 {
		return fmt.Sprintf("%d bytes", bytes)
	}

	if bytes < 1024*1024 {
		return fmt.Sprintf("%.1fkb", float32(bytes)/float32(1024))
	}

	return fmt.Sprintf("%.1fmb", float32(bytes)/float32(1024*1024))
}


Review comment (refactor):
The contents of `AdditionalFiles` end up being written out twice because they are both written out here and duplicated to become normal files in this code elsewhere:

```go
		for _, bundle := range group {
			for _, additionalFile := range bundle.AdditionalFiles {
				if additionalFile.Path != "" {
					results = append(results, BundleResult{additionalFile.Path, []byte(additionalFile.Contents), "", []byte(""), []AdditionalFile{}})
				}
			}
		}
```

That's ok, I can fix it after landing this.

Code after:
		if err := os.MkdirAll(args.bundleOptions.AbsOutputDir, 0755); err != nil {
			exitWithError(fmt.Sprintf("Cannot create output directory: %s", err))
		}
	}

	// Write out the results
	for _, item := range result {
		// Special-case writing to stdout
		if args.bundleOptions.WriteToStdout {
			_, err := os.Stdout.Write(item.JsContents)
			if err != nil {
				exitWithError(fmt.Sprintf("Failed to write to stdout: %s", err.Error()))
			}
			continue
		}

		// Write out the JavaScript file
		err := ioutil.WriteFile(item.JsAbsPath, []byte(item.JsContents), 0644)
		path := resolver.PrettyPath(item.JsAbsPath)
		if err != nil {
			exitWithError(fmt.Sprintf("Failed to write to %s (%s)", path, err.Error()))
		}
		args.logInfo(fmt.Sprintf("Wrote to %s (%s)", path, toSize(len(item.JsContents))))

		// Write out the additional files
		for _, file := range item.AdditionalFiles {
			if file.Path != "" {
				err := ioutil.WriteFile(file.Path, []byte(file.Contents), 0644)
				path := resolver.PrettyPath(file.Path)
				if err != nil {
					exitWithError(fmt.Sprintf("Failed to write to %s (%s)", path, err.Error()))
				}
				args.logInfo(fmt.Sprintf("Wrote to %s (%s)", path, toSize(len(file.Contents))))
			}
		}
		// Also write the source map
		if item.SourceMapAbsPath != "" {
			err := ioutil.WriteFile(item.SourceMapAbsPath, item.SourceMapContents, 0644)
			path := resolver.PrettyPath(item.SourceMapAbsPath)
			if err != nil {
				exitWithError(fmt.Sprintf("Failed to write to %s: (%s)", path, err.Error()))
			}
			args.logInfo(fmt.Sprintf("Wrote to %s (%s)", path, toSize(len(item.SourceMapContents))))
		}
	}
}

func toSize(bytes int) string {
	if bytes < 1024 {
		return fmt.Sprintf("%d bytes", bytes)
	}