<commit_before>module.exports = commonChunksPlugin

function commonChunksPlugin() {

  return {
    apply: compiler => {
      compiler.plugin('compilation', compilation => {

        compilation.plugin('optimize-chunks', chunks => {

          // determine the usage of modules
          const moduleUsage = chunks.reduce(
            (result, chunk) => chunk.getModules().reduce(
              (result, module) => {
                const chunks = result.get(module) || []
                chunks.push(chunk)

                return result.set(module, chunks)
              },
              result
            ),
            new Map()
          )

          // determine the chunks we need to make
          const newChunks = Array.from(moduleUsage.entries()).reduce(
            (result, [module, chunks]) => {
              if (chunks.length === 1) return result

              const newChunk = 'commons (' + chunks.map(chunk => chunk.name).join(', ') + ')'

              const { modules } = result[newChunk] || (result[newChunk] = { chunks, modules: [] })
              modules.push(module)
              return result
            },
            {}
          )

          // create the new chunks
          Object.keys(newChunks).forEach(chunkName => {

            const newChunk = compilation.addChunk(chunkName)
            newChunk.filenameTemplate = '[hash].js'

            const { chunks, modules } = newChunks[chunkName]

            modules.forEach(module => {

              // remove module from old chunks
              chunks.forEach(chunk => { chunk.removeModule(module) })

              // add module to new chunk
              newChunk.addModule(module)
              module.addChunk(newChunk)

              // connect old and new chunks
              chunks.forEach(chunk => {
                const parents = chunk.parents || []
                parents.push(newChunk)
                newChunk.addChunk(chunk)

                chunk.entrypoints.forEach(entrypoint => { entrypoint.insertChunk(newChunk, chunk) })
              })
            })
          })
        })
      })
    }
  }
}<commit_msg>fixed a serious bug by moving a block of code out of a loop<commit_after>module.exports = commonChunksPlugin

function commonChunksPlugin() {

  return {
    apply: compiler => {
      compiler.plugin('compilation', compilation => {

        compilation.plugin('optimize-chunks', chunks => {

          // determine the usage of modules
          const moduleUsage = chunks.reduce(
            (result, chunk) => chunk.getModules().reduce(
              (result, module) => {
                const chunks = result.get(module) || []
                chunks.push(chunk)

                return result.set(module, chunks)
              },
              result
            ),
            new Map()
          )

          // determine the chunks we need to make
          const newChunks = Array.from(moduleUsage.entries()).reduce(
            (result, [module, chunks]) => {
              if (chunks.length === 1) return result

              const newChunk = 'commons (' + chunks.map(chunk => chunk.name).join(', ') + ')'

              const { modules } = result[newChunk] || (result[newChunk] = { chunks, modules: [] })
              modules.push(module)
              return result
            },
            {}
          )

          // create the new chunks
          Object.keys(newChunks).forEach(chunkName => {

            const newChunk = compilation.addChunk(chunkName)
            newChunk.filenameTemplate = '[id].[hash].js'

            const { chunks, modules } = newChunks[chunkName]

            // move the modules
            modules.forEach(module => {

              // remove module from old chunks
              chunks.forEach(chunk => { chunk.removeModule(module) })

              // add module to new chunk
              newChunk.addModule(module)
              module.addChunk(newChunk)
            })

            // connect old and new chunks
            chunks.forEach(chunk => {
              const parents = chunk.parents || []
              parents.push(newChunk)
              newChunk.addChunk(chunk)

              chunk.entrypoints.forEach(entrypoint => { entrypoint.insertChunk(newChunk, chunk) })
            })
          })

          return false
        })
      })
    }
  }
}
{'fraction_non_alphanumeric': 0.11275074936592114, 'fraction_numerical': 0.0004611482591653217, 'mean_word_length': 1.5220930232558139, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '4221808', 'n_tokens_mistral': 1078, 'n_tokens_neox': 1042, 'n_words': 236}