diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a0ce71b59d..90a575451a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/pkg/api/api_impl.go b/pkg/api/api_impl.go index 89a4e9d612e..3bab967a8ce 100644 --- a/pkg/api/api_impl.go +++ b/pkg/api/api_impl.go @@ -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() diff --git a/pkg/cli/cli_impl.go b/pkg/cli/cli_impl.go index 48cdbf937e9..dfb3b2d02b7 100644 --- a/pkg/cli/cli_impl.go +++ b/pkg/cli/cli_impl.go @@ -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") @@ -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) @@ -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)