Skip to content

Commit 228ca98

Browse files
committed
cmd: fix --inplace bugs, set default mimetype for .tmpl to text/x-template
1 parent 60a240a commit 228ca98

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

cmd/minify/io.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func SameFile(filename1 string, filename2 string) (bool, error) {
3333

3434
func openInputFile(input string) (io.ReadCloser, error) {
3535
var r *os.File
36-
if input == "" {
36+
if input == "-" {
3737
r = os.Stdin
3838
} else {
3939
err := try.Do(func(attempt int) (bool, error) {
@@ -55,7 +55,7 @@ func openInputFiles(filenames []string, sep []byte) (*concatFileReader, error) {
5555

5656
func openOutputFile(output string) (*os.File, error) {
5757
var w *os.File
58-
if output == "" {
58+
if output == "-" {
5959
w = os.Stdout
6060
} else {
6161
dir := filepath.Dir(output)

cmd/minify/main.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var extMap = map[string]string{
4848
"php": "application/x-httpd-php",
4949
"rss": "application/rss+xml",
5050
"svg": "image/svg+xml",
51-
"tmpl": "text/x-go-template",
51+
"tmpl": "text/x-template",
5252
"webmanifest": "application/manifest+json",
5353
"xhtml": "application/xhtml-xml",
5454
"xml": "text/xml",
@@ -165,7 +165,7 @@ type Task struct {
165165
func NewTask(root, input, output string, sync bool) (Task, error) {
166166
if output == "" {
167167
output = input // in-place
168-
} else if output == "." || output[len(output)-1] == os.PathSeparator || output[len(output)-1] == '/' {
168+
} else if output != "-" && (output == "." || output[len(output)-1] == os.PathSeparator || output[len(output)-1] == '/') {
169169
rel, err := filepath.Rel(root, input)
170170
if err != nil {
171171
return Task{}, err
@@ -236,6 +236,7 @@ func run() int {
236236
f.AddOpt(&htmlMinifier.KeepEndTags, "", "html-keep-end-tags", "Preserve all end tags")
237237
f.AddOpt(&htmlMinifier.KeepWhitespace, "", "html-keep-whitespace", "Preserve whitespace characters but still collapse multiple into one")
238238
f.AddOpt(&htmlMinifier.KeepQuotes, "", "html-keep-quotes", "Preserve quotes around attribute values")
239+
//f.AddOpt(&htmlMinifier.TemplateDelims, "", "html-template-delims", "Set template delimiters explicitly, for example <?,?> for PHP or {{,}} for Go templates") // TODO: fix parsing {{ }} in tdewolff/argp
239240
f.AddOpt(&jsMinifier.Precision, "", "js-precision", "Number of significant digits to preserve in numbers, 0 is all")
240241
f.AddOpt(&jsMinifier.KeepVarNames, "", "js-keep-var-names", "Preserve original variable names")
241242
f.AddOpt(&jsMinifier.Version, "", "js-version", "ECMAScript version to toggle supported optimizations (e.g. 2019, 2020), by default 0 is the latest version")
@@ -280,8 +281,8 @@ func run() int {
280281

281282
if len(inputs) == 1 && inputs[0] == "-" {
282283
inputs = inputs[:0] // stdin
283-
} else if output == "-" {
284-
output = "" // stdout
284+
} else if !inplace && output == "" {
285+
output = "-" // stdout
285286
}
286287
useStdin := len(inputs) == 0
287288

@@ -344,7 +345,7 @@ func run() int {
344345
Error.Println("cannot specify both --inplace and --output")
345346
}
346347
return 1
347-
} else if (useStdin || output == "" && !inplace) && (watch || sync) {
348+
} else if (useStdin || output == "-") && (watch || sync) {
348349
if watch {
349350
Error.Println("--watch doesn't work with stdin and stdout, specify input and output")
350351
}
@@ -360,7 +361,7 @@ func run() int {
360361
Error.Println("--recursive doesn't work with stdin, specify input")
361362
}
362363
return 1
363-
} else if output == "" && !inplace && recursive && !bundle {
364+
} else if output == "-" && recursive && !bundle {
364365
Error.Println("--recursive doesn't work with stdout, specify output or use --bundle")
365366
return 1
366367
}
@@ -419,7 +420,7 @@ func run() int {
419420

420421
// set output file or directory, empty means stdout
421422
dirDst := false
422-
if output != "" {
423+
if output != "" && output != "-" {
423424
if 0 < len(output) && (output[len(output)-1] == os.PathSeparator || output[len(output)-1] == '/') {
424425
dirDst = true
425426
} else if !bundle && 1 < len(inputs) {
@@ -437,16 +438,19 @@ func run() int {
437438
if dirDst {
438439
output += string(os.PathSeparator)
439440
}
440-
} else if !inplace && !bundle && 1 < len(inputs) {
441+
} else if output == "-" && !bundle && 1 < len(inputs) {
441442
Error.Println("must specify --bundle for multiple input files with stdout destination")
442443
return 1
444+
} else if inplace && bundle {
445+
Error.Println("--bundle cannot be used together with --inplace")
446+
return 1
443447
}
444448
if useStdin {
445449
Debug.Println("minify from stdin")
446450
}
447451
if inplace {
448452
Debug.Println("minify in-place")
449-
} else if output == "" {
453+
} else if output == "-" {
450454
Debug.Println("minify to stdout")
451455
} else if !dirDst {
452456
Debug.Printf("minify to output file %v", output)
@@ -459,7 +463,7 @@ func run() int {
459463
var tasks []Task
460464
var roots []string
461465
if useStdin {
462-
task, err := NewTask("", "", output, false)
466+
task, err := NewTask("", "-", output, false)
463467
if err != nil {
464468
Error.Println(err)
465469
return 1
@@ -513,6 +517,7 @@ func run() int {
513517

514518
tmplMinifier := htmlMinifier
515519
tmplMinifier.TemplateDelims = [2]string{"{{", "}}"}
520+
m.Add("text/x-template", &tmplMinifier)
516521
m.Add("text/x-go-template", &tmplMinifier)
517522
m.Add("text/x-mustache-template", &tmplMinifier)
518523
m.Add("text/x-handlebars-template", &tmplMinifier)
@@ -829,7 +834,7 @@ func minify(t Task) bool {
829834
srcName = "stdin"
830835
}
831836
dstName := t.dst
832-
if dstName == "" {
837+
if dstName == "-" {
833838
dstName = "stdout"
834839
} else {
835840
// rename original when overwriting

0 commit comments

Comments
 (0)