<commit_before>// @flow

import path from 'path'
import postcss from 'postcss'
import postcssModules from 'postcss-modules'
import { createFileTransformer, getChunk } from 'pundle-api'

import manifest from '../package.json'
import pluginImportResolver from './plugin-import-resolver'
import { getDevelopmentContents } from './helpers'

function createComponent({
  extensions = ['.css'],
  development = process.env.NODE_ENV !== 'production',
}: { extensions?: Array<string>, development?: boolean } = {}) {
  return createFileTransformer({
    name: 'pundle-transformer-css',
    version: manifest.version,
    priority: 1500,
    async callback({ file, resolve, addChunk }) {
      const extName = path.extname(file.filePath)
      if (!extensions.includes(extName)) {
        return null
      }

      let moduleMap = null
      const plugins = []
      const fileIsModule = file.filePath.endsWith(`.module${extName}`)

      if (fileIsModule) {
        plugins.push(
          postcssModules({
            scopeBehaviour: 'local',
            getJSON(_, map) {
              moduleMap = map
            },
          }),
        )
      }
      plugins.push(
        pluginImportResolver({
          file,
          resolve,
          addChunk,
        }),
      )

      const inlineMap = development && file.format === 'js'
      const cssChunk = getChunk('css', null, file.filePath)
      const processed = await postcss(plugins).process(
        typeof file.contents === 'string' ? file.contents : file.contents.toString(),
        {
          from: file.filePath,
          map: { inline: inlineMap, annotation: false },
        },
      )

      if (file.format === 'js') {
        const moduleMapContents = moduleMap ? `module.exports = ${JSON.stringify(moduleMap)}\n` : ''

        if (development) {
          return {
            contents: `${getDevelopmentContents(processed.css)}\n${moduleMapContents}`,
            sourceMap: false,
          }
        }

        // was imported from a JS file
        await addChunk(cssChunk)

        return {
          contents: moduleMapContents,
          sourceMap: false,
        }
      } else if (file.format === 'css') {
        // entry or was imported from a css file

        return {
          contents: processed.css,
          sourceMap: processed.map.toJSON(),
        }
      }
      throw new Error(`Unknown format for css files '${file.format}' encountered in loader-css`)
    },
  })
}

module.exports = createComponent
<commit_msg>:memo: Document a todo in transformer-css<commit_after>// @flow

import path from 'path'
import postcss from 'postcss'
import postcssModules from 'postcss-modules'
import { createFileTransformer, getChunk } from 'pundle-api'

import manifest from '../package.json'
import pluginImportResolver from './plugin-import-resolver'
import { getDevelopmentContents } from './helpers'

function createComponent({
  extensions = ['.css'],
  development = process.env.NODE_ENV !== 'production',
}: { extensions?: Array<string>, development?: boolean } = {}) {
  return createFileTransformer({
    name: 'pundle-transformer-css',
    version: manifest.version,
    priority: 1500,
    async callback({ file, resolve, addChunk }) {
      const extName = path.extname(file.filePath)
      if (!extensions.includes(extName)) {
        return null
      }

      let moduleMap = null
      const plugins = []
      const fileIsModule = file.filePath.endsWith(`.module${extName}`)

      if (fileIsModule) {
        plugins.push(
          postcssModules({
            scopeBehaviour: 'local',
            getJSON(_, map) {
              moduleMap = map
            },
          }),
        )
      }
      plugins.push(
        pluginImportResolver({
          file,
          resolve,
          addChunk,
        }),
      )

      const inlineMap = development && file.format === 'js'
      const cssChunk = getChunk('css', null, file.filePath)
      // TODO: Fix source map paths
      const processed = await postcss(plugins).process(
        typeof file.contents === 'string' ? file.contents : file.contents.toString(),
        {
          from: file.filePath,
          map: { inline: inlineMap, annotation: false },
        },
      )

      if (file.format === 'js') {
        const moduleMapContents = moduleMap ? `module.exports = ${JSON.stringify(moduleMap)}\n` : ''

        if (development) {
          return {
            contents: `${getDevelopmentContents(processed.css)}\n${moduleMapContents}`,
            sourceMap: false,
          }
        }

        // was imported from a JS file
        await addChunk(cssChunk)

        return {
          contents: moduleMapContents,
          sourceMap: false,
        }
      } else if (file.format === 'css') {
        // entry or was imported from a css file

        return {
          contents: processed.css,
          sourceMap: processed.map.toJSON(),
        }
      }
      throw new Error(`Unknown format for css files '${file.format}' encountered in loader-css`)
    },
  })
}

module.exports = createComponent

{'fraction_non_alphanumeric': 0.12022073314938904, 'fraction_numerical': 0.0015766653527788726, 'mean_word_length': 2.1818181818181817, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '403935', 'n_tokens_mistral': 1386, 'n_tokens_neox': 1352, 'n_words': 348}