Sha256: 8c6ce6cbad418c5cf6578d839c07ba9bb243a82f582493e95619ec10c5b41155

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

const chokidar = require("chokidar")
const fs = require("fs-extra")
const htmlmin = require("html-minifier")
const sass = require("sass")

function minifyHtml(html) {
  return htmlmin.minify(html, {
    useShortDoctype: true,
    removeComments: true,
    collapseWhitespace: true
  })
}

function monkeypatch(cls, fn) {
  const orig = cls.prototype[fn.name][`_PS_original`] || cls.prototype[fn.name]
  function wrapped() {
    return fn.bind(this, orig).apply(this, arguments)
  }
  wrapped[`_PS_original`] = orig
  cls.prototype[fn.name] = wrapped
}

const minifyPlugin = {
  initArguments: {},
  configFunction: function(eleventyConfig) {
    eleventyConfig.addTransform(
      "htmlmin",
      function(content, outputPath) {
        if (outputPath && outputPath.endsWith(".html")) {
          return minifyHtml(content)
        }
        return content
      }
    )
  }
}

function compileSass() {
  const { css } = sass.renderSync({
    file: "site/style.scss",
  })
  fs.writeFileSync("_site/style.css", css)
}

module.exports = function(eleventyConfig) {
  console.log("ppl site")

  fs.ensureDirSync("_site")
  fs.emptyDirSync("_site")

  eleventyConfig.addPassthroughCopy("site/casts/*.svg")
  eleventyConfig.addPlugin(minifyPlugin)

  compileSass()

  if (process.argv.includes("--serve")) {
    setImmediate(function() {
      const Eleventy = require("@11ty/eleventy/src/Eleventy.js")
      if (Eleventy.prototype) {
        function watch(original) {
          const watcher = chokidar.watch(["./site/style.scss"], {
            persistent: true
          })
          const compileAndReload = eleventyInstance => () => {
            compileSass()
            this.eleventyServe.reload()
          }
          watcher.on("add", compileAndReload(this))
          watcher.on("change", compileAndReload(this))
          return original.apply(this)
        }
        monkeypatch(Eleventy, watch)
      }
    })
  }

  const input = "site"

  const dir = {
    input,
  }

  return {
	  dir,
  }
}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ppl-4.0.3 .eleventy.js