Skip to content

✨ Minifies and obfuscates the class names in HTML #169

Closed

Description

@1aron

Description

The HTML generated by Master CSS SSR:

import { renderHTML } from '@master/css'
...
const html = renderHTML(html);
res.set('Content-Type', 'text/html');
res.send(Buffer.from(html));
<html>
<head>
    <style title="master">
        .text\:center { text-align: center }
        .font\:16\:hover:hover { font-size: 1rem }
    </style>
</head>
<body>
    <h1 class="text:center font:16:hover"></h1>
</body>
</html>

Now with minifying:

import { renderHTML, minify } from '@master/css'
...
const html = minify(renderHTML(html));
res.set('Content-Type', 'text/html');
res.send(Buffer.from(html));
<html>
<head>
    <style title="master">
        .a { text-align: center }
        .b:hover { font-size: 1rem }
    </style>
</head>
<body>
    <h1 class="a b"></h1>
</body>
</html>

Activity

  1. added this to the Master CSS 2.X milestone on Feb 15, 2023
  2. itsezc commented on Feb 23, 2023

    @itsezc
    Member

    Would it not be possible to merge class a and b to reduce the overall output CSS and classes used? i.e the CSS could look like:

    .c { text-align: center }
    .c:hover { font-size: 1rem }

    now c could be used by multiple elements, and it would just be the 1 class

  3. 1aron commented on Feb 24, 2023

    @1aron
    MemberAuthor

    Would it not be possible to merge class a and b to reduce the overall output CSS and classes used? i.e the CSS could look like:

    .c { text-align: center }
    .c:hover { font-size: 1rem }

    now c could be used by multiple elements, and it would just be the 1 class

    Great start! It may be possible, but more complete test cases are needed to confirm whether it will conflict with the current API.

  4. added
    undocumentedNot yet written into official documentation
    on May 7, 2023
  5. pragyamishra56 commented on Oct 6, 2023

    @pragyamishra56

    @ If it would be possible then can you assign me to this issue I want to resolve this would be my first contribution

  6. 1aron commented on Oct 6, 2023

    @1aron
    MemberAuthor

    @ If it would be possible then can you assign me to this issue I want to resolve this would be my first contribution

    Welcome! It's recommended to refer https://github.com/master-co/css/blob/beta/packages/css/src/functions/render-html.ts

  7. pragyamishra56 commented on Oct 6, 2023

    @pragyamishra56

    @1aron Thank you so much, Sir for assigning me this issue

  8. pragyamishra56 commented on Oct 6, 2023

    @pragyamishra56

    @1aron Sir Is it okay if I request more information, or is there anything else you would like assistance with? I'm here to resolve this issue?

    import { MasterCSS } from '../core';
    import { Config, renderHTML, minify } from '@master/css';  // Import renderHTML and minify
    
    import generateFromHTML from './generate-from-html';
    
    /**
     * Renders the page-required and sorted CSS text from HTML and injected it back into HTML
     * @param html 
     * @param config 
     * @returns html text
     */
    export default function renderHTMLWithMinification(html: string, config?: Config): string {
        if (!html) return;
    
        let replaced = false;
        html = html.replace(
            /(<style id="master">).*?(<\/style>)/,
            (_, prefix, suffix) => {
                replaced = true;
                return prefix + generateFromHTML(html, config) + suffix;
            }
        );
    
        if (replaced) {
            return minify(html);  // Minify the HTML if styles were replaced
        }
    
        const styleText = `<style id="master">${generateFromHTML(html, config)}</style>`;
        const minifiedHtml = minify(html.replace(/<\/head>/, `${styleText}$&`));
    
        return minifiedHtml;
    }[](URL)
  9. pragyamishra56 commented on Oct 6, 2023

    @pragyamishra56

    @1aron Sir Is this your main website?
    Screenshot 2023-10-06 190829

  10. 1aron commented on Oct 6, 2023

    @1aron
    MemberAuthor

    @pragyamishra56 I think you misunderstood. minify is the function to be implemented in the issue.

  11. pragyamishra56 commented on Oct 7, 2023

    @pragyamishra56

    @1aron Sir, could you please assist me? You mentioned that you want improvement in the section I referred to, correct?"

    import { renderHTML } from '@master/css'
    ...
    const html = renderHTML(html);
    res.set('Content-Type', 'text/html');
    res.send(Buffer.from(html));

    <style title="master"> .text\:center { text-align: center } .font\:16\:hover:hover { font-size: 1rem } </style>

    Now with minifying:

    import { renderHTML, minify } from '@master/css'
    ...
    const html = minify(renderHTML(html));
    res.set('Content-Type', 'text/html');
    res.send(Buffer.from(html));

    <style title="master"> .a { text-align: center } .b:hover { font-size: 1rem } </style>

  12. 1aron commented on Oct 11, 2023

    @1aron
    MemberAuthor

    @1aron Sir, could you please assist me? You mentioned that you want improvement in the section I referred to, correct?"

    import { renderHTML } from '@master/css' ... const html = renderHTML(html); res.set('Content-Type', 'text/html'); res.send(Buffer.from(html));

    <style title="master"> .text\:center { text-align: center } .font\:16\:hover:hover { font-size: 1rem } </style>

    Now with minifying:

    import { renderHTML, minify } from '@master/css' ... const html = minify(renderHTML(html)); res.set('Content-Type', 'text/html'); res.send(Buffer.from(html));

    <style title="master"> .a { text-align: center } .b:hover { font-size: 1rem } </style>

    Again, there is currently no minify function.

  13. added theissue type on Mar 24, 2025
  14. removed this from the 2.X milestone on Apr 11, 2025
  15. locked and limited conversation to collaborators on Apr 11, 2025
  16. converted this issue into a discussion #395 on Apr 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    undocumentedNot yet written into official documentation

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions