Code Monkey home page Code Monkey logo

Comments (2)

developit avatar developit commented on June 2, 2024

For now, we can do a simple check both here and here, as well as here where we filter the rules:

+ // print styles are not considered critical. everything else is.
+ isCriticalMedia(media) {
+   return !media || !/(?<!not\s)print\b/i.test(media);
+ }

  async embedLinkedStylesheet (link, compilation, outputPath, publicPath) {
    const href = link.getAttribute('href');
    const media = link.getAttribute('media');
    const document = link.ownerDocument;

+   if (!this.isCriticalMedia(media)) return; // ignore print/etc styles

    // ...etc
  }

  async processStyle (style) {
+   const media = style.getAttribute(media);
+   if (!this.isCriticalMedia(media)) return; // ignore print/etc styles

    // ...etc

    // Walk all CSS rules, marking unused rules with `.$$remove=true` for removal in the second pass.
    // This first pass is also used to collect font and keyframe usage used in the second pass.
    walkStyleRules(ast, markOnly(rule => {
      if (rule.type === 'rule') {
        // ...etc
      }

      // keep font and media rules, they're handled in the second pass:
-     if (rule.type === 'font-face') return;
+     if (rule.type === 'font-face' || rule.type === 'media') return;
  }

    // ...etc

    // Second pass, using data picked up from the first
    walkStyleRulesWithReverseMirror(ast, astInverse, rule => {
      // remove any rules marked in the first pass
      if (rule.$$remove === true) return false;

      applyMarkedSelectors(rule);

      // prune @keyframes rules
      if (rule.type === 'keyframes') {
        if (keyframesMode === 'none') return false;
        if (keyframesMode === 'all') return true;
        return criticalKeyframeNames.indexOf(rule.name) !== -1;
      }

+     // prune @media (print) { ... }
+     if (rule.type === 'media') {
+       return this.isCriticalMedia(rule.media);
+     }

from critters.

7M0506ALO avatar 7M0506ALO commented on June 2, 2024

Nice tanks

from critters.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.