All files / minimalcss/src run.js

54.91% Statements 95/173
48.51% Branches 49/101
33.33% Functions 9/27
55.23% Lines 95/172
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479    1x   1x   1x 1x 1x 1x                                       1x                                                                                                                                                         1x                     18x         18x   18x 18x 18x       18x 18x 18x   18x 18x       18x         18x                   18x 18x 79x 79x     79x       1x 78x   17x 61x           2x 2x   59x         18x 57x   57x 57x 1x 56x       4x       4x 52x       17x 17x 17x 489x 7x 7x 7x 2x     7x 7x   4x     4x 4x 2x   2x     7x 2x   5x       17x 17x         18x 1x         18x   18x 18x 17x     17x 17x 17x                                 17x     16x         16x                                                                             16x                 1x 18x 18x       18x       18x 18x 18x 18x 18x 18x   18x   18x 18x 18x 18x 18x                       18x   18x       18x     18x                                                                                                                                                                                                                       1x  
'use strict'
 
const puppeteer = require('puppeteer')
// @ts-ignore
const csso = require('csso')
// @ts-ignore
const csstree = require('css-tree')
const cheerio = require('cheerio')
const utils = require('./utils')
const url = require('url')
 
/**
 * Take in a csstree AST, mutate it and return a csstree AST.
 * The mutation is about:
 *
 *   1) Remove all keyframe declarations that are *not* mentioned
 *      by animation name.
 *   2) Remove all font-face declarations that are *not* mentioned
 *      as the font-family.
 *
 * The gist of the function is that it walks the AST, populates sets
 * that track the names of all animations and font families. Then,
 * it converts the AST to a plain object, which it mutates by filtering
 * the 'children' object.
 * Lastly it uses the csstree's fromPlainObject to return the plain
 * object back as an AST.
 * @param {Object} ast
 * @return Object
 */
const postProcessOptimize = ast => {
  // First walk the AST to know which animations are ever mentioned
  // by the remaining rules.
  const activeAnimationNames = new Set(
    csstree.lexer
      .findAllFragments(ast, 'Type', 'keyframes-name')
      .map(entry => csstree.generate(entry.nodes.first()))
  )
 
  // This is the function we use to filter @keyframes atrules out,
  // if its name is not actively used.
  // It also filters out all `@media print` atrules.
  csstree.walk(ast, {
    visit: 'Atrule',
    enter: (node, item, list) => {
      const basename = csstree.keyword(node.name).basename
      if (basename === 'keyframes') {
        if (!activeAnimationNames.has(csstree.generate(node.prelude))) {
          list.remove(item)
        }
      } else if (basename === 'media') {
        if (csstree.generate(node.prelude) === 'print') {
          list.remove(item)
        }
      }
    }
  })
 
  // Now figure out what font-families are at all used in the AST.
  const activeFontFamilyNames = new Set()
  csstree.walk(ast, {
    visit: 'Declaration',
    enter: function(node) {
      // walker pass through `font-family` declarations inside @font-face too
      // this condition filter them, to walk through declarations
      // inside a rules only.
      if (this.rule) {
        csstree.lexer
          .findDeclarationValueFragments(node, 'Type', 'family-name')
          .forEach(entry => {
            const name = utils.unquoteString(
              csstree.generate({
                type: 'Value',
                children: entry.nodes
              })
            )
            activeFontFamilyNames.add(name)
          })
      }
    }
  })
 
  // Walk into every font-family rule and inspect if we uses its declarations
  csstree.walk(ast, {
    visit: 'Atrule',
    enter: (atrule, atruleItem, atruleList) => {
      if (csstree.keyword(atrule.name).basename === 'font-face') {
        // We're inside a font-face rule! Let's dig deeper.
        csstree.walk(atrule, {
          visit: 'Declaration',
          enter: declaration => {
            if (csstree.property(declaration.property).name === 'font-family') {
              const name = utils.unquoteString(
                csstree.generate(declaration.value)
              )
              // was this @font-face used?
              if (!activeFontFamilyNames.has(name)) {
                atruleList.remove(atruleItem)
              }
            }
          }
        })
      }
    }
  })
}
 
const processPage = ({
  page,
  options,
  pageUrl,
  stylesheetAsts,
  stylesheetContents,
  doms,
  allHrefs,
  redirectResponses,
  skippedUrls
}) =>
  new Promise(async (resolve, reject) => {
    // If anything goes wrong, for example a `pageerror` event or
    // a bad download request (e.g. !response.ok), then remember that
    // we have fulfilled the promise and don't want to call `reject` or `resolve`
    // a second time.
    let fulfilledPromise = false
 
    const safeReject = error => {
      Eif (!fulfilledPromise) {
        reject(error)
      }
    }
 
    const debug = options.debug || false
    const loadimages = options.loadimages || false
    const withoutjavascript = options.withoutjavascript || false
 
    try {
      Iif (options.userAgent) {
        await page.setUserAgent(options.userAgent)
      }
 
      Iif (options.viewport) {
        await page.setViewport(options.viewport)
      }
 
      // A must or else you can't do console.log from within page.evaluate()
      page.on('console', msg => {
        if (debug) {
          // console.log(...(msg.args))
          // console.log(msg.args)
          for (let i = 0; i < msg.args.length; ++i) {
            console.log(`${i}: ${msg.args[i]}`)
          }
        }
      })
 
      await page.setRequestInterception(true)
      page.on('request', request => {
        const requestUrl = request.url()
        Iif (/data:image\//.test(requestUrl)) {
          // don't need to download those
          request.abort()
        } else if (
          !loadimages &&
          /\.(png|jpg|jpeg|gif|webp)$/.test(requestUrl.split('?')[0])
        ) {
          request.abort()
        } else if (stylesheetAsts[requestUrl]) {
          // no point downloading this again
          request.abort()
        } else if (options.skippable && options.skippable(request)) {
          // If the URL of the request that got skipped is a CSS file
          // not having it in stylesheetAsts is going to cause a
          // problem later when we loop through all <link ref="stylesheet">
          // tags.
          // So put in an empty (but not falsy!) object for this URL.
          skippedUrls.add(requestUrl)
          request.abort()
        } else {
          request.continue()
        }
      })
 
      // To build up a map of all downloaded CSS
      page.on('response', response => {
        const responseUrl = response.url()
 
        const ct = response.headers()['content-type'] || ''
        if (response.status() >= 400) {
          return safeReject(new Error(`${response.status()} on ${responseUrl}`))
        } else if (response.status() >= 300) {
          // If the 'Location' header points to a relative URL,
          // convert it to an absolute URL.
          // If it already was an absolute URL, it stays like that.
          const redirectsTo = new URL(
            response.headers().location,
            responseUrl
          ).toString()
          redirectResponses[responseUrl] = redirectsTo
        } else if (
          ct.indexOf('text/css') > -1 ||
          /\.css$/i.test(responseUrl.split('?')[0])
        ) {
          response.text().then(text => {
            const ast = csstree.parse(text)
            csstree.walk(ast, node => {
              if (node.type === 'Url') {
                let value = node.value
                let path = value.value
                if (value.type !== 'Raw') {
                  path = path.substr(1, path.length - 2)
                }
                const sameHost =
                  url.parse(responseUrl).host === url.parse(pageUrl).host
                if (/^https?:\/\/|^\/\//i.test(path)) {
                  // do nothing
                } else Iif (/^\//.test(path) && sameHost) {
                  // do nothing
                } else {
                  const resolved = new url.URL(path, responseUrl)
                  if (sameHost) {
                    path = resolved.pathname
                  } else {
                    path = resolved.href
                  }
                }
                if (value.type !== 'Raw') {
                  value.value = `"${path}"`
                } else {
                  value.value = path
                }
              }
            })
            stylesheetAsts[responseUrl] = ast
            stylesheetContents[responseUrl] = text
          })
        }
      })
 
      page.on('pageerror', error => {
        safeReject(error)
      })
 
      let response
 
      Eif (!withoutjavascript) {
        // First, go to the page with JavaScript disabled.
        await page.setJavaScriptEnabled(false)
        response = await page.goto(pageUrl)
        Iif (!response.ok()) {
          return safeReject(new Error(`${response.status()} on ${pageUrl}`))
        }
        const htmlVanilla = await page.content()
        doms.push(cheerio.load(htmlVanilla))
        await page.setJavaScriptEnabled(true)
      }
 
      // XXX There is another use case *between* the pure DOM (without any
      // javascript) and after the network is idle.
      // For example, any CSSOM that is *made by javascript* but goes away
      // after all XHR is finished loading.
      // What we can do is do that lookup here using...
      //    response = await page.goto(pageUrl, {
      //        waitUntil: ['domcontentloaded']
      //    })
      // And add that to the list of DOMs.
      // This will slow down the whole processing marginally.
 
      // Second, goto the page and evaluate it on the 'networkidle2' event.
      // This gives the page a chance to load any <script defer src="...">
      // and even some JS that does XHR requests right after load.
      response = await page.goto(pageUrl, {
        waitUntil: ['domcontentloaded', 'networkidle0']
      })
      Iif (!response.ok()) {
        return safeReject(
          new Error(`${response.status()} on ${pageUrl} (second time)`)
        )
      }
      const evalNetworkIdle = await page.evaluate(() => {
        // The reason for NOT using a Set here is that that might not be
        // supported in ES5.
        const hrefs = []
        // Loop over all the 'link' elements in the document and
        // for each, collect the URL of all the ones we're going to assess.
        Array.from(document.querySelectorAll('link')).forEach(link => {
          if (
            link.href &&
            (link.rel === 'stylesheet' ||
              link.href.toLowerCase().endsWith('.css')) &&
            !link.href.toLowerCase().startsWith('blob:') &&
            link.media !== 'print'
          ) {
            // if (!stylesheetAsts[link.href]) {
            //   throw new Error(`${link.href} not in stylesheetAsts!`)
            // }
            // if (!Object.keys(stylesheetAsts[link.href]).length) {
            //   // If the 'stylesheetAsts[link.href]' thing is an
            //   // empty object, simply skip this link.
            //   return
            // }
            hrefs.push(link.href)
          }
        })
        return {
          html: document.documentElement.outerHTML,
          hrefs
        }
      })
 
      const htmlNetworkIdle = evalNetworkIdle.html
      doms.push(cheerio.load(htmlNetworkIdle))
      evalNetworkIdle.hrefs.forEach(href => {
        allHrefs.add(href)
      })
 
      if (!fulfilledPromise) resolve()
    } catch (e) {
      return safeReject(e)
    }
  })
 
