Skip to content

Commit

Permalink
Update docs, remove --mime in favor of --type, add mimetype for webma…
Browse files Browse the repository at this point in the history
…nifest extension, see #438
  • Loading branch information
tdewolff committed Sep 29, 2023
1 parent dfaa2fc commit 8b7e1ad
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 37 deletions.
17 changes: 11 additions & 6 deletions cmd/minify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,32 +149,37 @@ $ minify --type=html -o index-min.tpl index.tpl

You need to set the type or the mimetype option when using standard input:
```sh
$ minify --mime=application/javascript < script.js > script-min.js
$ minify --type=application/javascript < script.js > script-min.js

$ cat script.js | minify --type=js > script-min.js
```

### Directories
You can also give directories as input, and these directories can be minified recursively.

Minify files in the current working directory to **out/** (no subdirectories):
Minify files in the current working directory to **out/...** (excluding subdirectories):
```sh
$ minify -o out/ *
```

Minify files recursively in **src/**:
Minify files recursively in **src/...** to **out/src/...**:
```sh
$ minify -r -o out/ src
```

Minify files recursively in **src/...** to **out/...**:
```sh
$ minify -r -o out/ src/
```

Minify only javascript files in **src/**:
```sh
$ minify -r -o out/ --match="\.js$" src
$ minify -r -o out/ --match="*.js" src/
```

A trailing slash in the source path will copy all files inside the directory, while omitting the trainling slash will copy the directory as well. Both `src/` and `src/*` are equivalent, except that the second case uses input expansion from bash and ignores hidden files starting with a dot.
A trailing slash in the source path will copy all files inside the directory, while omitting the trainling slash will copy the directory as well. Both `src/` and `src/.` are equivalent, however `src/*` uses input expansion from bash and ignores hidden files starting with a dot.

A trailing slash in the destination path will write a single file into a directory instead of to a file of that name.
A trailing slash in the destination path forces writing into a directory. This removes ambiguity when minifying a single file which would otherwise write to a file.

### Concatenate
When multiple inputs are given and the output is either standard output or a single file, it will concatenate the files together if you use the bundle option.
Expand Down
7 changes: 2 additions & 5 deletions cmd/minify/bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ _minify_complete() {
local cur prev flags mimes types
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
flags="-a --all --bundle --exclude --include -l --list --match --mime -o --output -p --preserve -q --quiet -r --recursive --type --url -v --verbose --version -w --watch --css-precision --html-keep-comments --html-keep-conditional-comments --html-keep-default-attrvals --html-keep-document-tags --html-keep-end-tags --html-keep-quotes --html-keep-whitespace --js-precision --js-keep-var-names --js-version --json-precision --json-keep-numbers --svg-keep-comments --svg-precision -s --sync --xml-keep-whitespace"
mimes="text/css text/html text/javascript application/javascript application/json image/svg+xml text/xml application/xml"
types="css html js json svg xml"
flags="-a --all --bundle --exclude --include -l --list --match -o --output -p --preserve -q --quiet -r --recursive --type --url -v --verbose --version -w --watch --css-precision --html-keep-comments --html-keep-conditional-comments --html-keep-default-attrvals --html-keep-document-tags --html-keep-end-tags --html-keep-quotes --html-keep-whitespace --js-precision --js-keep-var-names --js-version --json-precision --json-keep-numbers --svg-keep-comments --svg-precision -s --sync --xml-keep-whitespace"
types="css html js json svg xml text/css text/html text/javascript application/javascript application/json image/svg+xml text/xml application/xml"

if echo "${cur}" | grep -Eq '^-'; then
COMPREPLY=($(compgen -W "${flags}" -- "${cur}"))
elif echo "${prev}" | grep -Eq '^--mime$'; then
COMPREPLY=($(compgen -W "${mimes}" -- "${cur}"))
elif echo "${prev}" | grep -Eq '^--type$'; then
COMPREPLY=($(compgen -W "${types}" -- "${cur}"))
elif echo "${prev}" | grep -Eq '^--(exclude|include|match|url|css-precision|js-precision|json-precision|svg-precision)$'; then
Expand Down
57 changes: 31 additions & 26 deletions cmd/minify/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ import (
// Version is the current minify version.
var Version = "built from source"

var filetypeMime = map[string]string{
"css": "text/css",
"htm": "text/html",
"html": "text/html",
"js": "application/javascript",
"mjs": "application/javascript",
"json": "application/json",
"svg": "image/svg+xml",
"xml": "text/xml",
var extMap = map[string]string{
"css": "text/css",
"htm": "text/html",
"html": "text/html",
"js": "application/javascript",
"mjs": "application/javascript",
"json": "application/json",
"svg": "image/svg+xml",
"xml": "text/xml",
"webmanifest": "application/manifest+json",
}

var (
Expand All @@ -64,6 +65,7 @@ var (
preserveTimestamps bool
preserveLinks bool
mimetype string
oldmimetype string
)

// Task is a minify task.
Expand Down Expand Up @@ -100,7 +102,6 @@ func main() {

func run() int {
output := ""
filetype := ""
siteurl := ""

cssMinifier := &css.Minifier{}
Expand All @@ -114,13 +115,13 @@ func run() int {
f.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] [input]\n\nOptions:\n", os.Args[0])
f.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nInput:\n Files or directories, leave blank to use stdin. Specify --mime or --type to use stdin and stdout.\n")
fmt.Fprintf(os.Stderr, "\nInput:\n Files or directories, leave blank to use stdin. Specify --type to use stdin and stdout.\n")
}

f.BoolVarP(&help, "help", "h", false, "Show usage")
f.StringVarP(&output, "output", "o", "", "Output file or directory (must have trailing slash), leave blank to use stdout")
f.StringVar(&mimetype, "mime", "", "Mimetype (eg. text/css), optional for input filenames, has precedence over --type")
f.StringVar(&filetype, "type", "", "Filetype (eg. css), optional for input filenames")
f.StringVar(&oldmimetype, "mime", "", "Mimetype (eg. text/css), optional for input filenames (DEPRECATED, use --type)")
f.StringVar(&mimetype, "type", "", "Filetype (eg. css or text/css), optional for input filenames")
f.String("match", "", "Filename matching pattern, only matching files are processed")
f.String("include", "", "Filename inclusion pattern, includes files previously excluded")
f.String("exclude", "", "Filename exclusion pattern, excludes files from being processed")
Expand Down Expand Up @@ -155,7 +156,7 @@ func run() int {
f.BoolVar(&xmlMinifier.KeepWhitespace, "xml-keep-whitespace", false, "Preserve whitespace characters but still collapse multiple into one")
if len(os.Args) == 1 {
if !quiet {
fmt.Printf("minify: must specify --mime or --type in order to use stdin and stdout\n")
fmt.Printf("minify: must specify --type in order to use stdin and stdout\n")
fmt.Printf("Try 'minify --help' for more information\n")
}
return 1
Expand Down Expand Up @@ -218,12 +219,12 @@ func run() int {
if list {
if !quiet {
var keys []string
for k := range filetypeMime {
for k := range extMap {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
fmt.Println(k + "\t" + filetypeMime[k])
fmt.Println(k + "\t" + extMap[k])
}
}
return 0
Expand All @@ -247,10 +248,14 @@ func run() int {
}

// detect mimetype, mimetype=="" means we'll infer mimetype from file extensions
if mimetype == "" && filetype != "" {
if oldmimetype != "" {
Error.Println("deprecated use of '--mime %v', please use '--type %v' instead", oldmimetype, oldmimetype)

Check failure on line 252 in cmd/minify/main.go

View workflow job for this annotation

GitHub Actions / build

(*log.Logger).Println call has possible Printf formatting directive %v
mimetype = oldmimetype
}
if slash := strings.Index(mimetype, "/"); slash == -1 && 0 < len(mimetype) {
var ok bool
if mimetype, ok = filetypeMime[filetype]; !ok {
Error.Println("cannot find mimetype for filetype", filetype)
if mimetype, ok = extMap[mimetype]; !ok {
Error.Println("unknown filetype", mimetype)
return 1
}
}
Expand All @@ -276,10 +281,10 @@ func run() int {
return 1
}
if mimetype == "" && useStdin {
Error.Println("must specify --mime or --type for stdin")
Error.Println("must specify --type for stdin")
return 1
} else if mimetype != "" && sync {
Error.Println("must specify either --sync or --mime/--type")
Error.Println("must specify either --sync or --type")
return 1
}
if mimetype == "" {
Expand Down Expand Up @@ -555,7 +560,7 @@ func fileMatches(filename string) bool {
if 0 < len(ext) {
ext = ext[1:]
}
if _, ok := filetypeMime[ext]; !ok {
if _, ok := extMap[ext]; !ok {
return false
}
return true
Expand Down Expand Up @@ -691,15 +696,15 @@ func minify(t Task) bool {
if 0 < len(ext) {
ext = ext[1:]
}
srcMimetype, ok := filetypeMime[ext]
srcMimetype, ok := extMap[ext]
if !ok {
Warning.Println("cannot infer mimetype from extension in", src, ", set --type or --mime explicitly")
Warning.Println("cannot infer mimetype from extension in", src, ", set --type explicitly")
return false
}
if fileMimetype == "" {
fileMimetype = srcMimetype
} else if srcMimetype != fileMimetype {
Warning.Println("inferred mimetype", srcMimetype, "of", src, "for concatenation unequal to previous mimetypes, set --type or --mime explicitly")
Warning.Println("inferred mimetype", srcMimetype, "of", src, "for concatenation unequal to previous mimetypes, set --type explicitly")
return false
}
}
Expand Down Expand Up @@ -740,7 +745,7 @@ func minify(t Task) bool {
fr, err = openInputFile(t.srcs[0])
} else {
var sep []byte
if err == nil && fileMimetype == filetypeMime["js"] {
if err == nil && fileMimetype == extMap["js"] {
sep = []byte(";\n")
}
fr, err = openInputFiles(t.srcs, sep)
Expand Down

0 comments on commit 8b7e1ad

Please sign in to comment.