From e1471c7f6594da256f83c67ba4524356cd1fa4c8 Mon Sep 17 00:00:00 2001 From: garrettladley Date: Sat, 6 Jul 2024 12:16:29 -0400 Subject: [PATCH 1/7] added note about removal of implicit context import --- docs/docs/03-syntax-and-usage/14-context.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/docs/03-syntax-and-usage/14-context.md b/docs/docs/03-syntax-and-usage/14-context.md index fdf727771..4b2c560b9 100644 --- a/docs/docs/03-syntax-and-usage/14-context.md +++ b/docs/docs/03-syntax-and-usage/14-context.md @@ -134,6 +134,10 @@ templ themeName() { } ``` +:::note +As of v0.2.731, Go's built in `context` package is no longer implicitly imported into .templ files. +::: + ## Using `context` with HTTP middleware In HTTP applications, a common pattern is to insert HTTP middleware into the request/response chain. From a3bc918cd1d9a3e7ab0503dc8024fbad4d8ae1be Mon Sep 17 00:00:00 2001 From: garrettladley Date: Sun, 11 Aug 2024 17:01:47 -0400 Subject: [PATCH 2/7] feat: minifyjs arg for templ generate --- .version | 2 +- cmd/templ/generatecmd/cmd.go | 3 +++ cmd/templ/generatecmd/main.go | 1 + cmd/templ/main.go | 4 ++++ docs/docs/09-commands-and-tools/01-cli.md | 2 ++ generator/generator.go | 22 ++++++++++++++++++++++ go.mod | 3 +++ go.sum | 7 +++++++ 8 files changed, 43 insertions(+), 1 deletion(-) diff --git a/.version b/.version index f38c8c40c..5cc385c97 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -0.2.754 \ No newline at end of file +0.2.759 \ No newline at end of file diff --git a/cmd/templ/generatecmd/cmd.go b/cmd/templ/generatecmd/cmd.go index dbbd2ccbe..7db9dc0be 100644 --- a/cmd/templ/generatecmd/cmd.go +++ b/cmd/templ/generatecmd/cmd.go @@ -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 { diff --git a/cmd/templ/generatecmd/main.go b/cmd/templ/generatecmd/main.go index d72121790..6c0bd3492 100644 --- a/cmd/templ/generatecmd/main.go +++ b/cmd/templ/generatecmd/main.go @@ -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 diff --git a/cmd/templ/main.go b/cmd/templ/main.go index ce6ad10ad..ebdbecf1b 100644 --- a/cmd/templ/main.go +++ b/cmd/templ/main.go @@ -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 @@ -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", "", "") @@ -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, }) diff --git a/docs/docs/09-commands-and-tools/01-cli.md b/docs/docs/09-commands-and-tools/01-cli.md index 6d74fd5cd..f83e67e4b 100644 --- a/docs/docs/09-commands-and-tools/01-cli.md +++ b/docs/docs/09-commands-and-tools/01-cli.md @@ -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 diff --git a/generator/generator.go b/generator/generator.go index 5614e22ae..fba507972 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -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 @@ -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) { @@ -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) { @@ -1302,6 +1311,13 @@ func (g *generator) writeRawElement(indentLevel int, n parser.RawElement) (err e return err } } + if g.minifyJS { + 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 @@ -1460,6 +1476,12 @@ func (g *generator) writeScript(t parser.ScriptTemplate) error { prefix := "function " + fn + "(" + stripTypes(t.Parameters.Value) + "){" body := strings.TrimLeftFunc(t.Value, unicode.IsSpace) suffix := "}" + // impacts script elements (script instad of templ) + 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 } diff --git a/go.mod b/go.mod index 8708add70..674f65f78 100644 --- a/go.mod +++ b/go.mod @@ -24,6 +24,8 @@ 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 @@ -31,6 +33,7 @@ require ( 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 diff --git a/go.sum b/go.sum index cfc3b4216..e1012d83f 100644 --- a/go.sum +++ b/go.sum @@ -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= From 04ccd4c1d2e703ee750c2103214e5eef1b3848af Mon Sep 17 00:00:00 2001 From: garrettladley Date: Sun, 11 Aug 2024 17:03:22 -0400 Subject: [PATCH 3/7] fix: update template-generation --- docs/docs/04-core-concepts/02-template-generation.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/docs/04-core-concepts/02-template-generation.md b/docs/docs/04-core-concepts/02-template-generation.md index f2b24eb36..1f582b63e 100644 --- a/docs/docs/04-core-concepts/02-template-generation.md +++ b/docs/docs/04-core-concepts/02-template-generation.md @@ -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 From e072d1f9e42f270f81bbfb92efcb7d33b0262a6f Mon Sep 17 00:00:00 2001 From: garrettladley Date: Sun, 11 Aug 2024 17:18:08 -0400 Subject: [PATCH 4/7] fix: update comments --- generator/generator.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/generator/generator.go b/generator/generator.go index fba507972..1082c45a7 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -6,6 +6,7 @@ import ( "fmt" "html" "io" + "log/slog" "path/filepath" "reflect" "strconv" @@ -1311,7 +1312,9 @@ func (g *generator) writeRawElement(indentLevel int, n parser.RawElement) (err e return err } } - if g.minifyJS { + // handles script elements within a templ component + // ex: templ Component() { } + if g.minifyJS && html.EscapeString(n.Name) == "script" { minified, err := minify.JS(n.Contents) if err != nil { return err @@ -1323,6 +1326,7 @@ func (g *generator) writeRawElement(indentLevel int, n parser.RawElement) (err e return err } // + slog.Info(html.EscapeString(n.Name)) if _, err = g.w.WriteStringLiteral(indentLevel, fmt.Sprintf(``, html.EscapeString(n.Name))); err != nil { return err } @@ -1476,7 +1480,8 @@ func (g *generator) writeScript(t parser.ScriptTemplate) error { prefix := "function " + fn + "(" + stripTypes(t.Parameters.Value) + "){" body := strings.TrimLeftFunc(t.Value, unicode.IsSpace) suffix := "}" - // impacts script elements (script instad of templ) + // handles contents of a script component + // ex: script Component() { ... } if g.minifyJS { if body, err = minify.JS(body); err != nil { return err From efc166f5f8da66241d165aa496b3751f5441eb34 Mon Sep 17 00:00:00 2001 From: garrettladley Date: Sun, 11 Aug 2024 17:23:34 -0400 Subject: [PATCH 5/7] fix: remove stray slog.Info(...) --- generator/generator.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/generator/generator.go b/generator/generator.go index 1082c45a7..6f151564b 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -6,7 +6,6 @@ import ( "fmt" "html" "io" - "log/slog" "path/filepath" "reflect" "strconv" @@ -1326,7 +1325,6 @@ func (g *generator) writeRawElement(indentLevel int, n parser.RawElement) (err e return err } // - slog.Info(html.EscapeString(n.Name)) if _, err = g.w.WriteStringLiteral(indentLevel, fmt.Sprintf(``, html.EscapeString(n.Name))); err != nil { return err } From 1a607739ae967018705c80fecd91b5c9da5cb19c Mon Sep 17 00:00:00 2001 From: garrettladley Date: Sun, 11 Aug 2024 17:36:41 -0400 Subject: [PATCH 6/7] fix: attempt to fix CI --- generator/generator.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/generator/generator.go b/generator/generator.go index 6f151564b..707bccac5 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -8,6 +8,7 @@ import ( "io" "path/filepath" "reflect" + "regexp" "strconv" "strings" "time" @@ -16,9 +17,16 @@ import ( _ "embed" "github.com/a-h/templ/parser/v2" - "github.com/tdewolff/minify/v2/minify" + "github.com/tdewolff/minify/v2" + "github.com/tdewolff/minify/v2/js" ) +var minifyer *minify.M + +func init() { + setupMinifyer() +} + type GenerateOpt func(g *generator) error // WithVersion enables the version to be included in the generated code. @@ -1314,7 +1322,7 @@ func (g *generator) writeRawElement(indentLevel int, n parser.RawElement) (err e // handles script elements within a templ component // ex: templ Component() { } if g.minifyJS && html.EscapeString(n.Name) == "script" { - minified, err := minify.JS(n.Contents) + minified, err := minifyer.String("application/javascript", n.Contents) if err != nil { return err } @@ -1481,7 +1489,7 @@ func (g *generator) writeScript(t parser.ScriptTemplate) error { // handles contents of a script component // ex: script Component() { ... } if g.minifyJS { - if body, err = minify.JS(body); err != nil { + if body, err = minifyer.String("application/javascript", body); err != nil { return err } } @@ -1537,3 +1545,8 @@ func stripTypes(parameters string) string { } return strings.Join(variableNames, ", ") } + +func setupMinifyer() { + minifyer = minify.New() + minifyer.AddFuncRegexp(regexp.MustCompile(`^(application|text)/(x-)?(java|ecma|j|live)script(1\\.[0-5])?$|^module$`), js.Minify) +} From 1f97cb9c77a298052daca6c4c442881ab3a7ab10 Mon Sep 17 00:00:00 2001 From: garrettladley Date: Sun, 11 Aug 2024 17:46:43 -0400 Subject: [PATCH 7/7] fix: revert ci debugging --- generator/generator.go | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/generator/generator.go b/generator/generator.go index 707bccac5..6f151564b 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -8,7 +8,6 @@ import ( "io" "path/filepath" "reflect" - "regexp" "strconv" "strings" "time" @@ -17,16 +16,9 @@ import ( _ "embed" "github.com/a-h/templ/parser/v2" - "github.com/tdewolff/minify/v2" - "github.com/tdewolff/minify/v2/js" + "github.com/tdewolff/minify/v2/minify" ) -var minifyer *minify.M - -func init() { - setupMinifyer() -} - type GenerateOpt func(g *generator) error // WithVersion enables the version to be included in the generated code. @@ -1322,7 +1314,7 @@ func (g *generator) writeRawElement(indentLevel int, n parser.RawElement) (err e // handles script elements within a templ component // ex: templ Component() { } if g.minifyJS && html.EscapeString(n.Name) == "script" { - minified, err := minifyer.String("application/javascript", n.Contents) + minified, err := minify.JS(n.Contents) if err != nil { return err } @@ -1489,7 +1481,7 @@ func (g *generator) writeScript(t parser.ScriptTemplate) error { // handles contents of a script component // ex: script Component() { ... } if g.minifyJS { - if body, err = minifyer.String("application/javascript", body); err != nil { + if body, err = minify.JS(body); err != nil { return err } } @@ -1545,8 +1537,3 @@ func stripTypes(parameters string) string { } return strings.Join(variableNames, ", ") } - -func setupMinifyer() { - minifyer = minify.New() - minifyer.AddFuncRegexp(regexp.MustCompile(`^(application|text)/(x-)?(java|ecma|j|live)script(1\\.[0-5])?$|^module$`), js.Minify) -}