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"
"errors"
"fmt"
"path/filepath"
"strings"
esbuild "github.com/evanw/esbuild/pkg/api"
@@ -13,6 +14,11 @@ import (
type Options struct {
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
Aliases map[string]string
Sourcemap bool
@@ -45,8 +51,7 @@ func Bundle(options Options) (Result, error) {
sourcemap = esbuild.SourceMapInline
}
output := esbuild.Build(esbuild.BuildOptions{
EntryPoints: []string{options.EntryFile},
buildOptions := esbuild.BuildOptions{
Bundle: true,
Format: esbuild.FormatIIFE,
Target: esbuild.ES2020,
@@ -56,7 +61,26 @@ func Bundle(options Options) (Result, error) {
Sourcemap: sourcemap,
Write: false,
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 {
var messages []string