<commit_before>// @flow

import * as t from '@babel/types'
import invariant from 'assert'
import traverse from '@babel/traverse'
import generate from '@babel/generator'
import pluginCommonJSModules from '@babel/plugin-transform-modules-commonjs'
import { promisify } from 'util'
import { transform } from '@babel/core'
import { createFileTransformer, getChunk, getUniqueHash } from 'pundle-api'

import { getName, getStringFromLiteralOrTemplate } from './helpers'
import manifest from '../package.json'
import pluginRemoveDeadNodes from './plugin-remove-dead-nodes'
import getPluginReplaceProcess from './plugin-replace-process'

const INJECTIONS = new Map([['timers', ['setImmediate', 'clearImmediate']], ['buffer', ['Buffer']], ['process', 'process']])
const INJECTIONS_NAMES = new Map()
INJECTIONS.forEach(function(names, sourceModule) {
  ;[].concat(names).forEach(function(name) {
    INJECTIONS_NAMES.set(name, sourceModule)
  })
})

const transformAsync = promisify(transform)

function createComponent() {
  return createFileTransformer({
    name: manifest.name,
    version: manifest.version,
    priority: 1000,
    async callback({ file, resolve, context, addImport, addChunk }) {
      if (file.format !== 'js') return null

      const promises = []
      const injectionNames = new Set()
      const { ast } = await transformAsync(typeof file.contents === 'string' ? file.contents : file.contents.toString(), {
        ast: true,
        code: false,
        babelrc: false,
        configFile: false,
        sourceMaps: false,
        filename: file.filePath,
        highlightCode: false,
        plugins: [getPluginReplaceProcess(context.config.target), pluginCommonJSModules],
        sourceType: 'unambiguous',
        parserOpts: {
          plugins: ['dynamicImport'],
        },
      })

      traverse(ast, {
        ...pluginRemoveDeadNodes,
        ImportDeclaration({ node }) {
          const { source } = node
          if (!t.isStringLiteral(source)) return
          promises.push(
            resolve(source.value, source.loc, !!node.specifiers.length).then(givenResolved => {
              if (givenResolved.filePath === false) return null
              const resolved = { ...givenResolved, format: 'js' }

              source.value = getUniqueHash(resolved)
              return addImport(resolved)
            }),
          )
        },
        // TODO: Support module.hot.accept etc in resolutions
        CallExpression(path) {
          const { node } = path
          const { callee } = node
          const [givenArg] = node.arguments

          const arg = getStringFromLiteralOrTemplate(givenArg)
          if (!arg) return

          if (t.isImport(callee)) {
            promises.push(
              resolve(arg.value.raw || arg.value, arg.loc).then(givenResolved => {
                if (givenResolved.filePath === false) return null
                const resolved = { ...givenResolved, format: 'js' }

                const chunk = getChunk(resolved.format, null, resolved.filePath, [], false)
                node.callee = t.memberExpression(t.identifier('require'), t.identifier('chunk'))
                node.arguments = [t.stringLiteral(context.getPublicPath(chunk)), t.stringLiteral(getUniqueHash(resolved))]
                return addChunk(chunk)
              }),
            )
            return
          }

          const calleeName = getName(callee, ['require'], 2)<commit_msg>:fire: Remove dead words in transformer-js<commit_after>// @flow

import * as t from '@babel/types'
import invariant from 'assert'
import traverse from '@babel/traverse'
import generate from '@babel/generator'
import pluginCommonJSModules from '@babel/plugin-transform-modules-commonjs'
import { promisify } from 'util'
import { transform } from '@babel/core'
import { createFileTransformer, getChunk, getUniqueHash } from 'pundle-api'

import { getName, getStringFromLiteralOrTemplate } from './helpers'
import manifest from '../package.json'
import pluginRemoveDeadNodes from './plugin-remove-dead-nodes'
import getPluginReplaceProcess from './plugin-replace-process'

const INJECTIONS = new Map([['timers', ['setImmediate', 'clearImmediate']], ['buffer', ['Buffer']], ['process', 'process']])
const INJECTIONS_NAMES = new Map()
INJECTIONS.forEach(function(names, sourceModule) {
  ;[].concat(names).forEach(function(name) {
    INJECTIONS_NAMES.set(name, sourceModule)
  })
})

const transformAsync = promisify(transform)

function createComponent() {
  return createFileTransformer({
    name: manifest.name,
    version: manifest.version,
    priority: 1000,
    async callback({ file, resolve, context, addImport, addChunk }) {
      if (file.format !== 'js') return null

      const promises = []
      const injectionNames = new Set()
      const { ast } = await transformAsync(typeof file.contents === 'string' ? file.contents : file.contents.toString(), {
        ast: true,
        code: false,
        babelrc: false,
        configFile: false,
        sourceMaps: false,
        filename: file.filePath,
        highlightCode: false,
        plugins: [getPluginReplaceProcess(context.config.target), pluginCommonJSModules],
        sourceType: 'unambiguous',
        parserOpts: {
          plugins: ['dynamicImport'],
        },
      })

      traverse(ast, {
        ...pluginRemoveDeadNodes,
        CallExpression(path) {
          const { node } = path
          const { callee } = node
          const [givenArg] = node.arguments

          const arg = getStringFromLiteralOrTemplate(givenArg)
          if (!arg) return

          if (t.isImport(callee)) {
            promises.push(
              resolve(arg.value.raw || arg.value, arg.loc).then(givenResolved => {
                if (givenResolved.filePath === false) return null
                const resolved = { ...givenResolved, format: 'js' }

                const chunk = getChunk(resolved.format, null, resolved.filePath, [], false)
                node.callee = t.memberExpression(t.identifier('require'), t.identifier('chunk'))
                node.arguments = [t.stringLiteral(context.getPublicPath(chunk)), t.stringLiteral(getUniqueHash(resolved))]
                return addChunk(chunk)
              }),
            )
            return
          }

          const calleeName = getName(callee, ['require'], 2)
{'fraction_non_alphanumeric': 0.12261322392299195, 'fraction_numerical': 0.0015780337699226763, 'mean_word_length': 2.7458628841607564, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '2262762', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1685, 'n_words': 412}