text: /**
   * Copyright (c) 2020 Google Inc
   *
   * Permission is hereby granted, free of charge, to any person obtaining a copy of
   * this software and associated documentation files (the "Software"), to deal in
   * the Software without restriction, including without limitation the rights to
   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
   * the Software, and to permit persons to whom the Software is furnished to do so,
   * subject to the following conditions:
   *
   * The above copyright notice and this permission notice shall be included in all
   * copies or substantial portions of the Software.
   *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
   * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
   * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
   * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   */
  
  const minify = require("html-minifier").minify;
  const AmpOptimizer = require("@ampproject/toolbox-optimizer");
  const ampOptimizer = AmpOptimizer.create({
    blurredPlaceholders: true,
    imageBasePath: "./_site/",
    //verbose: true,
  });
  const PurgeCSS = require("purgecss").PurgeCSS;
  const csso = require("csso");
  
  /**
   * Inlines the CSS.
   * Makes font display display-optional
   * Minifies and optimizes the JS
   * Optimizes HTML
   * Optimizes AMP
   */
  
  const purifyCss = async (rawContent, outputPath) => {
    let content = rawContent;
    if (
      outputPath &&
      outputPath.endsWith(".html") &&
      !isAmp(content) &&
      !/data-style-override/.test(content)
    ) {
      let before = require("fs").readFileSync("css/main.css", {
        encoding: "utf-8",
      });
  
      before = before.replace(
        /@font-face {/g,
        "@font-face {font-display:swap;"
      );
  
      const purged = await new PurgeCSS().purge({
        content: [
          {
            raw: rawContent,
            extension: "html",
          },
        ],
        css: [
          {
            raw: before,
          },
        ],
        /*extractors: [
          {
            extractor: require("purge-from-html").extract,
            extensions: ["html"],
          },
        ],*/
        fontFace: true,
        variables: true,
      });
  
      const after = csso.minify(purged[0].css).css;
      //console.log("CSS reduction", before.length - after.length);
  
      content = content.replace("</head>", `<style>${after}</style></head>`);
    }
    return content;
  };
  
  const minifyHtml = (rawContent, outputPath) => {
    let content = rawContent;
    if (outputPath && outputPath.endsWith(".html") && !isAmp(content)) {
      content = minify(content, {
        removeAttributeQuotes: true,
        collapseBooleanAttributes: true,
        collapseWhitespace: true,
        removeComments: true,
        sortClassName: true,
        sortAttributes: true,
        html5: true,
        decodeEntities: true,
        removeOptionalTags: true,
      });
    }
    return content;
  };
  
  const optimizeAmp = async (rawContent, outputPath) => {
    let content = rawContent;
    if (outputPath && outputPath.endsWith(".html") && isAmp(content)) {
      content = await ampOptimizer.transformHtml(content);
    }
    return content;
  };
  
  module.exports = {
    initArguments: {},
    configFunction: async (eleventyConfig, pluginOptions = {}) => {
      eleventyConfig.addTransform("purifyCss", purifyCss);
      eleventyConfig.addTransform("minifyHtml", minifyHtml);
      eleventyConfig.addTransform("optimizeAmp", optimizeAmp);
    },
  };
  
  function isAmp(content) {
    return /\<html amp/i.test(content);
  }
  