Code Minifier Tool
Characters: 0
Savings: 0%
Minified code is optimized for production use. Comments and unnecessary whitespace have been removed.
⚡
Fast Processing
Minify your code in milliseconds with our optimized algorithms
📦
Size Reduction
Reduce file size by up to 60% for faster loading times
🔒
Secure & Private
Your code is processed locally and never sent to any server
📱
Responsive Design
Works perfectly on desktop, tablet, and mobile devices
`,
css: `/* This is a sample CSS file */
body {
font-family: 'Arial', sans-serif;
margin: 0;
padding: 0;
background-color: #f5f5f5;
color: #333;
}.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}.header {
background: linear-gradient(to right, #4a6ee0, #6a82fb);
color: white;
padding: 30px 0;
text-align: center;
margin-bottom: 30px;
}.button {
display: inline-block;
padding: 10px 20px;
background-color: #4a6ee0;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s;
}.button:hover {
background-color: #3a5ac8;
}/* Media query for responsiveness */
@media (max-width: 768px) {
.container {
padding: 10px;
}
.header {
padding: 20px 0;
}
}`,
js: `// This is a sample JavaScript file// Function to calculate factorial
function factorial(n) {
if (n === 0 || n === 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}// Function to validate email
function validateEmail(email) {
const regex = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/;
return regex.test(email);
}// Example class
class Calculator {
constructor() {
this.result = 0;
}
add(value) {
this.result += value;
return this;
}
subtract(value) {
this.result -= value;
return this;
}
getResult() {
return this.result;
}
}// Usage example
const calc = new Calculator();
const result = calc.add(5).subtract(2).getResult();
console.log('Result:', result);// Event listener example
document.addEventListener('DOMContentLoaded', function() {
console.log('Document ready');
});`
};
// Initialize the app
function init() {
// Set up language buttons
langButtons.forEach(btn => {
btn.addEventListener('click', () => {
langButtons.forEach(b => b.classList.remove('active'));
btn.classList.add('active');
currentLang = btn.getAttribute('data-lang');
updateUI();
});
});
// Update character and line counts
inputCode.addEventListener('input', updateStats);
// Set up action buttons
minifyBtn.addEventListener('click', minifyCode);
clearBtn.addEventListener('click', clearCode);
exampleBtn.addEventListener('click', loadExample);
copyBtn.addEventListener('click', copyToClipboard);
downloadBtn.addEventListener('click', downloadCode);
// Load HTML example by default
loadExample();
}
// Update UI based on current language
function updateUI() {
inputCode.placeholder = `Paste your ${currentLang.toUpperCase()} code here...`;
compressionInfo.textContent = getCompressionInfo();
}
// Get compression info based on language
function getCompressionInfo() {
const info = {
html: "Minified HTML removes comments, extra whitespace, and optional tags.",
css: "Minified CSS removes comments, unnecessary whitespace, and optimizes values.",
js: "Minified JavaScript removes comments, whitespace, and shortens variable names where possible."
};
return info[currentLang];
}
// Update character and line counts
function updateStats() {
const text = inputCode.value;
const chars = text.length;
const lines = text.split('\n').length;
charCount.textContent = `Characters: ${chars}`;
lineCount.textContent = `Lines: ${lines}`;
}
// Minify code based on current language
function minifyCode() {
const input = inputCode.value.trim();
if (!input) {
alert('Please enter some code to minify.');
return;
}
let output;
let originalSize = input.length;
switch(currentLang) {
case 'html':
output = minifyHTML(input);
break;
case 'css':
output = minifyCSS(input);
break;
case 'js':
output = minifyJS(input);
break;
default:
output = input;
}
outputCode.textContent = output;
const minifiedSize = output.length;
const savingsPercent = originalSize > 0 ?
Math.round(((originalSize - minifiedSize) / originalSize) * 100) : 0;
outputCharCount.textContent = `Characters: ${minifiedSize}`;
savings.textContent = `Savings: ${savingsPercent}%`;
}
// Minify HTML
function minifyHTML(html) {
return html
// Remove comments
.replace(//g, '')
// Remove whitespace between tags
.replace(/>\s+<')
// Collapse multiple spaces to one
.replace(/\s+/g, ' ')
// Trim
.trim();
}
// Minify CSS
function minifyCSS(css) {
return css
// Remove comments
.replace(/\/\*[\s\S]*?\*\//g, '')
// Remove whitespace around braces and colons
.replace(/\s*{\s*/g, '{')
.replace(/\s*}\s*/g, '}')
.replace(/\s*:\s*/g, ':')
.replace(/\s*;\s*/g, ';')
// Remove trailing semicolons
.replace(/;}/g, '}')
// Collapse multiple spaces to one
.replace(/\s+/g, ' ')
// Trim
.trim();
}
// Minify JavaScript (basic minification)
function minifyJS(js) {
return js
// Remove single-line comments
.replace(/\/\/.*$/gm, '')
// Remove multi-line comments
.replace(/\/\*[\s\S]*?\*\//g, '')
// Remove whitespace around operators
.replace(/\s*([=+\-*\/<>!]={0,1})\s*/g, '$1')
// Remove unnecessary semicolons
.replace(/;\s*;/g, ';')
// Collapse multiple spaces to one
.replace(/\s+/g, ' ')
// Remove whitespace around parentheses and braces
.replace(/\s*\(\s*/g, '(')
.replace(/\s*\)\s*/g, ')')
.replace(/\s*{\s*/g, '{')
.replace(/\s*}\s*/g, '}')
// Trim
.trim();
}
// Clear input and output
function clearCode() {
inputCode.value = '';
outputCode.textContent = '';
updateStats();
outputCharCount.textContent = 'Characters: 0';
savings.textContent = 'Savings: 0%';
}
// Load example code
function loadExample() {
inputCode.value = examples[currentLang];
updateStats();
}
// Copy output to clipboard
function copyToClipboard() {
const output = outputCode.textContent;
if (!output) {
alert('No minified code to copy.');
return;
}
navigator.clipboard.writeText(output)
.then(() => {
// Visual feedback
const originalText = copyBtn.textContent;
copyBtn.textContent = 'Copied!';
setTimeout(() => {
copyBtn.textContent = originalText;
}, 2000);
})
.catch(err => {
console.error('Failed to copy: ', err);
alert('Failed to copy to clipboard. Please try again.');
});
}
// Download minified code
function downloadCode() {
const output = outputCode.textContent;
if (!output) {
alert('No minified code to download.');
return;
}
const blob = new Blob([output], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `minified.${currentLang}`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
// Initialize the application
init();