<commit_before>import { sync as rm } from "rimraf"
import { sync as mkdir } from "mkdirp"
import color from "chalk"
import nanoLogger from "nano-logger"

import webpack from "./webpack"
import devServer from "./dev-server"

export default function(options) {
  const {
    config,
    source,
  } = options
  const webpackConfig = options.webpack

  const log = nanoLogger("statinamic/lib/builder")

  JSON.stringify(config, null, 2).split("\n").forEach(l => log(l))

  const dest = webpackConfig.output.path

  // cleanup
  // rm(dest)
  // mkdir(dest)

  if (config.__DEVSERVER__) {
    devServer(webpackConfig, {
      protocol: config.__SERVER_PROTOCOL__,
      host: config.__SERVER_HOSTNAME__,
      port: config.__SERVER_PORT__,
      open: process.argv.includes("--open"),
    })
  }
  else {
    webpack(webpackConfig, log, (stats) => {
      log(color.green("✓ Static assets: build completed"))

      // faking webpack.DefinePlugin for node usage
      Object.keys(config).forEach((key) => {
        global[key] = config[key]
      })

      require("../to-static-html")({
        urls: [
          ...options.urls || [],
          ...getMdUrlsFromWebpackStats(stats, source),
        ],
        source,
        dest,
        exports: options.exports(),
        log,
      })
      .then(
        (files) => {
          log(
            color.green(`✓ Static html files: ${ files.length } files written`)
          )
        },
        (error) => {
          log(color.red(`✗ Static html files: failed to create files`))
          setTimeout(() => {
            throw error
          }, 1)
        }
      )
    })
  }
}

import filenameToUrl from "../filename-to-url"

function getMdUrlsFromWebpackStats(stats, source) {
  return stats.compilation.fileDependencies.reduce(
    (array, filename) => ([
      ...(filename.match(/\.md$/) ? [ filenameToUrl(filename, source) ] : []),
      ...array,
    ]),
    []
  )
}
<commit_msg>Clean by default<commit_after>import { sync as rm } from "rimraf"
import { sync as mkdir } from "mkdirp"
import color from "chalk"
import nanoLogger from "nano-logger"

import webpack from "./webpack"
import devServer from "./dev-server"

export default function(options) {
  const {
    config,
    source,
  } = options
  const webpackConfig = options.webpack

  const log = nanoLogger("statinamic/lib/builder")

  JSON.stringify(config, null, 2).split("\n").forEach(l => log(l))

  const dest = webpackConfig.output.path

  // cleanup
  rm(dest)
  mkdir(dest)

  if (config.__DEVSERVER__) {
    devServer(webpackConfig, {
      protocol: config.__SERVER_PROTOCOL__,
      host: config.__SERVER_HOSTNAME__,
      port: config.__SERVER_PORT__,
      open: process.argv.includes("--open"),
    })
  }
  else {
    webpack(webpackConfig, log, (stats) => {
      log(color.green("✓ Static assets: build completed"))

      // faking webpack.DefinePlugin for node usage
      Object.keys(config).forEach((key) => {
        global[key] = config[key]
      })

      require("../to-static-html")({
        urls: [
          ...options.urls || [],
          ...getMdUrlsFromWebpackStats(stats, source),
        ],
        source,
        dest,
        exports: options.exports(),
        log,
      })
      .then(
        (files) => {
          log(
            color.green(`✓ Static html files: ${ files.length } files written`)
          )
        },
        (error) => {
          log(color.red(`✗ Static html files: failed to create files`))
          setTimeout(() => {
            throw error
          }, 1)
        }
      )
    })
  }
}

import filenameToUrl from "../filename-to-url"

function getMdUrlsFromWebpackStats(stats, source) {
  return stats.compilation.fileDependencies.reduce(
    (array, filename) => ([
      ...(filename.match(/\.md$/) ? [ filenameToUrl(filename, source) ] : []),
      ...array,
    ]),
    []
  )
}

{'fraction_non_alphanumeric': 0.14746068574374838, 'fraction_numerical': 0.0010311936065996391, 'mean_word_length': 2.2360300250208507, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3584373', 'n_tokens_mistral': 1213, 'n_tokens_neox': 1183, 'n_words': 256}