Skip to content

Command line

korpress makes HTML, CSS and JS files smaller from the shell. This is called minifying: it removes the comments, spaces and line breaks that a browser does not need, so the files load faster and work the same. The command installs with the package: pip install --pre korpress.

One file to the screen

Take a small file, style.css:

/* Main styles */
body {
    margin : 0px ;
    color : #ffffff ;
}

h1 {
    font-size : 2em ;
}

With one file and no other option, korpress prints the result on stdout (the screen) and no statistics. The file is not changed.

korpress style.css
body{margin:0;color:#fff}h1{font-size:2em}

The result has no final line break, so the next prompt may follow it on the same line.

To save the result in a file, use -o, see Options.

The other sample files

The next examples also use these two small files. app.js:

// Greets the user
function greet(name) {
    console.log("Hello, " + name) ;
}

greet("world") ;

page.html:

<!DOCTYPE html>
<html>
  <head>
    <!-- page title -->
    <title>Hello</title>
    <style>
      p { color : red ; }
    </style>
  </head>
  <body>
    <p>  Hello  <b>you</b>  </p>
    <script>
      var a = 1 ;
    </script>
  </body>
</html>

The type of each file comes from its extension: .html and .htm, .css, .js, .mjs and .cjs.

Usage

usage: korpress [-h] [-o OUT] [-i] [-t {html,css,js}] [--copy] [--no-css]
                [--no-js] [--keep-comments] [--strip-license] [-q] [--version]
                [PATH ...]

PATH is one or more files or folders. With none, korpress reads stdin.

Options

-o, --output OUT

Where to write. For one file, OUT is a file name, or a folder if it exists or ends with /. For several files or a folder, OUT is a folder. Missing folders are created. Sizes are printed on stderr.

korpress style.css -o style.min.css
style.css: 98 -> 42 B (-57.1%)

Several files go into a folder:

korpress style.css app.js -o multi/
style.css: 98 -> 42 B (-57.1%)
app.js: 98 -> 64 B (-34.7%)
total: 2 files, 196 -> 106 B (-45.9%)

-i, --in-place

Overwrites each input file with its minified result. It cannot be combined with -o:

korpress page.html --in-place
page.html: 247 -> 148 B (-40.1%)

page.html now holds the minified page. Keep a copy of your sources, or use version control: there is no undo.

korpress: error: --in-place and --output are exclusive

The line above is what you get with korpress style.css -i -o x.css.

-t, --type {html,css,js}

Forces the type, instead of guessing it from the extension. It is required for stdin. Without it, a file with an unknown extension is an error:

korpress style.txt
korpress: error: cannot guess the type of style.txt, use --type
korpress style.txt -t css
body{margin:0;color:#fff}h1{font-size:2em}

--copy

With a folder and -o, also copies the files that are not minified, like images, and the files with .min. in their name. See folders.

--no-css, --no-js, --keep-comments

HTML only. --no-css leaves <style> blocks and style="" attributes alone. --no-js leaves <script> blocks alone. --keep-comments keeps <!-- comments -->.

korpress page.html
<!DOCTYPE html><html><head><title>Hello</title><style>p{color:red}</style></head><body><p>Hello <b>you</b></p><script>var a=1</script></body></html>
korpress page.html --no-css --no-js --keep-comments
<!DOCTYPE html><html><head><!-- page title --><title>Hello</title><style>
      p { color : red ; }
    </style></head><body><p>Hello <b>you</b></p><script>
      var a = 1 ;
    </script></body></html>

--strip-license

CSS and JS only. A comment that starts with /*! is kept by default, since it is often a license. This option drops it too. With lic.css:

/*! MIT License */
/* other */
a { color : red }
korpress lic.css
korpress lic.css --strip-license
/*! MIT License */a{color:red}
a{color:red}

-q, --quiet

Does not print the file sizes. Errors are still printed.

korpress app.js -o app.min.js -q

The command prints nothing, and app.min.js holds:

function greet(name){console.log("Hello, "+name)}greet("world");

--version

korpress --version
korpress 0.1.0

Recursive folders

Give a folder and korpress walks it, in sorted order, at any depth. It needs -o or -i. Take this folder:

site/
  index.html          the page.html above
  css/main.css        the style.css above
  css/lib.min.css     .a { color : red }
  js/app.js           the app.js above
  img/logo.png
  node_modules/x/i.js
  .git/h.js
korpress site/ -o dist/
site/css/main.css: 98 -> 42 B (-57.1%)
site/index.html: 247 -> 148 B (-40.1%)
site/js/app.js: 98 -> 64 B (-34.7%)
total: 3 files, 443 -> 254 B (-42.7%)

dist/ holds only the three minified files, with the same paths. korpress skips:

  • files with .min. in their name, since they are already minified;
  • hidden folders (like .git) and node_modules and __pycache__;
  • files of another type, like logo.png.

Skipped files are not written to dist/. To get them there too, add --copy:

korpress site/ -o dist2/ --copy

The statistics are the same. dist2/ now holds:

dist2/css/lib.min.css
dist2/css/main.css
dist2/img/logo.png
dist2/index.html
dist2/js/app.js

lib.min.css and logo.png are copies, byte for byte. node_modules/ and .git/ are still left out.

To overwrite a folder where it stands, use -i. The skipped files stay untouched:

korpress site/ -i

The same four lines of statistics are printed.

Without -o or -i, a folder or several files are refused:

korpress site/
korpress: error: several files or a directory need --output or --in-place

The output folder cannot be the input folder:

korpress: error: --output cannot be the input directory (use --in-place)

(That is the message for korpress site/ -o site/.)

Stdin and stdout

With no path, korpress reads stdin. You must give -t. The result goes to stdout, or to the file given with -o. No sizes are printed.

cat style.css | korpress -t css
body{margin:0;color:#fff}h1{font-size:2em}
cat app.js | korpress -t js -o fromstdin.js

fromstdin.js holds function greet(name){console.log("Hello, "+name)}greet("world");.

Without -t:

korpress: error: reading stdin needs --type html|css|js

In depth

The details for scripts and build tools.

Exit codes

Code Meaning
0 Everything went fine.
1 At least one file could not be minified.
2 Wrong usage: unknown option, missing file, no -t for stdin, and so on.

Code 1 happens on broken JS. With broken.js:

var s = "abc;
var t = 1
korpress broken.js
korpress: broken.js: unterminated string at line 1, column 9

The exit code is 1. In a folder, the other files still go through, and the exit code is 1 at the end. With a folder bad/ that holds broken.js and style.css:

korpress bad/ -o bad-out/
korpress: bad/broken.js: unterminated string at line 1, column 9
bad/style.css: 98 -> 42 B (-57.1%)

bad-out/ holds style.css and no broken.js, and the exit code is 1. From stdin, the file name is <stdin>:

korpress: <stdin>: unterminated string at line 1, column 9

A missing file is a usage error, so the exit code is 2:

korpress: error: no such file or directory: nope.css

Usage errors (code 2) are also preceded by the usage: lines, left out above. Only JS can fail on its content: CSS and HTML minify what they understand and leave the rest as it is. A file that is not valid UTF-8 is also counted as a failure.