minify.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Package minify relates MIME type to minifiers. Several minifiers are provided in the subpackages.
  2. package minify // import "github.com/tdewolff/minify"
  3. import (
  4. "errors"
  5. "io"
  6. "mime"
  7. "net/http"
  8. "net/url"
  9. "os/exec"
  10. "path"
  11. "regexp"
  12. "sync"
  13. "github.com/tdewolff/parse"
  14. "github.com/tdewolff/parse/buffer"
  15. )
  16. // ErrNotExist is returned when no minifier exists for a given mimetype.
  17. var ErrNotExist = errors.New("minifier does not exist for mimetype")
  18. ////////////////////////////////////////////////////////////////
  19. // MinifierFunc is a function that implements Minifer.
  20. type MinifierFunc func(*M, io.Writer, io.Reader, map[string]string) error
  21. // Minify calls f(m, w, r, params)
  22. func (f MinifierFunc) Minify(m *M, w io.Writer, r io.Reader, params map[string]string) error {
  23. return f(m, w, r, params)
  24. }
  25. // Minifier is the interface for minifiers.
  26. // The *M parameter is used for minifying embedded resources, such as JS within HTML.
  27. type Minifier interface {
  28. Minify(*M, io.Writer, io.Reader, map[string]string) error
  29. }
  30. ////////////////////////////////////////////////////////////////
  31. type patternMinifier struct {
  32. pattern *regexp.Regexp
  33. Minifier
  34. }
  35. type cmdMinifier struct {
  36. cmd *exec.Cmd
  37. }
  38. func (c *cmdMinifier) Minify(_ *M, w io.Writer, r io.Reader, _ map[string]string) error {
  39. cmd := &exec.Cmd{}
  40. *cmd = *c.cmd // concurrency safety
  41. cmd.Stdout = w
  42. cmd.Stdin = r
  43. return cmd.Run()
  44. }
  45. ////////////////////////////////////////////////////////////////
  46. // M holds a map of mimetype => function to allow recursive minifier calls of the minifier functions.
  47. type M struct {
  48. literal map[string]Minifier
  49. pattern []patternMinifier
  50. URL *url.URL
  51. }
  52. // New returns a new M.
  53. func New() *M {
  54. return &M{
  55. map[string]Minifier{},
  56. []patternMinifier{},
  57. nil,
  58. }
  59. }
  60. // Add adds a minifier to the mimetype => function map (unsafe for concurrent use).
  61. func (m *M) Add(mimetype string, minifier Minifier) {
  62. m.literal[mimetype] = minifier
  63. }
  64. // AddFunc adds a minify function to the mimetype => function map (unsafe for concurrent use).
  65. func (m *M) AddFunc(mimetype string, minifier MinifierFunc) {
  66. m.literal[mimetype] = minifier
  67. }
  68. // AddRegexp adds a minifier to the mimetype => function map (unsafe for concurrent use).
  69. func (m *M) AddRegexp(pattern *regexp.Regexp, minifier Minifier) {
  70. m.pattern = append(m.pattern, patternMinifier{pattern, minifier})
  71. }
  72. // AddFuncRegexp adds a minify function to the mimetype => function map (unsafe for concurrent use).
  73. func (m *M) AddFuncRegexp(pattern *regexp.Regexp, minifier MinifierFunc) {
  74. m.pattern = append(m.pattern, patternMinifier{pattern, minifier})
  75. }
  76. // AddCmd adds a minify function to the mimetype => function map (unsafe for concurrent use) that executes a command to process the minification.
  77. // It allows the use of external tools like ClosureCompiler, UglifyCSS, etc. for a specific mimetype.
  78. func (m *M) AddCmd(mimetype string, cmd *exec.Cmd) {
  79. m.literal[mimetype] = &cmdMinifier{cmd}
  80. }
  81. // AddCmdRegexp adds a minify function to the mimetype => function map (unsafe for concurrent use) that executes a command to process the minification.
  82. // It allows the use of external tools like ClosureCompiler, UglifyCSS, etc. for a specific mimetype regular expression.
  83. func (m *M) AddCmdRegexp(pattern *regexp.Regexp, cmd *exec.Cmd) {
  84. m.pattern = append(m.pattern, patternMinifier{pattern, &cmdMinifier{cmd}})
  85. }
  86. // Match returns the pattern and minifier that gets matched with the mediatype.
  87. // It returns nil when no matching minifier exists.
  88. // It has the same matching algorithm as Minify.
  89. func (m *M) Match(mediatype string) (string, map[string]string, MinifierFunc) {
  90. mimetype, params := parse.Mediatype([]byte(mediatype))
  91. if minifier, ok := m.literal[string(mimetype)]; ok { // string conversion is optimized away
  92. return string(mimetype), params, minifier.Minify
  93. }
  94. for _, minifier := range m.pattern {
  95. if minifier.pattern.Match(mimetype) {
  96. return minifier.pattern.String(), params, minifier.Minify
  97. }
  98. }
  99. return string(mimetype), params, nil
  100. }
  101. // Minify minifies the content of a Reader and writes it to a Writer (safe for concurrent use).
  102. // An error is returned when no such mimetype exists (ErrNotExist) or when an error occurred in the minifier function.
  103. // Mediatype may take the form of 'text/plain', 'text/*', '*/*' or 'text/plain; charset=UTF-8; version=2.0'.
  104. func (m *M) Minify(mediatype string, w io.Writer, r io.Reader) error {
  105. mimetype, params := parse.Mediatype([]byte(mediatype))
  106. return m.MinifyMimetype(mimetype, w, r, params)
  107. }
  108. // MinifyMimetype minifies the content of a Reader and writes it to a Writer (safe for concurrent use).
  109. // It is a lower level version of Minify and requires the mediatype to be split up into mimetype and parameters.
  110. // It is mostly used internally by minifiers because it is faster (no need to convert a byte-slice to string and vice versa).
  111. func (m *M) MinifyMimetype(mimetype []byte, w io.Writer, r io.Reader, params map[string]string) error {
  112. err := ErrNotExist
  113. if minifier, ok := m.literal[string(mimetype)]; ok { // string conversion is optimized away
  114. err = minifier.Minify(m, w, r, params)
  115. } else {
  116. for _, minifier := range m.pattern {
  117. if minifier.pattern.Match(mimetype) {
  118. err = minifier.Minify(m, w, r, params)
  119. break
  120. }
  121. }
  122. }
  123. return err
  124. }
  125. // Bytes minifies an array of bytes (safe for concurrent use). When an error occurs it return the original array and the error.
  126. // It returns an error when no such mimetype exists (ErrNotExist) or any error occurred in the minifier function.
  127. func (m *M) Bytes(mediatype string, v []byte) ([]byte, error) {
  128. out := buffer.NewWriter(make([]byte, 0, len(v)))
  129. if err := m.Minify(mediatype, out, buffer.NewReader(v)); err != nil {
  130. return v, err
  131. }
  132. return out.Bytes(), nil
  133. }
  134. // String minifies a string (safe for concurrent use). When an error occurs it return the original string and the error.
  135. // It returns an error when no such mimetype exists (ErrNotExist) or any error occurred in the minifier function.
  136. func (m *M) String(mediatype string, v string) (string, error) {
  137. out := buffer.NewWriter(make([]byte, 0, len(v)))
  138. if err := m.Minify(mediatype, out, buffer.NewReader([]byte(v))); err != nil {
  139. return v, err
  140. }
  141. return string(out.Bytes()), nil
  142. }
  143. // Reader wraps a Reader interface and minifies the stream.
  144. // Errors from the minifier are returned by the reader.
  145. func (m *M) Reader(mediatype string, r io.Reader) io.Reader {
  146. pr, pw := io.Pipe()
  147. go func() {
  148. if err := m.Minify(mediatype, pw, r); err != nil {
  149. pw.CloseWithError(err)
  150. } else {
  151. pw.Close()
  152. }
  153. }()
  154. return pr
  155. }
  156. // minifyWriter makes sure that errors from the minifier are passed down through Close (can be blocking).
  157. type minifyWriter struct {
  158. pw *io.PipeWriter
  159. wg sync.WaitGroup
  160. err error
  161. }
  162. // Write intercepts any writes to the writer.
  163. func (w *minifyWriter) Write(b []byte) (int, error) {
  164. return w.pw.Write(b)
  165. }
  166. // Close must be called when writing has finished. It returns the error from the minifier.
  167. func (w *minifyWriter) Close() error {
  168. w.pw.Close()
  169. w.wg.Wait()
  170. return w.err
  171. }
  172. // Writer wraps a Writer interface and minifies the stream.
  173. // Errors from the minifier are returned by Close on the writer.
  174. // The writer must be closed explicitly.
  175. func (m *M) Writer(mediatype string, w io.Writer) *minifyWriter {
  176. pr, pw := io.Pipe()
  177. mw := &minifyWriter{pw, sync.WaitGroup{}, nil}
  178. mw.wg.Add(1)
  179. go func() {
  180. defer mw.wg.Done()
  181. if err := m.Minify(mediatype, w, pr); err != nil {
  182. io.Copy(w, pr)
  183. mw.err = err
  184. }
  185. pr.Close()
  186. }()
  187. return mw
  188. }
  189. // minifyResponseWriter wraps an http.ResponseWriter and makes sure that errors from the minifier are passed down through Close (can be blocking).
  190. // All writes to the response writer are intercepted and minified on the fly.
  191. // http.ResponseWriter loses all functionality such as Pusher, Hijacker, Flusher, ...
  192. type minifyResponseWriter struct {
  193. http.ResponseWriter
  194. writer *minifyWriter
  195. m *M
  196. mediatype string
  197. }
  198. // WriteHeader intercepts any header writes and removes the Content-Length header.
  199. func (w *minifyResponseWriter) WriteHeader(status int) {
  200. w.ResponseWriter.Header().Del("Content-Length")
  201. w.ResponseWriter.WriteHeader(status)
  202. }
  203. // Write intercepts any writes to the response writer.
  204. // The first write will extract the Content-Type as the mediatype. Otherwise it falls back to the RequestURI extension.
  205. func (w *minifyResponseWriter) Write(b []byte) (int, error) {
  206. if w.writer == nil {
  207. // first write
  208. if mediatype := w.ResponseWriter.Header().Get("Content-Type"); mediatype != "" {
  209. w.mediatype = mediatype
  210. }
  211. w.writer = w.m.Writer(w.mediatype, w.ResponseWriter)
  212. }
  213. return w.writer.Write(b)
  214. }
  215. // Close must be called when writing has finished. It returns the error from the minifier.
  216. func (w *minifyResponseWriter) Close() error {
  217. if w.writer != nil {
  218. return w.writer.Close()
  219. }
  220. return nil
  221. }
  222. // ResponseWriter minifies any writes to the http.ResponseWriter.
  223. // http.ResponseWriter loses all functionality such as Pusher, Hijacker, Flusher, ...
  224. // Minification might be slower than just sending the original file! Caching is advised.
  225. func (m *M) ResponseWriter(w http.ResponseWriter, r *http.Request) *minifyResponseWriter {
  226. mediatype := mime.TypeByExtension(path.Ext(r.RequestURI))
  227. return &minifyResponseWriter{w, nil, m, mediatype}
  228. }
  229. // Middleware provides a middleware function that minifies content on the fly by intercepting writes to http.ResponseWriter.
  230. // http.ResponseWriter loses all functionality such as Pusher, Hijacker, Flusher, ...
  231. // Minification might be slower than just sending the original file! Caching is advised.
  232. func (m *M) Middleware(next http.Handler) http.Handler {
  233. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  234. mw := m.ResponseWriter(w, r)
  235. defer mw.Close()
  236. next.ServeHTTP(mw, r)
  237. })
  238. }