From 7aa75f3d9f6ce066a4114474b7ed5fe62998d67f Mon Sep 17 00:00:00 2001 From: PJ Date: Sat, 18 Apr 2026 00:08:31 +0700 Subject: [PATCH] feat(bundler): add Aliases option for spec import resolution Specs import from "@uatu/spec"; the bundler maps that to the vendored pkg/spec-api/src/index.ts via esbuild's Alias map. --- internal/bundler/bundler.go | 2 ++ internal/bundler/bundler_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/internal/bundler/bundler.go b/internal/bundler/bundler.go index e0dc7ed..b26dca6 100644 --- a/internal/bundler/bundler.go +++ b/internal/bundler/bundler.go @@ -14,6 +14,7 @@ import ( type Options struct { EntryFile string Defines map[string]string + Aliases map[string]string Sourcemap bool } @@ -51,6 +52,7 @@ func Bundle(options Options) (Result, error) { Target: esbuild.ES2020, Platform: esbuild.PlatformNeutral, Define: defines, + Alias: options.Aliases, Sourcemap: sourcemap, Write: false, LogLevel: esbuild.LogLevelSilent, diff --git a/internal/bundler/bundler_test.go b/internal/bundler/bundler_test.go index 7443b0d..47cf396 100644 --- a/internal/bundler/bundler_test.go +++ b/internal/bundler/bundler_test.go @@ -164,3 +164,28 @@ func TestBundle_SourcemapInlinedWhenRequested(t *testing.T) { t.Errorf("expected inline sourcemap") } } + +func TestBundle_AliasResolvesPackageImport(t *testing.T) { + directory := t.TempDir() + aliasTarget := filepath.Join(directory, "aliased.ts") + entryPath := filepath.Join(directory, "entry.ts") + if err := os.WriteFile(aliasTarget, []byte(`export const aliasMagic = "alias-resolved";`), 0o600); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(entryPath, []byte(` + import { aliasMagic } from "@example/aliased"; + console.log(aliasMagic); + `), 0o600); err != nil { + t.Fatal(err) + } + result, err := Bundle(Options{ + EntryFile: entryPath, + Aliases: map[string]string{"@example/aliased": aliasTarget}, + }) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(string(result.JavaScript), "alias-resolved") { + t.Errorf("alias target should be inlined: %s", result.JavaScript) + } +}