feat(bundler): optional RuntimeFile prepends a runtime-entry import via stdin

This commit is contained in:
pj committed 2026-06-01 16:43:50 +05:30
1 parent bf89f3b1cf
commit 45366ddde5
1 file changed
+27 -3
+27 -3
View File
@@ -6,6 +6,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"path/filepath"
"strings" "strings"
esbuild "github.com/evanw/esbuild/pkg/api" esbuild "github.com/evanw/esbuild/pkg/api"
@@ -13,6 +14,11 @@ import (
type Options struct { type Options struct {
EntryFile string EntryFile string
// RuntimeFile, when set, is imported BEFORE the spec via a stdin entry so
// the bundle installs __sanderlingNextAction__ / __sanderlingExtractors__
// (the goja runtime entry wires the shared picker). Empty bundles the spec
// alone, as the bundle-check tool and unit fixtures do.
RuntimeFile string
Defines map[string]string Defines map[string]string
Aliases map[string]string Aliases map[string]string
Sourcemap bool Sourcemap bool
@@ -45,8 +51,7 @@ func Bundle(options Options) (Result, error) {
sourcemap = esbuild.SourceMapInline sourcemap = esbuild.SourceMapInline
} }
output := esbuild.Build(esbuild.BuildOptions{ buildOptions := esbuild.BuildOptions{
EntryPoints: []string{options.EntryFile},
Bundle: true, Bundle: true,
Format: esbuild.FormatIIFE, Format: esbuild.FormatIIFE,
Target: esbuild.ES2020, Target: esbuild.ES2020,
@@ -56,7 +61,26 @@ func Bundle(options Options) (Result, error) {
Sourcemap: sourcemap, Sourcemap: sourcemap,
Write: false, Write: false,
LogLevel: esbuild.LogLevelSilent, LogLevel: esbuild.LogLevelSilent,
}) }
if options.RuntimeFile == "" {
buildOptions.EntryPoints = []string{options.EntryFile}
} else {
runtimeAbs, err := filepath.Abs(options.RuntimeFile)
if err != nil {
return Result{}, fmt.Errorf("runtime path: %w", err)
}
specAbs, err := filepath.Abs(options.EntryFile)
if err != nil {
return Result{}, fmt.Errorf("entry path: %w", err)
}
buildOptions.Stdin = &esbuild.StdinOptions{
Contents: fmt.Sprintf("import %q;\nimport %q;\n", runtimeAbs, specAbs),
ResolveDir: filepath.Dir(specAbs),
Loader: esbuild.LoaderTS,
}
}
output := esbuild.Build(buildOptions)
if len(output.Errors) > 0 { if len(output.Errors) > 0 {
var messages []string var messages []string