Skip to content

Commit f591811

Browse files
committed
fix(cli): inline build-time constants with post-bundle replacement plugin
esbuild's define plugin runs before bundling and can't replace mangled identifiers like import_node_process21.default.env["KEY"]. Created a post-bundle replacement plugin that handles all forms of env access. Changes: - Add envVarReplacementPlugin() to replace env vars after bundling - Add createDefineEntries() helper to DRY the env var definitions - Support both dot and bracket notation (process.env.KEY and process.env["KEY"]) - Replace with regex pattern that matches mangled identifiers This fixes the "CLI: vundefined" issue - version now shows correctly.
1 parent 4ce5ba7 commit f591811

File tree

1 file changed

+84
-29
lines changed

1 file changed

+84
-29
lines changed

packages/cli/.config/esbuild.cli.build.mjs

Lines changed: 84 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,58 @@ const versionHash = `${packageJson.version}:${gitHash}:${randUuidSegment}${
4949
publishedBuild ? '' : ':dev'
5050
}`
5151

52+
// Helper to create both dot and bracket notation define keys.
53+
function createDefineEntries(envVars) {
54+
const entries = {}
55+
for (const [key, value] of Object.entries(envVars)) {
56+
// Dot notation: process.env.KEY
57+
entries[`process.env.${key}`] = value
58+
// Bracket notation: process.env["KEY"]
59+
entries[`process.env["${key}"]`] = value
60+
}
61+
return entries
62+
}
63+
64+
// esbuild plugin to replace env vars after bundling (handles mangled identifiers).
65+
function envVarReplacementPlugin(envVars) {
66+
return {
67+
name: 'env-var-replacement',
68+
setup(build) {
69+
build.onEnd((result) => {
70+
const outputs = result.outputFiles
71+
if (!outputs || outputs.length === 0) {
72+
return
73+
}
74+
75+
for (const output of outputs) {
76+
let content = output.text
77+
78+
// Replace all forms of process.env["KEY"] access, even with mangled identifiers.
79+
// Pattern: <anything>.env["KEY"] where <anything> could be "import_node_process21.default" etc.
80+
for (const [key, value] of Object.entries(envVars)) {
81+
// Match: <identifier>.env["KEY"] or <identifier>.env['KEY']
82+
const pattern = new RegExp(
83+
`(\\w+\\.)+env\\["${key}"\\]`,
84+
'g'
85+
)
86+
const singleQuotePattern = new RegExp(
87+
`(\\w+\\.)+env\\['${key}'\\]`,
88+
'g'
89+
)
90+
91+
// Replace with the actual value (already JSON.stringified).
92+
content = content.replace(pattern, value)
93+
content = content.replace(singleQuotePattern, value)
94+
}
95+
96+
// Update the output content.
97+
output.contents = Buffer.from(content, 'utf8')
98+
}
99+
})
100+
},
101+
}
102+
}
103+
52104
// Get local Socket package paths using canonical helper.
53105
// rootPath is packages/cli, so go up to socket-cli root for getLocalPackageAliases.
54106
const socketCliRoot = path.join(rootPath, '..', '..')
@@ -143,42 +195,45 @@ const config = {
143195
define: {
144196
'process.env.NODE_ENV': '"production"',
145197
'import.meta.url': '__importMetaUrl',
146-
// Inject build metadata (replaces Rollup replace plugin).
147-
'process.env.INLINED_SOCKET_CLI_VERSION': JSON.stringify(
148-
packageJson.version,
149-
),
150-
'process.env.INLINED_SOCKET_CLI_VERSION_HASH': JSON.stringify(versionHash),
151-
'process.env.INLINED_SOCKET_CLI_NAME': JSON.stringify(packageJson.name),
152-
'process.env.INLINED_SOCKET_CLI_HOMEPAGE': JSON.stringify(
153-
packageJson.homepage,
154-
),
155-
'process.env.INLINED_SOCKET_CLI_AI_VERSION': JSON.stringify(
156-
packageJson.version,
157-
),
158-
'process.env.INLINED_SOCKET_CLI_COANA_VERSION':
159-
JSON.stringify(coanaVersion),
160-
'process.env.INLINED_SOCKET_CLI_CYCLONEDX_CDXGEN_VERSION':
161-
JSON.stringify(cdxgenVersion),
162-
'process.env.INLINED_SOCKET_CLI_SYNP_VERSION': JSON.stringify(synpVersion),
163-
'process.env.INLINED_SOCKET_CLI_PUBLISHED_BUILD': JSON.stringify(
164-
publishedBuild ? '1' : '',
165-
),
166-
'process.env.INLINED_SOCKET_CLI_LEGACY_BUILD': JSON.stringify(
167-
legacyBuild ? '1' : '',
168-
),
169-
'process.env.INLINED_SOCKET_CLI_SENTRY_BUILD': JSON.stringify(
170-
sentryBuild ? '1' : '',
171-
),
172-
// Python version/tag are optional and typically empty for standard builds.
173-
'process.env.INLINED_SOCKET_CLI_PYTHON_VERSION': JSON.stringify(''),
174-
'process.env.INLINED_SOCKET_CLI_PYTHON_BUILD_TAG': JSON.stringify(''),
198+
// Inject build metadata using DRY helper.
199+
...createDefineEntries({
200+
INLINED_SOCKET_CLI_VERSION: JSON.stringify(packageJson.version),
201+
INLINED_SOCKET_CLI_VERSION_HASH: JSON.stringify(versionHash),
202+
INLINED_SOCKET_CLI_NAME: JSON.stringify(packageJson.name),
203+
INLINED_SOCKET_CLI_HOMEPAGE: JSON.stringify(packageJson.homepage),
204+
INLINED_SOCKET_CLI_AI_VERSION: JSON.stringify(packageJson.version),
205+
INLINED_SOCKET_CLI_COANA_VERSION: JSON.stringify(coanaVersion),
206+
INLINED_SOCKET_CLI_CYCLONEDX_CDXGEN_VERSION: JSON.stringify(cdxgenVersion),
207+
INLINED_SOCKET_CLI_SYNP_VERSION: JSON.stringify(synpVersion),
208+
INLINED_SOCKET_CLI_PUBLISHED_BUILD: JSON.stringify(publishedBuild ? '1' : ''),
209+
INLINED_SOCKET_CLI_LEGACY_BUILD: JSON.stringify(legacyBuild ? '1' : ''),
210+
INLINED_SOCKET_CLI_SENTRY_BUILD: JSON.stringify(sentryBuild ? '1' : ''),
211+
INLINED_SOCKET_CLI_PYTHON_VERSION: JSON.stringify(''),
212+
INLINED_SOCKET_CLI_PYTHON_BUILD_TAG: JSON.stringify(''),
213+
}),
175214
},
176215

177216
// Inject import.meta.url polyfill for CJS.
178217
inject: [path.join(__dirname, 'esbuild-inject-import-meta.mjs')],
179218

180219
// Handle special cases with plugins.
181220
plugins: [
221+
// Environment variable replacement must run AFTER unicode transform.
222+
envVarReplacementPlugin({
223+
INLINED_SOCKET_CLI_VERSION: JSON.stringify(packageJson.version),
224+
INLINED_SOCKET_CLI_VERSION_HASH: JSON.stringify(versionHash),
225+
INLINED_SOCKET_CLI_NAME: JSON.stringify(packageJson.name),
226+
INLINED_SOCKET_CLI_HOMEPAGE: JSON.stringify(packageJson.homepage),
227+
INLINED_SOCKET_CLI_AI_VERSION: JSON.stringify(packageJson.version),
228+
INLINED_SOCKET_CLI_COANA_VERSION: JSON.stringify(coanaVersion),
229+
INLINED_SOCKET_CLI_CYCLONEDX_CDXGEN_VERSION: JSON.stringify(cdxgenVersion),
230+
INLINED_SOCKET_CLI_SYNP_VERSION: JSON.stringify(synpVersion),
231+
INLINED_SOCKET_CLI_PUBLISHED_BUILD: JSON.stringify(publishedBuild ? '1' : ''),
232+
INLINED_SOCKET_CLI_LEGACY_BUILD: JSON.stringify(legacyBuild ? '1' : ''),
233+
INLINED_SOCKET_CLI_SENTRY_BUILD: JSON.stringify(sentryBuild ? '1' : ''),
234+
INLINED_SOCKET_CLI_PYTHON_VERSION: JSON.stringify(''),
235+
INLINED_SOCKET_CLI_PYTHON_BUILD_TAG: JSON.stringify(''),
236+
}),
182237
unicodeTransformPlugin(),
183238
{
184239
name: 'resolve-socket-packages',

0 commit comments

Comments
 (0)