@@ -20,6 +20,8 @@ type EvaluateConfig struct {
2020 // before evaluation. If an error is returned from any of these functions, the evaluation process is aborted.
2121 // The script hook can make modifications and return them to the script if necessary.
2222 ScriptHooks []func (string ) (string , error )
23+ // ScriptPreTranspileHooks are called before transpiling (if applicable) with the script that will be evaluated
24+ ScriptPreTranspileHooks []func (string ) (string , error )
2325 // Transpile indicates whether the script should be transpiled before its evaluated in the runtime.
2426 Transpile bool
2527 // TranspileOptions are options passed directly to the transpiler if applicable
@@ -81,6 +83,13 @@ func WithScriptHook(hook func(script string) (string, error)) EvaluateOptionFunc
8183 }
8284}
8385
86+ // WithScriptPreTranspileHook adds a script hook that should be evaluated immediately before transpiling the script
87+ func WithScriptPreTranspileHook (hook func (script string ) (string , error )) EvaluateOptionFunc {
88+ return func (cfg * EvaluateConfig ) {
89+ cfg .ScriptPreTranspileHooks = append (cfg .ScriptPreTranspileHooks , hook )
90+ }
91+ }
92+
8493// Evaluate calls EvaluateCtx using the default background context
8594func Evaluate (src io.Reader , opts ... EvaluateOptionFunc ) (goja.Value , error ) {
8695 return EvaluateCtx (context .Background (), src , opts ... )
@@ -143,6 +152,12 @@ func EvaluateCtx(ctx context.Context, src io.Reader, opts ...EvaluateOptionFunc)
143152 for _ , opt := range cfg .TranspileOptions {
144153 opts = append (opts , opt )
145154 }
155+ for _ , h := range cfg .ScriptPreTranspileHooks {
156+ script , err = h (script )
157+ if err != nil {
158+ return nil , fmt .Errorf ("running script pre-transpile hook: %w" , err )
159+ }
160+ }
146161 script , err = TranspileCtx (ctx , strings .NewReader (script ), opts ... )
147162 if err != nil {
148163 return nil , fmt .Errorf ("transpiling script: %w" , err )
0 commit comments