Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 6 additions & 32 deletions packages/build-infra/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ Shared build infrastructure utilities for Socket CLI. Provides esbuild plugins,
│ │ Unicode │ │ API Client │ │ SHA256 │ │
│ │ Transform │ │ + Download │ │ Content │ │
│ │ │ │ │ │ Hashing │ │
─────────────── ├──────────────┤ └──────────┤ │
│ Dead Code │ Asset Cache │ │ Skip │ │
│ Elimination │ (1hr TTL) │ │ Regen │ │
└───────────────┘ └──────────────┘ └──────────┘ │
─────────────── ├──────────────┤ └──────────┤ │
│ Asset Cache │ │ Skip │ │
│ (1hr TTL) │ │ Regen │ │
└──────────────┘ └──────────┘ │
│ │
│ Helpers │
│ ┌───────────────────────────────────────────────────────┐ │
Expand Down Expand Up @@ -72,30 +72,6 @@ export default {
- Replaces unsupported patterns with `/(?:)/` (no-op)
- Removes `/u` and `/v` flags after transformation

#### `deadCodeEliminationPlugin()`

Removes unreachable code branches based on constant boolean conditions. Simplifies bundled output by eliminating dead paths.

```javascript
import { deadCodeEliminationPlugin } from 'build-infra/lib/esbuild-plugin-dead-code-elimination'

export default {
plugins: [deadCodeEliminationPlugin()],
}
```

**Transformations:**

- `if (false) { deadCode() }` → `` (removed)
- `if (true) { liveCode() } else { deadCode() }` → `liveCode()` (unwrapped)
- `if (false) { } else { liveCode() }` → `liveCode()` (unwrapped)

**Implementation:**

- Uses Babel parser + MagicString for safe AST transformations
- Only processes `.js` files in esbuild output
- Applies transformations in reverse order to maintain positions

### esbuild Helpers

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

```javascript
// .config/esbuild.config.mjs
// .config/esbuild.cli.build.mjs
import { IMPORT_META_URL_BANNER } from 'build-infra/lib/esbuild-helpers'
import { unicodeTransformPlugin } from 'build-infra/lib/esbuild-plugin-unicode-transform'
import { deadCodeEliminationPlugin } from 'build-infra/lib/esbuild-plugin-dead-code-elimination'

export default {
entryPoints: ['src/cli.mts'],
Expand All @@ -317,7 +292,7 @@ export default {
'import.meta.url': '__importMetaUrl',
},

plugins: [unicodeTransformPlugin(), deadCodeEliminationPlugin()],
plugins: [unicodeTransformPlugin()],
}
```

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

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

Expand Down
156 changes: 0 additions & 156 deletions packages/build-infra/lib/esbuild-plugin-dead-code-elimination.mjs

This file was deleted.

1 change: 0 additions & 1 deletion packages/build-infra/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"type": "module",
"exports": {
"./lib/esbuild-helpers": "./lib/esbuild-helpers.mjs",
"./lib/esbuild-plugin-dead-code-elimination": "./lib/esbuild-plugin-dead-code-elimination.mjs",
"./lib/esbuild-plugin-unicode-transform": "./lib/esbuild-plugin-unicode-transform.mjs",
"./lib/extraction-cache": "./lib/extraction-cache.mjs",
"./lib/github-error-utils": "./lib/github-error-utils.mjs",
Expand Down
87 changes: 1 addition & 86 deletions packages/cli/.config/esbuild.cli.build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* esbuild is much faster than Rollup and doesn't have template literal corruption issues.
*/

import { existsSync, readFileSync } from 'node:fs'
import { existsSync } from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

Expand Down Expand Up @@ -46,50 +46,6 @@ function findSocketLibPath(importerPath) {
return null
}

// CLI build must use published packages only - no local sibling directories.
// This ensures the CLI is properly isolated and doesn't depend on local dev setup.
const socketPackages = {}

// Resolve subpath from package.json exports.
function resolvePackageSubpath(packagePath, subpath) {
try {
const pkgJsonPath = path.join(packagePath, 'package.json')
const pkgJson = JSON.parse(readFileSync(pkgJsonPath, 'utf-8'))
const exports = pkgJson.exports || {}

// Try exact export match.
const exportKey = subpath === '.' ? '.' : `./${subpath}`
if (exports[exportKey]) {
const exportValue = exports[exportKey]
// Handle conditional exports.
if (typeof exportValue === 'object' && exportValue.default) {
return path.join(packagePath, exportValue.default)
}
// Handle simple string exports.
if (typeof exportValue === 'string') {
return path.join(packagePath, exportValue)
}
}

// Fallback: try conventional paths.
const distPath = path.join(packagePath, 'dist', subpath)
if (existsSync(`${distPath}.js`)) {
return `${distPath}.js`
}
if (existsSync(`${distPath}.mjs`)) {
return `${distPath}.mjs`
}
if (existsSync(path.join(distPath, 'index.js'))) {
return path.join(distPath, 'index.js')
}
if (existsSync(path.join(distPath, 'index.mjs'))) {
return path.join(distPath, 'index.mjs')
}
} catch {}

return null
}

const config = {
entryPoints: [path.join(rootPath, 'src/cli-dispatch.mts')],
bundle: true,
Expand Down Expand Up @@ -148,47 +104,6 @@ const config = {
// Environment variable replacement must run AFTER unicode transform.
envVarReplacementPlugin(inlinedEnvVars),
unicodeTransformPlugin(),
{
name: 'resolve-socket-packages',
setup(build) {
// Resolve local Socket packages with subpath exports.
for (const [packageName, packagePath] of Object.entries(
socketPackages,
)) {
// Handle package root imports.
build.onResolve(
{ filter: new RegExp(`^${packageName.replace('/', '\\/')}$`) },
() => {
if (!existsSync(packagePath)) {
return null
}
const resolved = resolvePackageSubpath(packagePath, '.')
if (resolved) {
return { path: resolved }
}
return null
},
)

// Handle subpath imports.
build.onResolve(
{ filter: new RegExp(`^${packageName.replace('/', '\\/')}\\/`) },
args => {
if (!existsSync(packagePath)) {
return null
}
const subpath = args.path.slice(packageName.length + 1)
const resolved = resolvePackageSubpath(packagePath, subpath)
if (resolved) {
return { path: resolved }
}
return null
},
)
}
},
},

{
name: 'resolve-socket-lib-internals',
setup(build) {
Expand Down
1 change: 0 additions & 1 deletion packages/cli/scripts/constants/env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export const INLINED_COANA_VERSION =
export const INLINED_CYCLONEDX_CDXGEN_VERSION =
'INLINED_CYCLONEDX_CDXGEN_VERSION'
export const INLINED_HOMEPAGE = 'INLINED_HOMEPAGE'
export const INLINED_LEGACY_BUILD = 'INLINED_LEGACY_BUILD'
export const INLINED_NAME = 'INLINED_NAME'
export const INLINED_PUBLISHED_BUILD =
'INLINED_PUBLISHED_BUILD'
Expand Down
Loading
Loading