<commit_before>const { join, relative } = require('path')
const webpack = require('webpack')
const merge = require('webpack-merge')
const VirtualModulesPlugin = require('webpack-virtual-modules')
const getBuildId = require('object-hash')
const cache = require('./cache')
const { yellow, cyan, white, green, blue } = require('chalk')
const {
  DebounceUtil: { debounce },
  StringUtil: { crossPlatformPath }
} = require('@uiengine/util')

const QUEUES = {}
const TARGET_FOLDER = '_webpack'
const WEBPACK_NAME_SERVER = 'server'
const WEBPACK_NAME_CLIENT = 'client'

// queue ids separate different adapter types, as in one project multiple
// file types (i.e. react and vue) can be build with the webpack adapter.
// -> one queue per file type
const getQueue = options => {
  const id = getBuildId(options)
  if (!QUEUES[id]) {
    QUEUES[id] = {
      id,
      config: buildConfig(options),
      handles: {},
      promises: {}
    }
  }

  return QUEUES[id]
}

const drainQueue = id => {
  const queue = QUEUES[id]
  delete QUEUES[id]

  return queue
}

// naive implementation of a  dynamic threshold, which ensures
// high value for initial build and keeps rebuild times low.
const getDebounceThreshold = buildId =>
  cache.all(buildId).length === 0 ? 500 : 50

const getFileId = (filePath, type) =>
  join('_build', relative(process.cwd(), filePath).replace(/[^\w\s]/gi, '_'), type)

// prevent problems with cache-loader not being able to lookup virtual
// files by using a custom file type that no other loader uses.
const getEntryPath = id => `/${id}.js-virtual`

const filesDir = ({ target }) => join(target, TARGET_FOLDER)

const debug = (opts, label, ...additional) => {
  if (!opts.debug) return

  // we intentionally skip the timing functions here
  const prefix = `WebpackAdapter[${opts.ext}]`
  const [, timingLabel, timingEvent] = label.match(/(.*):(start|end)$/) || []
  if (timingLabel && timingEvent) {
    const action = timingEvent === 'start' ? 'time' : 'timeEnd'
    if (timingEvent === 'start') console.debug(yellow(`${prefix}.${timingLabel} -> start`), additional.join(['\n\n']))
    console[action](yellow(`${prefix}.${timingLabel} -> end`))
  } else {
    console.debug(cyan(`${prefix}.${label}`), additional.join(['\n\n']))
  }
}

const buildConfig = options => {
  const { serverConfig, serverRenderPath, clientConfig, clientRenderPath } = options
  const config = {}

  // deliberately skip optimizations
  const mode = 'development'

  // build webpack config by overriding entry and output
  // and explicitely setting the target
  if (serverConfig && serverRenderPath) {
    config.server = merge(serverConfig, {<commit_msg>🐞 webpack: replace entry and output on merge<commit_after>const { join, relative } = require('path')
const webpack = require('webpack')
const webpackMerge = require('webpack-merge')
const VirtualModulesPlugin = require('webpack-virtual-modules')
const getBuildId = require('object-hash')
const cache = require('./cache')
const { yellow, cyan, white, green, blue } = require('chalk')
const {
  DebounceUtil: { debounce },
  StringUtil: { crossPlatformPath }
} = require('@uiengine/util')

const QUEUES = {}
const TARGET_FOLDER = '_webpack'
const WEBPACK_NAME_SERVER = 'server'
const WEBPACK_NAME_CLIENT = 'client'

// queue ids separate different adapter types, as in one project multiple
// file types (i.e. react and vue) can be build with the webpack adapter.
// -> one queue per file type
const getQueue = options => {
  const id = getBuildId(options)
  if (!QUEUES[id]) {
    QUEUES[id] = {
      id,
      config: buildConfig(options),
      handles: {},
      promises: {}
    }
  }

  return QUEUES[id]
}

const drainQueue = id => {
  const queue = QUEUES[id]
  delete QUEUES[id]

  return queue
}

// naive implementation of a  dynamic threshold, which ensures
// high value for initial build and keeps rebuild times low.
const getDebounceThreshold = buildId =>
  cache.all(buildId).length === 0 ? 500 : 50

const getFileId = (filePath, type) =>
  join('_build', relative(process.cwd(), filePath).replace(/[^\w\s]/gi, '_'), type)

// prevent problems with cache-loader not being able to lookup virtual
// files by using a custom file type that no other loader uses.
const getEntryPath = id => `/${id}.js-virtual`

const filesDir = ({ target }) => join(target, TARGET_FOLDER)

const debug = (opts, label, ...additional) => {
  if (!opts.debug) return

  // we intentionally skip the timing functions here
  const prefix = `WebpackAdapter[${opts.ext}]`
  const [, timingLabel, timingEvent] = label.match(/(.*):(start|end)$/) || []
  if (timingLabel && timingEvent) {
    const action = timingEvent === 'start' ? 'time' : 'timeEnd'
    if (timingEvent === 'start') console.debug(yellow(`${prefix}.${timingLabel} -> start`), additional.join(['\n\n']))
    console[action](yellow(`${prefix}.${timingLabel} -> end`))
  } else {
    console.debug(cyan(`${prefix}.${label}`), additional.join(['\n\n']))
  }
}

const merge = webpackMerge.strategy({ entry: 'replace', output: 'replace' })

const buildConfig = options => {
  const { serverConfig, serverRenderPath, clientConfig, clientRenderPath } = options
  const config = {}

  // deliberately skip optimizations
  const mode = 'development'

  // build webpack config by overriding entry and output
  // and explicitely setting the target
  if (serverConfig && serverRenderPath) {
    config.server = merge(serverConfig, {
{'fraction_non_alphanumeric': 0.14672312223858616, 'fraction_numerical': 0.0022091310751104565, 'mean_word_length': 4.082319925163705, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '5883229', 'n_tokens_mistral': 1662, 'n_tokens_neox': 1622, 'n_words': 492}