Skip to content

Commit 1888a36

Browse files
committed
refactor(build): use plugin pattern for local package aliases
Replaced built-in alias option with custom esbuild plugin to provide consistent alias resolution across all Socket repos. While the built-in alias works fine with bundle: true, the plugin approach: - Provides identical implementation across socket-lib, socket-sdk-js, and socket-packageurl-js - Uses unified regexp pattern (/|$) to handle both exact and subpath imports - Maintains same functionality while improving cross-repo consistency This standardization simplifies maintenance and documentation across the Socket monorepo ecosystem.
1 parent 15ca9a9 commit 1888a36

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

.config/esbuild.config.mjs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,35 @@ const rootPath = path.join(__dirname, '..')
1313
const srcPath = path.join(rootPath, 'src')
1414
const distPath = path.join(rootPath, 'dist')
1515

16+
/**
17+
* Plugin to handle local package aliases.
18+
* Provides consistent alias resolution across all Socket repos.
19+
*/
20+
function createAliasPlugin() {
21+
const aliases = getLocalPackageAliases(rootPath)
22+
23+
// Only create plugin if we have local aliases
24+
if (Object.keys(aliases).length === 0) {
25+
return null
26+
}
27+
28+
return {
29+
name: 'local-package-aliases',
30+
setup(build) {
31+
// Intercept imports for aliased packages
32+
for (const [packageName, aliasPath] of Object.entries(aliases)) {
33+
// Match both exact package name and subpath imports
34+
build.onResolve({ filter: new RegExp(`^${packageName}(/|$)`) }, args => {
35+
// Handle subpath imports like '@socketsecurity/lib/spinner'
36+
const subpath = args.path.slice(packageName.length + 1)
37+
const resolvedPath = subpath ? path.join(aliasPath, subpath) : aliasPath
38+
return { path: resolvedPath, external: true }
39+
})
40+
}
41+
},
42+
}
43+
}
44+
1645
// Build configuration for CommonJS output
1746
export const buildConfig = {
1847
entryPoints: [`${srcPath}/index.ts`],
@@ -32,8 +61,8 @@ export const buildConfig = {
3261
// Preserve module structure for better tree-shaking
3362
splitting: false,
3463

35-
// Alias local packages when available (dev mode).
36-
alias: getLocalPackageAliases(rootPath),
64+
// Use plugin for local package aliases (consistent across all Socket repos)
65+
plugins: [createAliasPlugin()].filter(Boolean),
3766

3867
// External dependencies
3968
external: [

0 commit comments

Comments
 (0)