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

Code before:
          build.onLoad({ filter: /\.[cm]?[jt]s$/ }, async (args) => {
            const contents = await fsp.readFile(args.path, 'utf-8')
            let injectValues =
              `const ${dirnameVarName} = ${JSON.stringify(
                path.dirname(args.path),
              )};` +
              `const ${filenameVarName} = ${JSON.stringify(args.path)};` +
              `const ${importMetaUrlVarName} = ${JSON.stringify(
                pathToFileURL(args.path).href,
              )};`
            if (contents.includes('import.meta.resolve')) {
              if (isESM) {
                if (!importMetaResolverRegistered) {
                  importMetaResolverRegistered = true
                  await createImportMetaResolver()
                }
                injectValues += `const ${importMetaResolveVarName} = (specifier, importer = ${importMetaUrlVarName}) => (${importMetaResolveWithCustomHookString})(specifier, importer);`
              } else {
                injectValues += `const ${importMetaResolveVarName} = (specifier, importer = ${importMetaUrlVarName}) => { throw new Error('import.meta.resolve is not supported in CJS config files') };`
              }
            }

            let injectedContents: string
            if (contents.startsWith('#!')) {
              // hashbang
              const firstLineEndIndex = contents.indexOf('\n')
              injectedContents =
                contents.slice(0, firstLineEndIndex + 1) +
                injectValues +
                contents.slice(firstLineEndIndex + 1)
            } else {
              injectedContents = injectValues + contents
            }

            return {
              loader: args.path.endsWith('ts') ? 'ts' : 'js',
              contents: injectedContents,
            }
          })
        },
      },
    ],
  })
  const { text } = result.outputFiles[0]
  return {
    code: text,
    dependencies: Object.keys(result.metafile.inputs),
  }
}

interface NodeModuleWithCompile extends NodeModule {

Review comment (suggestion):
```suggestion
              let firstLineEndIndex = contents.indexOf('\n')
              if (firstLineEndIndex < 0) firstLineEndIndex = contents.length
```

Code after:
          build.onLoad({ filter: /\.[cm]?[jt]s$/ }, async (args) => {
            const contents = await fsp.readFile(args.path, 'utf-8')
            let injectValues =
              `const ${dirnameVarName} = ${JSON.stringify(
                path.dirname(args.path),
              )};` +
              `const ${filenameVarName} = ${JSON.stringify(args.path)};` +
              `const ${importMetaUrlVarName} = ${JSON.stringify(
                pathToFileURL(args.path).href,
              )};`
            if (contents.includes('import.meta.resolve')) {
              if (isESM) {
                if (!importMetaResolverRegistered) {
                  importMetaResolverRegistered = true
                  await createImportMetaResolver()
                }
                injectValues += `const ${importMetaResolveVarName} = (specifier, importer = ${importMetaUrlVarName}) => (${importMetaResolveWithCustomHookString})(specifier, importer);`
              } else {
                injectValues += `const ${importMetaResolveVarName} = (specifier, importer = ${importMetaUrlVarName}) => { throw new Error('import.meta.resolve is not supported in CJS config files') };`
              }
            }

            let injectedContents: string
            if (contents.startsWith('#!')) {
              // hashbang
              let firstLineEndIndex = contents.indexOf('\n')
              if (firstLineEndIndex < 0) firstLineEndIndex = contents.length
              injectedContents =
                contents.slice(0, firstLineEndIndex + 1) +
                injectValues +
                contents.slice(firstLineEndIndex + 1)
            } else {
              injectedContents = injectValues + contents
            }

            return {
              loader: args.path.endsWith('ts') ? 'ts' : 'js',
              contents: injectedContents,
            }
          })
        },
      },
    ],
  })
  const { text } = result.outputFiles[0]
  return {
    code: text,
    dependencies: Object.keys(result.metafile.inputs),
  }
}