github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/pkg/plugin/builtin/js_closure_plugin.go (about)

     1  // for some bizarre reason, naming this file 'protoc_js.go' makes it be ignored
     2  // by the compiler?
     3  package builtin
     4  
     5  import (
     6  	"fmt"
     7  	"path"
     8  	"path/filepath"
     9  	"strings"
    10  
    11  	"github.com/bazelbuild/bazel-gazelle/label"
    12  	"github.com/stackb/rules_proto/pkg/protoc"
    13  )
    14  
    15  func init() {
    16  	protoc.Plugins().MustRegisterPlugin(&JsClosurePlugin{})
    17  }
    18  
    19  // JsClosurePlugin implements Plugin for the built-in protoc js/library plugin.
    20  type JsClosurePlugin struct{}
    21  
    22  // Name implements part of the Plugin interface.
    23  func (p *JsClosurePlugin) Name() string {
    24  	return "builtin:js:closure"
    25  }
    26  
    27  // Configure implements part of the Plugin interface.
    28  func (p *JsClosurePlugin) Configure(ctx *protoc.PluginContext) *protoc.PluginConfiguration {
    29  	basename := strings.ToLower(ctx.ProtoLibrary.BaseName())
    30  	library := path.Join(ctx.Rel, basename+"_closure.js")
    31  
    32  	return &protoc.PluginConfiguration{
    33  		Label:   label.New("build_stack_rules_proto", "plugin/builtin", "closurejs"),
    34  		Outputs: []string{library},
    35  		Options: append(
    36  			ctx.PluginConfig.GetOptions(),
    37  			"import_style=closure",
    38  			fmt.Sprintf("library=%s", strings.TrimSuffix(library, filepath.Ext(library))),
    39  		),
    40  	}
    41  }