Skip to content

Commit

Permalink
fix #1357: "--metafile" with "--watch"
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jun 8, 2021
1 parent d3ef487 commit 6dc45ba
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -29,6 +29,10 @@

With this release, esbuild will no longer respect `tsconfig.json` files when the source file is inside a `node_modules` folder. Note that `tsconfig.json` files inside `node_modules` are still parsed, and extending `tsconfig.json` files from inside a package is still supported.

* Fix missing `--metafile` when using `--watch` ([#1357](https://github.com/evanw/esbuild/issues/1357))

Due to an oversight, the `--metafile` setting didn't work when `--watch` was also specified. This only affected the command-line interface. With this release, the `--metafile` setting should now work in this case.

## 0.12.6

* Improve template literal lowering transformation conformance ([#1327](https://github.com/evanw/esbuild/issues/1327))
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/api_impl.go
Expand Up @@ -960,10 +960,11 @@ func rebuildImpl(
if !log.HasErrors() {
// Compile the bundle
results, metafile := bundle.Compile(log, options, timer)
metafileJSON = metafile

// Stop now if there were errors
if !log.HasErrors() {
metafileJSON = metafile

// Flush any deferred warnings now
log.AlmostDone()

Expand Down
58 changes: 37 additions & 21 deletions pkg/cli/cli_impl.go
Expand Up @@ -692,9 +692,11 @@ func runImpl(osArgs []string) int {
// Validate the metafile absolute path and directory ahead of time so we
// don't write any output files if it's incorrect. That makes this API
// option consistent with how we handle all other API options.
var metafileAbsPath string
var metafileAbsDir string
var writeMetafile func(string)
if metafile != nil {
var metafileAbsPath string
var metafileAbsDir string

if buildOptions.Outfile == "" && buildOptions.Outdir == "" {
// Cannot use "metafile" when writing to stdout
logger.PrintErrorToStderr(osArgs, "Cannot use \"metafile\" without an output path")
Expand All @@ -711,11 +713,44 @@ func runImpl(osArgs []string) int {
} else {
// Don't fail in this case since the error will be reported by "api.Build"
}

writeMetafile = func(json string) {
if json == "" {
return // Don't write out the metafile on build errors
}
if err != nil {
// This should already have been checked above
panic(err.Error())
}
fs.BeforeFileOpen()
defer fs.AfterFileClose()
if err := os.MkdirAll(metafileAbsDir, 0755); err != nil {
logger.PrintErrorToStderr(osArgs, fmt.Sprintf(
"Failed to create output directory: %s", err.Error()))
} else {
if err := ioutil.WriteFile(metafileAbsPath, []byte(json), 0644); err != nil {
logger.PrintErrorToStderr(osArgs, fmt.Sprintf(
"Failed to write to output file: %s", err.Error()))
}
}
}

// Write out the metafile whenever we rebuild
if buildOptions.Watch != nil {
buildOptions.Watch.OnRebuild = func(result api.BuildResult) {
writeMetafile(result.Metafile)
}
}
}

// Run the build
result := api.Build(*buildOptions)

// Write the metafile to the file system
if writeMetafile != nil {
writeMetafile(result.Metafile)
}

// Do not exit if we're in watch mode
if buildOptions.Watch != nil {
<-make(chan bool)
Expand All @@ -726,25 +761,6 @@ func runImpl(osArgs []string) int {
return 1
}

// Write the metafile to the file system
if metafile != nil {
if err != nil {
// This should already have been checked above
panic(err.Error())
}
fs.BeforeFileOpen()
defer fs.AfterFileClose()
if err := os.MkdirAll(metafileAbsDir, 0755); err != nil {
logger.PrintErrorToStderr(osArgs, fmt.Sprintf(
"Failed to create output directory: %s", err.Error()))
} else {
if err := ioutil.WriteFile(metafileAbsPath, []byte(result.Metafile), 0644); err != nil {
logger.PrintErrorToStderr(osArgs, fmt.Sprintf(
"Failed to write to output file: %s", err.Error()))
}
}
}

case transformOptions != nil:
// Read the input from stdin
bytes, err := ioutil.ReadAll(os.Stdin)
Expand Down

0 comments on commit 6dc45ba

Please sign in to comment.