Skip to content

Commit bb98d38

Browse files
authored
chore(build): remove dead code (#1149)
- Delete esbuild-plugin-dead-code-elimination.mjs (unused plugin) - Delete packages/cli/scripts/esbuild.config.mjs (broken orphan referencing non-existent esbuild.cli.config.mjs) - Remove dead-code-elimination export from build-infra/package.json - Remove socketPackages / resolvePackageSubpath / resolve-socket-packages plugin from esbuild.cli.build.mjs (socketPackages is always empty {}) - Remove INLINED_LEGACY_BUILD constant (unused) - Update build-infra README to remove dead code elimination docs
1 parent 11b65fa commit bb98d38

File tree

6 files changed

+7
-333
lines changed

6 files changed

+7
-333
lines changed

packages/build-infra/README.md

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ Shared build infrastructure utilities for Socket CLI. Provides esbuild plugins,
1414
│ │ Unicode │ │ API Client │ │ SHA256 │ │
1515
│ │ Transform │ │ + Download │ │ Content │ │
1616
│ │ │ │ │ │ Hashing │ │
17-
─────────────── ├──────────────┤ └──────────┤ │
18-
│ Dead Code │ Asset Cache │ │ Skip │ │
19-
│ Elimination │ (1hr TTL) │ │ Regen │ │
20-
└───────────────┘ └──────────────┘ └──────────┘ │
17+
─────────────── ├──────────────┤ └──────────┤ │
18+
│ Asset Cache │ │ Skip │ │
19+
│ (1hr TTL) │ │ Regen │ │
20+
└──────────────┘ └──────────┘ │
2121
│ │
2222
│ Helpers │
2323
│ ┌───────────────────────────────────────────────────────┐ │
@@ -72,30 +72,6 @@ export default {
7272
- Replaces unsupported patterns with `/(?:)/` (no-op)
7373
- Removes `/u` and `/v` flags after transformation
7474

75-
#### `deadCodeEliminationPlugin()`
76-
77-
Removes unreachable code branches based on constant boolean conditions. Simplifies bundled output by eliminating dead paths.
78-
79-
```javascript
80-
import { deadCodeEliminationPlugin } from 'build-infra/lib/esbuild-plugin-dead-code-elimination'
81-
82-
export default {
83-
plugins: [deadCodeEliminationPlugin()],
84-
}
85-
```
86-
87-
**Transformations:**
88-
89-
- `if (false) { deadCode() }` → `` (removed)
90-
- `if (true) { liveCode() } else { deadCode() }``liveCode()` (unwrapped)
91-
- `if (false) { } else { liveCode() }``liveCode()` (unwrapped)
92-
93-
**Implementation:**
94-
95-
- Uses Babel parser + MagicString for safe AST transformations
96-
- Only processes `.js` files in esbuild output
97-
- Applies transformations in reverse order to maintain positions
98-
9975
### esbuild Helpers
10076

10177
#### `IMPORT_META_URL_BANNER`
@@ -296,10 +272,9 @@ ensureOutputDir('/path/to/output/file.js')
296272
### esbuild Configuration
297273

298274
```javascript
299-
// .config/esbuild.config.mjs
275+
// .config/esbuild.cli.build.mjs
300276
import { IMPORT_META_URL_BANNER } from 'build-infra/lib/esbuild-helpers'
301277
import { unicodeTransformPlugin } from 'build-infra/lib/esbuild-plugin-unicode-transform'
302-
import { deadCodeEliminationPlugin } from 'build-infra/lib/esbuild-plugin-dead-code-elimination'
303278

304279
export default {
305280
entryPoints: ['src/cli.mts'],
@@ -317,7 +292,7 @@ export default {
317292
'import.meta.url': '__importMetaUrl',
318293
},
319294

320-
plugins: [unicodeTransformPlugin(), deadCodeEliminationPlugin()],
295+
plugins: [unicodeTransformPlugin()],
321296
}
322297
```
323298

@@ -439,7 +414,6 @@ Assets are cached per tag to avoid re-downloading across builds.
439414
**Consumers:**
440415

441416
- `packages/cli/.config/esbuild.cli.build.mjs` - Main CLI bundle config
442-
- `packages/cli/.config/esbuild.inject.config.mjs` - Shadow npm inject config
443417
- `packages/cli/scripts/download-assets.mjs` - Unified asset downloader
444418
- `packages/cli/scripts/sea-build-utils/builder.mjs` - SEA binary builder
445419

packages/build-infra/lib/esbuild-plugin-dead-code-elimination.mjs

Lines changed: 0 additions & 156 deletions
This file was deleted.

packages/build-infra/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"type": "module",
77
"exports": {
88
"./lib/esbuild-helpers": "./lib/esbuild-helpers.mjs",
9-
"./lib/esbuild-plugin-dead-code-elimination": "./lib/esbuild-plugin-dead-code-elimination.mjs",
109
"./lib/esbuild-plugin-unicode-transform": "./lib/esbuild-plugin-unicode-transform.mjs",
1110
"./lib/extraction-cache": "./lib/extraction-cache.mjs",
1211
"./lib/github-error-utils": "./lib/github-error-utils.mjs",

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

Lines changed: 1 addition & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* esbuild is much faster than Rollup and doesn't have template literal corruption issues.
55
*/
66

7-
import { existsSync, readFileSync } from 'node:fs'
7+
import { existsSync } from 'node:fs'
88
import path from 'node:path'
99
import { fileURLToPath } from 'node:url'
1010

@@ -46,50 +46,6 @@ function findSocketLibPath(importerPath) {
4646
return null
4747
}
4848

49-
// CLI build must use published packages only - no local sibling directories.
50-
// This ensures the CLI is properly isolated and doesn't depend on local dev setup.
51-
const socketPackages = {}
52-
53-
// Resolve subpath from package.json exports.
54-
function resolvePackageSubpath(packagePath, subpath) {
55-
try {
56-
const pkgJsonPath = path.join(packagePath, 'package.json')
57-
const pkgJson = JSON.parse(readFileSync(pkgJsonPath, 'utf-8'))
58-
const exports = pkgJson.exports || {}
59-
60-
// Try exact export match.
61-
const exportKey = subpath === '.' ? '.' : `./${subpath}`
62-
if (exports[exportKey]) {
63-
const exportValue = exports[exportKey]
64-
// Handle conditional exports.
65-
if (typeof exportValue === 'object' && exportValue.default) {
66-
return path.join(packagePath, exportValue.default)
67-
}
68-
// Handle simple string exports.
69-
if (typeof exportValue === 'string') {
70-
return path.join(packagePath, exportValue)
71-
}
72-
}
73-
74-
// Fallback: try conventional paths.
75-
const distPath = path.join(packagePath, 'dist', subpath)
76-
if (existsSync(`${distPath}.js`)) {
77-
return `${distPath}.js`
78-
}
79-
if (existsSync(`${distPath}.mjs`)) {
80-
return `${distPath}.mjs`
81-
}
82-
if (existsSync(path.join(distPath, 'index.js'))) {
83-
return path.join(distPath, 'index.js')
84-
}
85-
if (existsSync(path.join(distPath, 'index.mjs'))) {
86-
return path.join(distPath, 'index.mjs')
87-
}
88-
} catch {}
89-
90-
return null
91-
}
92-
9349
const config = {
9450
entryPoints: [path.join(rootPath, 'src/cli-dispatch.mts')],
9551
bundle: true,
@@ -148,47 +104,6 @@ const config = {
148104
// Environment variable replacement must run AFTER unicode transform.
149105
envVarReplacementPlugin(inlinedEnvVars),
150106
unicodeTransformPlugin(),
151-
{
152-
name: 'resolve-socket-packages',
153-
setup(build) {
154-
// Resolve local Socket packages with subpath exports.
155-
for (const [packageName, packagePath] of Object.entries(
156-
socketPackages,
157-
)) {
158-
// Handle package root imports.
159-
build.onResolve(
160-
{ filter: new RegExp(`^${packageName.replace('/', '\\/')}$`) },
161-
() => {
162-
if (!existsSync(packagePath)) {
163-
return null
164-
}
165-
const resolved = resolvePackageSubpath(packagePath, '.')
166-
if (resolved) {
167-
return { path: resolved }
168-
}
169-
return null
170-
},
171-
)
172-
173-
// Handle subpath imports.
174-
build.onResolve(
175-
{ filter: new RegExp(`^${packageName.replace('/', '\\/')}\\/`) },
176-
args => {
177-
if (!existsSync(packagePath)) {
178-
return null
179-
}
180-
const subpath = args.path.slice(packageName.length + 1)
181-
const resolved = resolvePackageSubpath(packagePath, subpath)
182-
if (resolved) {
183-
return { path: resolved }
184-
}
185-
return null
186-
},
187-
)
188-
}
189-
},
190-
},
191-
192107
{
193108
name: 'resolve-socket-lib-internals',
194109
setup(build) {

packages/cli/scripts/constants/env.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export const INLINED_COANA_VERSION =
66
export const INLINED_CYCLONEDX_CDXGEN_VERSION =
77
'INLINED_CYCLONEDX_CDXGEN_VERSION'
88
export const INLINED_HOMEPAGE = 'INLINED_HOMEPAGE'
9-
export const INLINED_LEGACY_BUILD = 'INLINED_LEGACY_BUILD'
109
export const INLINED_NAME = 'INLINED_NAME'
1110
export const INLINED_PUBLISHED_BUILD =
1211
'INLINED_PUBLISHED_BUILD'

0 commit comments

Comments
 (0)