Skip to content

Commit 90ad05f

Browse files
committed
Harden behavior around conflicting browserVars and esbuild settings
1 parent b15def1 commit 90ad05f

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,9 @@ export const browser = {
952952

953953
The exported object is passed to esbuild's [`define`](https://esbuild.github.io/api/#define) options and is available to every js bundle.
954954

955+
> [!WARNING]
956+
> Setting `define` in [`esbuild.settings.ts`](#esbuild-settingsts) while also using the `browser` export will throw an error. Use one or the other.
957+
955958
### `global.client.ts`
956959

957960
This is a script bundle that is included on every page. It provides an easy way to inject analytics, or other small scripts that every page should have. Try to minimize what you put in here.
@@ -1044,6 +1047,7 @@ Important esbuild settings you may want to set here are:
10441047
- [target](https://esbuild.github.io/api/#target) - Set the `target` to make `esbuild` run a few small transforms on your CSS and JS code.
10451048
- [jsx](https://esbuild.github.io/api/#jsx) - Unset this if you want default react transform.
10461049
- [jsxImportSource](https://esbuild.github.io/api/#jsx-import-source) - Unset this if you want default react transform.
1050+
- [define](https://esbuild.github.io/api/#define) - Define compile-time constants for js bundles. Note: setting `define` here conflicts with the [`browser` export](#browser-variable) in `global.vars.ts` and will throw an error if both are set.
10471051

10481052
### `markdown-it.settings.ts`
10491053

lib/build-esbuild/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ export async function buildEsbuild (src, dest, siteData, opts) {
114114

115115
const extendedBuildOpts = await esbuildSettingsExtends(buildOpts)
116116

117+
if (browserVars && Object.keys(browserVars).length > 0 && extendedBuildOpts.define !== buildOpts.define) {
118+
throw new Error(
119+
'Conflict: both the "browser" export in global.vars and "define" in esbuild.settings are set. ' +
120+
'Use one or the other to define browser constants.'
121+
)
122+
}
123+
117124
try {
118125
// @ts-ignore This actually works fine
119126
const buildResults = await esbuild.build(extendedBuildOpts)

0 commit comments

Comments
 (0)