Skip to content

Instantly share code, notes, and snippets.

@Tc-001
Created August 9, 2022 09:50
Show Gist options
  • Select an option

  • Save Tc-001/d348c8493413797352e492f6e41128e6 to your computer and use it in GitHub Desktop.

Select an option

Save Tc-001/d348c8493413797352e492f6e41128e6 to your computer and use it in GitHub Desktop.
Quick and dirty html+css+js collapse/minify
const min = (html:string) => html
.replace(/(\n|\s)*(<[^>]*>)(\n|\s)*/g, "$2") //Collapse whitespace around tags
.replace(/(?<=<style[^>]*>[^<]+)(\{|;|:|\r)(\s)+(?=[^<]+<\/style>)/g, "$1") //Collapse css
.replace(/(?<=<script[^>]*>[^<]+)\s*([\{\}\,\;])\s*(?=[^<]+<\/script>)/g, "$1") //Collapse scripts
.replace(/[^\S\r\n]+/g," ") //Remove spaces
.replace(/\n+/g,"\n") //Remove newlines
/*
Will not remove all that can be removed
Focus was more on making sure nothing can break if you use mostly valid syntax
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment