<commit_before>import path from "path"
import fs from "fs-extra"
import color from "chalk"
import debug from "debug"

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

import collection from "../md-collection-loader/cache"
import toStaticHTML from "../static"

export default function(options) {
  const {
    config,
    store,
    exports,
  } = options

  const log = debug("statinamic:builder")
  // JSON.stringify(config, null, 2).split("\n").forEach(l => log(l))

  const destination = path.join(config.cwd, config.destination)
  fs.emptyDirSync(destination)

  if (config.static) {
    // Copy static assets to build folder
    if (config.assets) {
      const copyDest = path.join(destination, config.assets.route)
      fs.copySync(config.assets.path, copyDest)
      log(color.green("✓ Static assets: copy static assets completed"))
    }

    webpack(options.clientWebpackConfig, log, (stats) => {
      log(color.green("✓ Static assets: client build completed"))

      // TODO use a more reliable way to only get entry points
      const assetsFiles = {
        css: [],
        js: [],
      }
      const assets = stats.compilation.assets
      Object.keys(assets).forEach((name) => {
        if (name.endsWith(".js")) {
          assetsFiles.js.push(name)
        }
        if (name.endsWith(".css")) {
          assetsFiles.css.push(name)
        }
      })

      toStaticHTML({
        ...config,
        urls: [
          ...options.urls || [],
          ...collection.map(
            // get url without the base path
            (item) => item.__url.replace(config.baseUrl.pathname, "")
          ),
        ],
        collection,
        assetsFiles,
        exports,
        store,
      })
    })
  }<commit_msg>build: Reliable way to get assets' name<commit_after>import path from "path"
import fs from "fs-extra"
import color from "chalk"
import debug from "debug"

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

import collection from "../md-collection-loader/cache"
import toStaticHTML from "../static"

export default function(options) {
  const {
    config,
    store,
    exports,
  } = options

  const log = debug("statinamic:builder")
  // JSON.stringify(config, null, 2).split("\n").forEach(l => log(l))

  const destination = path.join(config.cwd, config.destination)
  fs.emptyDirSync(destination)

  if (config.static) {
    // Copy static assets to build folder
    if (config.assets) {
      const copyDest = path.join(destination, config.assets.route)
      fs.copySync(config.assets.path, copyDest)
      log(color.green("✓ Static assets: copy static assets completed"))
    }

    webpack(options.clientWebpackConfig, log, (stats) => {
      log(color.green("✓ Static assets: client build completed"))

      const assetsFiles = {
        css: [],
        js: [],
      }
      const assets = stats.toJson().assetsByChunkName

      // Flatten object of arrays
      // sort a-z => predictable chunks order
      Object.keys(assets)
        .reduce((result, key) => {
          const chunkAssets = assets[key]
          return result.concat(chunkAssets)
        }, [])
        .sort((a, b) => (a.toLowerCase() > b.toLowerCase()) ? 1 : -1)
        .forEach((name) => {
          if (name.endsWith(".js")) {
            assetsFiles.js.push(name)
          }
          else if (name.endsWith(".css")) {
            assetsFiles.css.push(name)
          }
        })

      toStaticHTML({
        ...config,
        urls: [
          ...options.urls || [],
          ...collection.map(
            // get url without the base path
            (item) => item.__url.replace(config.baseUrl.pathname, "")
          ),
        ],
        collection,
        assetsFiles,
        exports,
        store,
      })
    })
  }
{'fraction_non_alphanumeric': 0.13660337552742616, 'fraction_numerical': 0.0010548523206751054, 'mean_word_length': 2.179379715004191, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '2717546', 'n_tokens_mistral': 1103, 'n_tokens_neox': 1088, 'n_words': 271}