esbuild 0.16.2
-
Fix
process.env.NODE_ENVsubstitution when transforming (#2718)Version 0.16.0 introduced an unintentional regression that caused
process.env.NODE_ENVto be automatically substituted with either"development"or"production"when using esbuild'stransformAPI. This substitution is a necessary feature of esbuild'sbuildAPI because the React framework crashes when you bundle it without doing this. But thetransformAPI 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
platformsetting for thetransformAPI fromneutraltobrowser, both to align it with esbuild's documentation (which saysbrowseris the default value) and because escaping the</script>character sequence is now tied to thebrowserplatform (see the release notes for version 0.16.0 for details). That accidentally enabled automatic substitution ofprocess.env.NODE_ENVbecause esbuild always did that for code meant for the browser. To fix this regression, esbuild will now only automatically substituteprocess.env.NODE_ENVwhen using thebuildAPI. -
Prevent
definefrom substituting constants into assignment position (#2719)The
definefeature lets you replace certain expressions with constants. For example, you could use it to replace references to the global property referencewindow.DEBUGwithfalseat 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 = trueand then definedwindow.DEBUGtofalse, esbuild previously generated the outputfalse = truewhich is a syntax error in JavaScript. This behavior is not typically a problem because it doesn't make sense to substitutewindow.DEBUGwith a constant if its value changes at run-time (Webpack'sDefinePluginalso generatesfalse = truein 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
defineconstants 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'soptionalDependenciesfeature. However, if you deliberately disable this withnpm install --no-optionalthen 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.