Repository: vitejs/vite
File: packages/vite/src/node/plugins/css.ts (TypeScript)

Code before:
                    // legacy build
                    if (code.startsWith('#!')) {
                      let secondLinePos = code.indexOf('\n')
                      if (secondLinePos === -1) {
                        secondLinePos = 0
                      }
                      injectionPoint = secondLinePos
                    } else {
                      injectionPoint = 0
                    }
                  } else {
                    this.error('Non supported format')
                    return
                  }

                  s ||= new MagicString(code)
                  s.appendRight(injectionPoint, injectCode)
                }
              } else {
                // resolve public URL from CSS paths, we need to use absolute paths
                // Use 'style.css' as originalFileName since this is an aggregated
                // bundle and will be emitted with originalFileName: 'style.css'
                chunkCSS = resolveAssetUrlsInCss(
                  chunkCSS,
                  getCssBundleName(),
                  'style.css',
                )
                // finalizeCss is called for the aggregated chunk in generateBundle

                chunkCSSMap.set(chunk.fileName, chunkCSS)
              }
            }

            if (s) {
              if (config.build.sourcemap) {
                return {
                  code: s.toString(),
                  map: s.generateMap({ hires: 'boundary' }),
                }
              } else {
                return { code: s.toString() }
              }
            }
            return null
          },

          augmentChunkHash(chunk) {
            if (chunk.viteMetadata?.importedCss.size) {
              let hash = ''
              for (const id of chunk.viteMetadata.importedCss) {
                hash += id

Review comment (refactor):
@copilot extract this as a variable together with https://github.com/vitejs/vite/blob/870d512b5d354b07e920cd6c5fcf1cdd7a4e3d53/packages/vite/src/node/plugins/css.ts#L1067-L1069

Code after:
                    // legacy build
                    if (code.startsWith('#!')) {
                      let secondLinePos = code.indexOf('\n')
                      if (secondLinePos === -1) {
                        secondLinePos = 0
                      }
                      injectionPoint = secondLinePos
                    } else {
                      injectionPoint = 0
                    }
                  } else {
                    this.error('Non supported format')
                    return
                  }

                  s ||= new MagicString(code)
                  s.appendRight(injectionPoint, injectCode)
                }
              } else {
                // resolve public URL from CSS paths, we need to use absolute paths
                chunkCSS = resolveAssetUrlsInCss(
                  chunkCSS,
                  getCssBundleName(),
                  defaultCssBundleName,
                )
                // finalizeCss is called for the aggregated chunk in generateBundle

                chunkCSSMap.set(chunk.fileName, chunkCSS)
              }
            }

            if (s) {
              if (config.build.sourcemap) {
                return {
                  code: s.toString(),
                  map: s.generateMap({ hires: 'boundary' }),
                }
              } else {
                return { code: s.toString() }
              }
            }
            return null
          },

          augmentChunkHash(chunk) {
            if (chunk.viteMetadata?.importedCss.size) {
              let hash = ''
              for (const id of chunk.viteMetadata.importedCss) {
                hash += id
              }
              return hash