Skip to content

Commit f43cd86

Browse files
committed
fix(build): use plugin for local package aliases with bundle: false
esbuild's built-in alias option requires bundle: true, but socket-lib uses bundle: false (library pattern - outputs individual files). Created a custom esbuild plugin that intercepts imports and resolves local package aliases using getLocalPackageAliases(). This preserves the critical local development workflow where all socket-* repos are checked out side-by-side and changes are picked up immediately, while maintaining compatibility with esbuild's requirements. The plugin: - Only activates when local packages are detected - Handles both direct imports (@socketsecurity/lib) and subpaths - Marks resolved paths as external (no bundling)
1 parent e6f84fe commit f43cd86

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

.config/esbuild.config.mjs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,38 @@ const entryPoints = fg.sync('**/*.{ts,mts,cts}', {
2222
ignore: ['**/*.d.ts', '**/types/**', '**/external/**'],
2323
})
2424

25+
/**
26+
* Plugin to handle local package aliases when bundle: false
27+
* esbuild's built-in alias only works with bundle: true, so we need a custom plugin
28+
*/
29+
function createAliasPlugin() {
30+
const aliases = getLocalPackageAliases(rootPath)
31+
32+
// Only create plugin if we have local aliases
33+
if (Object.keys(aliases).length === 0) {
34+
return null
35+
}
36+
37+
return {
38+
name: 'local-package-aliases',
39+
setup(build) {
40+
// Intercept imports for aliased packages
41+
for (const [packageName, aliasPath] of Object.entries(aliases)) {
42+
build.onResolve({ filter: new RegExp(`^${packageName}$`) }, () => {
43+
// Return the path to the local package dist
44+
return { path: aliasPath, external: true }
45+
})
46+
47+
// Handle subpath imports like '@socketsecurity/lib/spinner'
48+
build.onResolve({ filter: new RegExp(`^${packageName}/`) }, args => {
49+
const subpath = args.path.slice(packageName.length + 1)
50+
return { path: path.join(aliasPath, subpath), external: true }
51+
})
52+
}
53+
},
54+
}
55+
}
56+
2557
// Build configuration for CommonJS output
2658
export const buildConfig = {
2759
entryPoints,
@@ -40,8 +72,8 @@ export const buildConfig = {
4072
metafile: true,
4173
logLevel: 'info',
4274

43-
// Alias local packages when available (dev mode).
44-
alias: getLocalPackageAliases(rootPath),
75+
// Use plugin for local package aliases (built-in alias requires bundle: true)
76+
plugins: [createAliasPlugin()].filter(Boolean),
4577

4678
// Note: Cannot use "external" with bundle: false
4779
// esbuild automatically treats all imports as external when not bundling

0 commit comments

Comments
 (0)