Skip to main content

JavaScript (ES5) minifier

Project description

crimp

A JavaScript minifier command-line utility written fully in Python; uses calmjs.parse as the underlying library.

https://travis-ci.org/calmjs/crimp.svg?branch=1.0.1 https://ci.appveyor.com/api/projects/status/nmtjoh4mavbilgvo/branch/1.0.1?svg=true https://coveralls.io/repos/github/calmjs/crimp/badge.svg?branch=1.0.1

Introduction

crimp serves as the front-end to calmjs.parse.

Both these libraries had their origin in slimit, a package that provided a Python based solution for handling JavaScript code, which is often used for situations where the usage of commonly used minifiers, which are typically written in Node.js, is impractical from a pure Python environment. However, slimit had not been maintained for a number of years. As of 2017, with many issues that impacted the correctness of the generated code remain outstanding, calmjs.parse was forked from slimit, and crimp was created as a front end for the former.

Installation

The following command may be executed to source the latest stable version of crimp wheel from PyPI for installation into the current Python environment.

$ pip install crimp

Usage

As crimp is a package that offers a command of the same name, executing the command after installation with the --help flag will reveal the options that are available.

$ crimp --help
usage: crimp [input_file [input_file ...]] [-h] [-O <output_path>] [-m] [-p]
             [-s [<sourcemap_path>]] [--version] [-o] [--drop-semi]
             [--indent-width n] [--encoding <codec>]

positional arguments:
  input_file            path(s) to input file(s)

optional arguments:
  -h, --help            show this help message and exit
  -O <output_path>, --output-path <output_path>
                        output file path
  -m, --mangle          enable all basic mangling options
  -p, --pretty-print    use pretty printer (omit for minify printer)
  -s [<sourcemap_path>], --source-map [<sourcemap_path>]
                        enable source map; filename defaults to
                        <output_path>.map, if identical to <output_path> it
                        will be written inline as a data url
  --version             show version information
  --indent-width n      indentation width for pretty printer
  --encoding <codec>    the encoding for file-based I/O; stdio relies on
                        system locale

basic mangling options:
  -o, --obfuscate       obfuscate (mangle) names
  --drop-semi           drop unneeded semicolons (minify printer only)

Typically, the program will be invoked with a single or multiple input files (if they are to be combined into a single file), and optionally with the -m flag to denote that it is safe to have all the mangle options enabled.

Please note that all input files must be listed before the flags, as this forced grouping of all input files result in a less ambiguous listing of files, given that there are flags for specifying target output files, which will be overwritten without prompt.

Standard pretty/minified printing

To minify some file:

$ crimp project.js -O project.min.js

To minify some file with just variable name obfuscation:

$ crimp project.js -m -O project.min.js

Pretty printing the minified file back onto stdout:

$ crimp project.min.js -p

Reading input from stdin and writing out to a file. Note that if a SIGINT (typically Ctrl-C or Ctrl-Break), the output file will not be opened for writing.

$ crimp -O demo.js

Source map generation

For source map generation, enable the -s flag.

$ crimp project.js -O project.min.js -s

The above will write out the source map file as project.min.js.map, and the reference to that (the sourceMappingURL) will also be appended to the output file as a comment. To specify a specific location, pass the name as a parameter.

$ crimp project.js -O project.min.js -s project.min.map

Inline source maps (where the sourceMappingURL is the data URL of the base64 encoding of the JSON serialization of the source map) are supported; these can be produced by supplying the argument with the same name used for the output file, like so:

$ crimp project.js -O project.min.js -s project.min.js

Troubleshooting

Parsing a moderately sized file takes 10x as much time as uglifyjs

This is due to the implementation done by calmjs.parse as a set of generator functions that produce very minimum output, and that the standard Python implementation has a very high overhead performance cost for function calls. The advantage with that approach is that maximum flexibility can be achieved (due to the ease of which unparsing workflows can be set up), while the drawback is obvious.

Contribute

Changelog

1.0.1 - 2018-08-11

  • Bumping minimum version of calmjs.parse to 1.1.1, which includes fixes to:

    • Line continuation in JavaScript sources should no longer break source map line counting. [ calmjs.parse#16 ]

    • Will no longer parse certain incorrectly unterminated statements as valid JavaScript. [ calmjs.parse#18 ]

    • Obfuscated minifier should no longer truncate certain closing braces as the internal accounting is corrected. [ calmjs.parse#19 ]

1.0.0 - 2017-09-26

Initial release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

crimp-1.0.1.zip (17.9 kB view details)

Uploaded Source

Built Distribution

crimp-1.0.1-py2.py3-none-any.whl (11.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file crimp-1.0.1.zip.

File metadata

  • Download URL: crimp-1.0.1.zip
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.5

File hashes

Hashes for crimp-1.0.1.zip
Algorithm Hash digest
SHA256 7cc287d19c5ad914b59ab8959915f473235d35b3f6c036ceddbef052f4ea7caf
MD5 c70c9d83ea95265b263d5abfc044c162
BLAKE2b-256 ef1b7070821d5ef0c14ccc63c0261f5477587506f3c5b3ce6fa4d395ad3d3703

See more details on using hashes here.

File details

Details for the file crimp-1.0.1-py2.py3-none-any.whl.

File metadata

  • Download URL: crimp-1.0.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.5

File hashes

Hashes for crimp-1.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a38b6e1702287fbc5eca8923e486f4198e5b217d28405022792582ea6d431bab
MD5 7a866a95142c9f7d8fc53aa741f957b9
BLAKE2b-256 fcfcaea5cb48108aef471a6ce32665e6ea9f3d5339632c458147aac44aae3751

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page