-
Add a log message for ambiguous re-exports (#2322)
In JavaScript, you can re-export symbols from another file using
export * from './another-file'
. When you do this from multiple files that export different symbols with the same name, this creates an ambiguous export which is causes that name to not be exported. This is harmless if you don't plan on using the ambiguous export name, so esbuild doesn't have a warning for this. But if you do want a warning for this (or if you want to make it an error), you can now opt-in to seeing this log message with--log-override:ambiguous-reexport=warning
or--log-override:ambiguous-reexport=error
. The log message looks like this:▲ [WARNING] Re-export of "common" in "example.js" is ambiguous and has been removed [ambiguous-reexport] One definition of "common" comes from "a.js" here: a.js:2:11: 2 │ export let common = 2 ╵ ~~~~~~ Another definition of "common" comes from "b.js" here: b.js:3:14: 3 │ export { b as common } ╵ ~~~~~~
-
Optimize the output of the JSON loader (#2161)
The
json
loader (which is enabled by default for.json
files) parses the file as JSON and generates a JavaScript file with the parsed expression as thedefault
export. This behavior is standard and works in both node and the browser (well, as long as you use an import assertion). As an extension, esbuild also allows you to import additional top-level properties of the JSON object directly as a named export. This is beneficial for tree shaking. For example:import { version } from 'esbuild/package.json' console.log(version)
If you bundle the above code with esbuild, you'll get something like the following:
// node_modules/esbuild/package.json var version = "0.14.44"; // example.js console.log(version);
Most of the
package.json
file is irrelevant and has been omitted from the output due to tree shaking. The way esbuild implements this is to have the JavaScript file that's generated from the JSON look something like this with a separate exported variable for each property on the top-level object:// node_modules/esbuild/package.json export var name = "esbuild"; export var version = "0.14.44"; export var repository = "https://github.com/evanw/esbuild"; export var bin = { esbuild: "bin/esbuild" }; ... export default { name, version, repository, bin, ... };
However, this means that if you import the
default
export instead of a named export, you will get non-optimal output. Thedefault
export references all top-level properties, leading to many unnecessary variables in the output. With this release esbuild will now optimize this case to only generate additional variables for top-level object properties that are actually imported:// Original code import all, { bar } from 'data:application/json,{"foo":[1,2,3],"bar":[4,5,6]}' console.log(all, bar) // Old output (with --bundle --minify --format=esm) var a=[1,2,3],l=[4,5,6],r={foo:a,bar:l};console.log(r,l); // New output (with --bundle --minify --format=esm) var l=[4,5,6],r={foo:[1,2,3],bar:l};console.log(r,l);
Notice how there is no longer an unnecessary generated variable for
foo
since it's never imported. And if you only import thedefault
export, esbuild will now reproduce the original JSON object in the output with all top-level properties compactly inline. -
Add
id
to warnings returned from the APIWith this release, warnings returned from esbuild's API now have an
id
property. This identifies which kind of log message it is, which can be used to more easily filter out certain warnings. For example, reassigning aconst
variable will generate a message with anid
of"assign-to-constant"
. This also gives you the identifier you need to apply a log override for that kind of message: https://esbuild.github.io/api/#log-override.