Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.754
0.2.759
3 changes: 3 additions & 0 deletions cmd/templ/generatecmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ func (cmd Generate) Run(ctx context.Context) (err error) {
if cmd.Args.IncludeTimestamp {
opts = append(opts, generator.WithTimestamp(time.Now()))
}
if cmd.Args.MinifyJS {
opts = append(opts, generator.WithJSMinification())
}

// Check the version of the templ module.
if err := modcheck.Check(cmd.Args.Path); err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/templ/generatecmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Arguments struct {
GenerateSourceMapVisualisations bool
IncludeVersion bool
IncludeTimestamp bool
MinifyJS bool
// PPROFPort is the port to run the pprof server on.
PPROFPort int
KeepOrphanedFiles bool
Expand Down
4 changes: 4 additions & 0 deletions cmd/templ/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ Args:
Set to false to skip inclusion of the templ version in the generated code. (default true)
-include-timestamp
Set to true to include the current time in the generated code.
-minify-js
Minify Javascript script blocks. (default false)
-watch
Set to true to watch the path for changes and regenerate code.
-cmd <cmd>
Expand Down Expand Up @@ -203,6 +205,7 @@ func generateCmd(stdout, stderr io.Writer, args []string) (code int) {
sourceMapVisualisationsFlag := cmd.Bool("source-map-visualisations", false, "")
includeVersionFlag := cmd.Bool("include-version", true, "")
includeTimestampFlag := cmd.Bool("include-timestamp", false, "")
minifyJSFlag := cmd.Bool("minify-js", false, "")
watchFlag := cmd.Bool("watch", false, "")
openBrowserFlag := cmd.Bool("open-browser", true, "")
cmdFlag := cmd.String("cmd", "", "")
Expand Down Expand Up @@ -257,6 +260,7 @@ func generateCmd(stdout, stderr io.Writer, args []string) (code int) {
GenerateSourceMapVisualisations: *sourceMapVisualisationsFlag,
IncludeVersion: *includeVersionFlag,
IncludeTimestamp: *includeTimestampFlag,
MinifyJS: *minifyJSFlag,
PPROFPort: *pprofPortFlag,
KeepOrphanedFiles: *keepOrphanedFilesFlag,
})
Expand Down
2 changes: 2 additions & 0 deletions docs/docs/04-core-concepts/02-template-generation.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Args:
Set to false to skip inclusion of the templ version in the generated code. (default true)
-include-timestamp
Set to true to include the current time in the generated code.
-minify-js
Minify Javascript script blocks. (default false)
-watch
Set to true to watch the path for changes and regenerate code.
-cmd <cmd>
Expand Down
2 changes: 2 additions & 0 deletions docs/docs/09-commands-and-tools/01-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Args:
Set to false to skip inclusion of the templ version in the generated code. (default true)
-include-timestamp
Set to true to include the current time in the generated code.
-minify-js
Minify Javascript script blocks. (default false)
-watch
Set to true to watch the path for changes and regenerate code.
-cmd <cmd>
Expand Down
25 changes: 25 additions & 0 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
_ "embed"

"github.com/a-h/templ/parser/v2"
"github.com/tdewolff/minify/v2/minify"
)

type GenerateOpt func(g *generator) error
Expand Down Expand Up @@ -67,6 +68,13 @@ func WithSkipCodeGeneratedComment() GenerateOpt {
}
}

func WithJSMinification() GenerateOpt {
return func(g *generator) error {
g.minifyJS = true
return nil
}
}

// Generate generates Go code from the input template file to w, and returns a map of the location of Go expressions in the template
// to the location of the generated Go code in the output.
func Generate(template parser.TemplateFile, w io.Writer, opts ...GenerateOpt) (sm *parser.SourceMap, literals string, err error) {
Expand Down Expand Up @@ -101,6 +109,7 @@ type generator struct {
fileName string
// skipCodeGeneratedComment skips the code generated comment at the top of the file.
skipCodeGeneratedComment bool
minifyJS bool
}

func (g *generator) generate() (err error) {
Expand Down Expand Up @@ -1302,6 +1311,15 @@ func (g *generator) writeRawElement(indentLevel int, n parser.RawElement) (err e
return err
}
}
// handles script elements within a templ component
// ex: templ Component() { <script ...>...</script> }
if g.minifyJS && html.EscapeString(n.Name) == "script" {
minified, err := minify.JS(n.Contents)
if err != nil {
return err
}
n.Contents = minified
}
// Contents.
if err = g.writeText(indentLevel, parser.Text{Value: n.Contents}); err != nil {
return err
Expand Down Expand Up @@ -1460,6 +1478,13 @@ func (g *generator) writeScript(t parser.ScriptTemplate) error {
prefix := "function " + fn + "(" + stripTypes(t.Parameters.Value) + "){"
body := strings.TrimLeftFunc(t.Value, unicode.IsSpace)
suffix := "}"
// handles contents of a script component
// ex: script Component() { ... }
if g.minifyJS {
if body, err = minify.JS(body); err != nil {
return err
}
}
if _, err = g.w.WriteIndent(indentLevel, "Function: "+createGoString(prefix+body+suffix)+",\n"); err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ require (
golang.org/x/tools v0.13.0
)

require github.com/tdewolff/parse/v2 v2.7.15 // indirect

require (
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/segmentio/encoding v0.4.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/tdewolff/minify/v2 v2.20.37
go.lsp.dev/pkg v0.0.0-20210717090340-384b27a52fb2 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/net v0.24.0 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ github.com/segmentio/encoding v0.4.0 h1:MEBYvRqiUB2nfR2criEXWqwdY6HJOUrCn5hboVOV
github.com/segmentio/encoding v0.4.0/go.mod h1:/d03Cd8PoaDeceuhUUUQWjU0KhWjrmYrWPgtJHYZSnI=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tdewolff/minify/v2 v2.20.37 h1:Q97cx4STXCh1dlWDlNHZniE8BJ2EBL0+2b0n92BJQhw=
github.com/tdewolff/minify/v2 v2.20.37/go.mod h1:L1VYef/jwKw6Wwyk5A+T0mBjjn3mMPgmjjA688RNsxU=
github.com/tdewolff/parse/v2 v2.7.15 h1:hysDXtdGZIRF5UZXwpfn3ZWRbm+ru4l53/ajBRGpCTw=
github.com/tdewolff/parse/v2 v2.7.15/go.mod h1:3FbJWZp3XT9OWVN3Hmfp0p/a08v4h8J9W1aghka0soA=
github.com/tdewolff/test v1.0.11-0.20231101010635-f1265d231d52/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE=
github.com/tdewolff/test v1.0.11-0.20240106005702-7de5f7df4739 h1:IkjBCtQOOjIn03u/dMQK9g+Iw9ewps4mCl1nB8Sscbo=
github.com/tdewolff/test v1.0.11-0.20240106005702-7de5f7df4739/go.mod h1:XPuWBzvdUzhCuxWO1ojpXsyzsA5bFoS3tO/Q3kFuTG8=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.lsp.dev/jsonrpc2 v0.10.0 h1:Pr/YcXJoEOTMc/b6OTmcR1DPJ3mSWl/SWiU1Cct6VmI=
go.lsp.dev/jsonrpc2 v0.10.0/go.mod h1:fmEzIdXPi/rf6d4uFcayi8HpFP1nBF99ERP1htC72Ac=
Expand Down