templ icon indicating copy to clipboard operation
templ copied to clipboard

feature: go:build tags support?

Open cethien opened this issue 1 year ago • 1 comments

go has these handy //go:build <tag> comments, allowing stuff like this:

config_dev.go

//go:build dev

package config

const Port = 8080

config.go

//go:build !dev

package config

const Port = 80

sadly, templ doesnt seem to support that. right now i'm using a bool variable "AppEnvDev" in a config and do stuff like this:

page.templ

package views

...

templ js() {
	if config.AppEnvDev {
		<script src="main.js"></script>
	} else {
		<script src="main.min.js"></script>
	}
}

does it job, but dev stuff still gets shipped in the final binary.

any possbility to support said tags? i'm not familiar how go build tags work with generated code, so dont know about any limitation

cethien avatar Mar 17 '24 23:03 cethien

Currently comments above the package declaration are not copied over to the generated files, once this is fixed I believe this should support build tags too.

Here the package declaration is written, we would also have to write the contents of TemplateFile.Header too. https://github.com/a-h/templ/blob/main/generator/generator.go#L104

joerdav avatar Mar 18 '24 09:03 joerdav