<commit_before>import createCache from '@emotion/cache'
import { CacheProvider } from '@emotion/react'
import createEmotionServer from '@emotion/server/create-instance'
import { ChunkExtractor, ChunkExtractorManager } from '@loadable/server'
import { createMemoryHistory } from 'history'
import type { Renderer } from 'html-renderer-webpack-plugin'
import { renderToString } from 'react-dom/server'
import type { FilledContext } from 'react-helmet-async'

import App from './components/App'

const whitespaceRegExp = /^\s+/gm
const emptyLineRegExp = /^\s*$(?:\r\n?|\n)/gm

const renderer: Renderer = async ({ path, stats }) => {
    const extractor = new ChunkExtractor({ entrypoints: ['main'], stats })
    const history = createMemoryHistory({ initialEntries: [path!] })
    const helmetContext = {} as FilledContext
    const cache = createCache({ key: 'css' })
    const { extractCritical } = createEmotionServer(cache)

    const app = (
        <ChunkExtractorManager extractor={extractor}>
            <CacheProvider value={cache}>
                <App helmetContext={helmetContext} history={history} />
            </CacheProvider>
        </ChunkExtractorManager>
    )

    const { html, css } = extractCritical(renderToString(app))
    const linkTags = extractor.getLinkTags()
    const scriptTags = extractor.getScriptTags({ type: 'module' }).replace(/async/gm, 'defer')

    return `
    <!DOCTYPE html>
    <html ${helmetContext.helmet.htmlAttributes.toString()}>
      <head>
        <meta name="version" content="${stats.hash}" />
        ${linkTags}
        ${helmetContext.helmet.meta.toString()}
        ${helmetContext.helmet.link.toString()}
        ${helmetContext.helmet.title.toString()}
        ${helmetContext.helmet.script.toString()}
        <style>${css}</style>
      </head>
      <body ${helmetContext.helmet.bodyAttributes.toString()}>
        <div id="root">${html}</div>
        ${scriptTags}
      </body>
    </html>
  `
        .replace(whitespaceRegExp, '')
        .replace(emptyLineRegExp, '')
}

export default renderer<commit_msg>refactor: construct multiple style tags<commit_after>import createCache from '@emotion/cache'
import { CacheProvider } from '@emotion/react'
import createEmotionServer from '@emotion/server/create-instance'
import { ChunkExtractor, ChunkExtractorManager } from '@loadable/server'
import { createMemoryHistory } from 'history'
import type { Renderer } from 'html-renderer-webpack-plugin'
import { renderToString } from 'react-dom/server'
import type { FilledContext } from 'react-helmet-async'

import App from './components/App'

const whitespaceRegExp = /^\s+/gm
const emptyLineRegExp = /^\s*$(?:\r\n?|\n)/gm

const renderer: Renderer = async ({ path, stats }) => {
    const extractor = new ChunkExtractor({ entrypoints: ['main'], stats })
    const history = createMemoryHistory({ initialEntries: [path!] })
    const helmetContext = {} as FilledContext
    const cache = createCache({ key: 'css' })
    const { extractCriticalToChunks, constructStyleTagsFromChunks } = createEmotionServer(cache)

    const app = (
        <ChunkExtractorManager extractor={extractor}>
            <CacheProvider value={cache}>
                <App helmetContext={helmetContext} history={history} />
            </CacheProvider>
        </ChunkExtractorManager>
    )

    const { html, styles } = extractCriticalToChunks(renderToString(app))
    const linkTags = extractor.getLinkTags()
    const scriptTags = extractor.getScriptTags({ type: 'module' }).replace(/async/gm, 'defer')

    return `
    <!DOCTYPE html>
    <html ${helmetContext.helmet.htmlAttributes.toString()}>
      <head>
        <meta name="version" content="${stats.hash}" />
        ${linkTags}
        ${helmetContext.helmet.meta.toString()}
        ${helmetContext.helmet.link.toString()}
        ${helmetContext.helmet.title.toString()}
        ${helmetContext.helmet.script.toString()}
        ${constructStyleTagsFromChunks({ html, styles })}
      </head>
      <body ${helmetContext.helmet.bodyAttributes.toString()}>
        <div id="root">${html}</div>
        ${scriptTags}
      </body>
    </html>
  `
        .replace(whitespaceRegExp, '')
        .replace(emptyLineRegExp, '')
}

export default renderer
{'fraction_non_alphanumeric': 0.14073899741115556, 'fraction_numerical': 0.0, 'mean_word_length': 3.5309168443496803, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 37, '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': '295580', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1218, 'n_words': 262}