How to Minify JavaScript and CSS Without Uploading Your Source
Minifying a script is a text rewrite. Gzipping it is a transfer encoding. Shrinking a photograph is neither. The code page on this site does the first job, on one snippet, with esbuild compiled to WebAssembly. It also lowers the syntax to ES2015, which means an async function can come out longer than it went in. The output below is from that version of esbuild, with the page's settings, not from a prettier example.
Minify is not the same job as compress
Minification rewrites source so the program means the same thing in fewer characters. Compression, in the HTTP sense, is Gzip or Brotli applied to whatever bytes you are already sending. You want both, and they stack. A JPEG has nothing to do with either one. The code minifier on this site runs esbuild's transform on a snippet you paste. It does not bundle a project, it does not know about your other files, and it does not talk to an image.
The engine is esbuild-wasm 0.20.1, loaded from esm.sh, initialized with its WASM binary from that same host. The call is transform with minify: true and target: 'es2015', and the loader is either js or css depending on the toggle. minify: true turns on whitespace, identifier, and syntax minification. The target is the part people miss. The output is lowered to ES2015. Modern syntax is accepted as input and is not preserved as itself.
What a real run changed
This is the output of that same call, not a hand-edited sketch. Input:
// Apply the discount if the cart is eligible.
function applyDiscount(cartTotal, member) {
const minimumSpend = 50;
if (member === true && cartTotal >= minimumSpend) {
return cartTotal * 0.9;
}
return cartTotal;
}
const apiKey = "sk_live_example_not_a_real_key";
console.log(applyDiscount(80, true), apiKey);
Output:
function applyDiscount(e,n){return n===!0&&e>=50?e*.9:e}const apiKey="sk_live_example_not_a_real_key";console.log(applyDiscount(80,!0),apiKey);
The comment is gone. cartTotal and member became e and n because they are locals. true became !0. The constant minimumSpend was folded into 50. The if/return became a conditional expression. 0.9 became .9.
apiKey did not get a short name. It is a top-level binding, and renaming it would change the meaning of the snippet if something else expected that name. The string itself is untouched. A minifier is not a secret scanner. If the string had been a real key, it would be in the output in full. Search for keys before you paste, and do not treat the minified file as a place to hide one. The same warning sits on the tool page because it is the failure that matters.
The target rewrites newer syntax
Pasting this:
const label = user?.name ?? "guest";
async function load() {
const value = await fetch("/data");
return value;
}
produces a helper that reimplements async as a generator-style state machine, and it lowers ?. and ?? into comparisons. I ran esbuild 0.20.1, the version the page loads, with the page's options (minify: true, target: 'es2015'). The optional chain and the default come out like this, minified:
var s;const label=(s=user==null?void 0:user.name)!=null?s:"guest";
The async function is longer than the source, because the helper that replaces await is bigger than the word await. On a large file the helper is shared and the minify still wins. On a three-line snippet, "minify" can make the async function bigger. That is not a bug in your code. It is the target. Current browsers understand async and optional chaining. If you are minifying for those browsers and you want the smallest output, a project build with a newer target is the right tool. This page is a single-snippet minifier aimed at ES2015, which is a conservative choice and a verbose one for async.
The tool does not emit a source map. You cannot reconstruct cartTotal from e after the fact. Keep the original. Pretty-printing the output restores line breaks and nothing else. The FAQ on the tool page says the same thing.
CSS is a lighter pass
The CSS loader, same minify flag, same target, turned this:
/* header block */
.hero {
color: #ffffff;
margin: 0px;
padding: 16px 16px 16px 16px;
background-color: #ffffff;
}
.hero {
font-weight: bold;
}
into:
.hero{color:#fff;margin:0;padding:16px;background-color:#fff}.hero{font-weight:700}
The comment is gone. #ffffff became #fff. 0px became 0. The four identical padding values became one. bold became 700. The two .hero rules were not merged. I am mentioning that because it is easy to assume a minifier combines duplicate selectors. This one, at these settings, did not. Custom properties, var(--primary), are left alone. They are part of the meaning. Do not expect the tool to inline them.
What it will not fix, and what it can break
- It does not tree-shake across files. Paste one snippet. An unused function in that snippet can be dropped when esbuild can prove it is unused. An unused export that some other file needed is not esbuild's problem here, because the other file is not in the input. Do not paste a single module and assume the rest of the app was considered.
- It does not understand strings you eval. If you
evala local variable's name, or build a property access from a shortened name, mangling changes the behavior. Ordinary code does not do this. Code that does should not go through a minifier that renames locals, or you should keep the names. - It will not make a slow algorithm fast. INP problems from a huge script are partly parse and compile cost, and a smaller file helps that. They are mostly the work the script does. Minifying a 200 KB library that walks the whole DOM on every click does not fix the click.
- It is not an obfuscator worth the name. The logic is still readable if someone cares. Short names slow a person down. They do not hide an API endpoint that is sitting in a string.
Gzip and Brotli still belong on the server. Minified source has less repetition for those algorithms to chew on than a pretty-printed file does, and it is still worth compressing. A server that serves the minified file without compression is leaving a second, larger win on the table. Configure that on the host. This page cannot set your response headers.
Using the page
- Choose JavaScript or CSS. The loader has to match. CSS pasted into the JavaScript mode will fail the parse, and the page will show the esbuild error.
- Paste. The byte count above the box is the UTF-8 length of what you pasted, which is the right unit for "how much source."
- Click Minify Code. The first click in a session waits for the WASM binary. After that it is local. The status line says whether the engine is ready.
- Read the savings percentage as a comparison of those two strings. It is not a prediction of the Gzipped transfer size.
- Copy or download
simplycompress.min.jsorsimplycompress.min.css. Keep the unminified file in your project. Do not edit the minified one and paste it back as the new source. - If the snippet contained
asyncand the output grew, you are looking at the ES2015 helper. Ship a build with a current target if that snippet is representative of the app.
The source you paste is not uploaded by this application. The WASM binary is. That split, and the reason a different website's minifier would be an upload, is in What Leaves Your Browser and Client-Side vs Cloud File Compressors.
Images are still the usual LCP problem. A minified stylesheet helps the browser start painting sooner. It does not shrink the hero. Size and encode the hero using Compressing Images for Core Web Vitals, and minify the CSS that describes the slot.
What not to paste
Do not paste node_modules. Those files are already built, often already minified, and they are not your source. Running them through a second minify with an ES2015 target can rewrite a bundle that was intentionally left on a newer syntax, and it can break a file that expected its own wrapper. Minify the code you wrote. Let the project bundler minify the dependencies, with a target you chose on purpose.
Comments disappear, including license headers. If the only copy of an MIT or BSD notice is a comment at the top of the file, the minified output will not have it. Keep the notice in a file you do not minify, or put it back after. The tool will not warn you. The same is true of a comment that says "test password, remove before release": the comment goes, and the password string, if it was code, stays. Read the output once when the input contained anything you would not publish.
A whole application pasted into one box is also the wrong shape. esbuild's transform will parse it if it is valid as a single script or a single stylesheet. It will not resolve import paths, it will not read the files those imports point at, and it will not tree-shake a package. Use it for a snippet you are about to inline, a stylesheet you are about to paste into a CMS, or a check of what minify does to a function. Use a real build when the code has modules.
The savings number is honest about the two strings and easy to overread. A drop from 4 KB to 3 KB is a real 25 percent and an irrelevant amount next to a 400 KB photograph. Spend the time on the photograph. Spend the minify pass on the stylesheet that is render-blocking and on the script that runs on every page. Ignore it for a file that is requested once, cached, and already small.
Frequently Asked Questions
Will minifying change how my code runs?
Ordinary code keeps its behavior. eval of local names, or property access built from those names, can break because locals are renamed. Top-level names in a snippet are not all shortened. Strings are not rewritten.
Why did my async function get bigger?
The page targets ES2015, so await is rewritten into a helper. On a tiny snippet that helper costs more than it saves. A project build aimed at current browsers can use a newer target and stay smaller.
Does minification remove API keys?
No. A string is copied through because the program needs it. Short variable names are not a hiding place. Take secrets out before you minify, and do not commit them in either file.
Is the minified file enough, or do I still want Gzip or Brotli?
You still want them. Minification and HTTP compression do different jobs and stack. The server has to be configured to compress the response. This page cannot set that header.
Minify a snippet in the tab
Paste JavaScript or CSS. The first run loads the WASM engine. The source is not posted to a server by this app.
Open the Code Minifier