/**
 *
 * @param {{ urls: Array<string>, debug: boolean, loadimages: boolean, skippable: function, browser: any, userAgent: string, withoutjavascript: boolean, viewport: any }} options
 * @return Promise<{ finalCss: string, stylesheetContents: { [key: string]: string } }>
 */
const minimalcss = async options => {
  const { urls } = options
  const debug = options.debug || false
  // const keepPrintAtRules = options.keepPrintAtRules || false
  // XXX The launch options should be a parameter once this is no longer
  // just a cli app.
  const browser = options.browser || (await puppeteer.launch({}))
 
  // All of these get mutated by the processPage() function. Once
  // per URL.
  const stylesheetAsts = {}
  const stylesheetContents = {}
  const doms = []
  const allHrefs = new Set()
  const redirectResponses = {}
  const skippedUrls = new Set()
 
  try {
    // Note! This opens one URL at a time synchronous
    for (let i = 0; i < urls.length; i++) {
      const pageUrl = urls[i]
      const page = await browser.newPage()
      try {
        await processPage({
          page,
          options,
          pageUrl,
          stylesheetAsts,
          stylesheetContents,
          doms,
          allHrefs,
          redirectResponses,
          skippedUrls
        })
      } catch (e) {
        throw e
      } finally {
        await page.close()
      }
    }
  } catch (e) {
    throw e
  } finally {
    // We can close the browser now that all URLs have been opened.
    Iif (!options.browser) {
      browser.close()
    }
  }
 
  // All URLs have been opened, and we now have multiple DOM objects.
 
  // Now, let's loop over ALL links and process their ASTs compared to
  // the DOMs.
  const decisionsCache = {}
  const isSelectorMatchToAnyElement = selectorString => {
    // Here's the crucial part. Decide whether to keep the selector
    // Find at least 1 DOM that contains an object that matches
    // this selector string.
    return doms.some(dom => {
      try {
        return dom(selectorString).length > 0
      } catch (ex) {
        // Be conservative. If we can't understand the selector,
        // best to leave it in.
        if (debug) {
          console.warn(selectorString, ex.toString())
        }
        return true
      }
    })
  }
  allHrefs.forEach(href => {
    while (redirectResponses[href]) {
      href = redirectResponses[href]
    }
    if (skippedUrls.has(href)) {
      return
    }
    const ast = stylesheetAsts[href]
 
    csstree.walk(ast, {
      visit: 'Rule',
      enter: function(node, item, list) {
        if (
          this.atrule &&
          csstree.keyword(this.atrule.name).basename === 'keyframes'
        ) {
          // Don't bother inspecting rules that are inside a keyframe.
          return
        }
 
        node.prelude.children.forEach((node, item, list) => {
          // Translate selector's AST to a string and filter pseudos from it
          // This changes things like `a.button:active` to `a.button`
          const selectorString = utils.reduceCSSSelector(csstree.generate(node))
          if (selectorString in decisionsCache === false) {
            decisionsCache[selectorString] = isSelectorMatchToAnyElement(
              selectorString
            )
          }
          if (!decisionsCache[selectorString]) {
            // delete selector from a list of selectors
            list.remove(item)
          }
        })
 
        if (node.prelude.children.isEmpty()) {
          // delete rule from a list
          list.remove(item)
        }
      }
    })
  })
  // Every unique URL in every <link> tag has been checked.
  const allCombinedAst = {
    type: 'StyleSheet',
    loc: null,
    children: Object.keys(stylesheetAsts).reduce(
      (children, href) => children.appendList(stylesheetAsts[href].children),
      new csstree.List()
    )
  }
 
  // Lift important comments (i.e. /*! comment */) up to the beginning
  const comments = new csstree.List()
  csstree.walk(allCombinedAst, {
    visit: 'Comment',
    enter: (_node, item, list) => {
      comments.append(list.remove(item))
    }
  })
  allCombinedAst.children.prependList(comments)
 
  // Why not just allow the return of the "unminified" CSS (in case
  // some odd ball wants it)?
  // 'allCombinedAst' concatenates multiple payloads of CSS.
  // It only contains the selectors that are supposedly
  // in the DOM. However it does contain *duplicate* selectors.
  // E.g. `p { color: blue; } p { font-weight: bold; }`
  // When ultimately, what was need is `p { color: blue; font-weight: bold}`.
  // The csso.minify() function will solve this, *and* whitespace minify
  // it too.
  csso.compress(allCombinedAst)
  postProcessOptimize(allCombinedAst)
 
  const returned = {
    finalCss: csstree.generate(allCombinedAst),
    stylesheetContents
  }
  return Promise.resolve(returned)
}
 
module.exports = { run: minimalcss }