-
Fix
process.env.NODE_ENV
substitution when transforming (#2718)Version 0.16.0 introduced an unintentional regression that caused
process.env.NODE_ENV
to be automatically substituted with either"development"
or"production"
when using esbuild'stransform
API. This substitution is a necessary feature of esbuild'sbuild
API because the React framework crashes when you bundle it without doing this. But thetransform
API is typically used as part of a larger build pipeline so the benefit of esbuild doing this automatically is not as clear, and esbuild previously didn't do this.However, version 0.16.0 switched the default value of the
platform
setting for thetransform
API fromneutral
tobrowser
, both to align it with esbuild's documentation (which saysbrowser
is the default value) and because escaping the</script>
character sequence is now tied to thebrowser
platform (see the release notes for version 0.16.0 for details). That accidentally enabled automatic substitution ofprocess.env.NODE_ENV
because esbuild always did that for code meant for the browser. To fix this regression, esbuild will now only automatically substituteprocess.env.NODE_ENV
when using thebuild
API. -
Prevent
define
from substituting constants into assignment position (#2719)The
define
feature lets you replace certain expressions with constants. For example, you could use it to replace references to the global property referencewindow.DEBUG
withfalse
at compile time, which can then potentially help esbuild remove unused code from your bundle. It's similar to DefinePlugin in Webpack.However, if you write code such as
window.DEBUG = true
and then definedwindow.DEBUG
tofalse
, esbuild previously generated the outputfalse = true
which is a syntax error in JavaScript. This behavior is not typically a problem because it doesn't make sense to substitutewindow.DEBUG
with a constant if its value changes at run-time (Webpack'sDefinePlugin
also generatesfalse = true
in this case). But it can be alarming to have esbuild generate code with a syntax error.So with this release, esbuild will no longer substitute
define
constants into assignment position to avoid generating code with a syntax error. Instead esbuild will generate a warning, which currently looks like this:▲ [WARNING] Suspicious assignment to defined constant "window.DEBUG" [assign-to-define] example.js:1:0: 1 │ window.DEBUG = true ╵ ~~~~~~~~~~~~ The expression "window.DEBUG" has been configured to be replaced with a constant using the "define" feature. If this expression is supposed to be a compile-time constant, then it doesn't make sense to assign to it here. Or if this expression is supposed to change at run-time, this "define" substitution should be removed.
-
Fix a regression with
npm install --no-optional
(#2720)Normally when you install esbuild with
npm install
, npm itself is the tool that downloads the correct binary executable for the current platform. This happens because of how esbuild's primary package uses npm'soptionalDependencies
feature. However, if you deliberately disable this withnpm install --no-optional
then esbuild's install script will attempt to repair the installation by manually downloading and extracting the binary executable from the package that was supposed to be installed.The change in version 0.16.0 to move esbuild's nested packages into the
@esbuild/
scope unintentionally broke this logic because of how npm's URL structure is different for scoped packages vs. normal packages. It was actually already broken for a few platforms earlier because esbuild already had packages for some platforms in the@esbuild/
scope, but I didn't discover this then because esbuild's integration tests aren't run on all platforms. Anyway, this release contains some changes to the install script that should hopefully get this scenario working again.