Skip to content

Commit 903d52d

Browse files
committed
Line endings are silly... what a world
1 parent dbe8ac2 commit 903d52d

File tree

1 file changed

+7
-1
lines changed
  • src/module-server/middleware

1 file changed

+7
-1
lines changed

src/module-server/middleware/js.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { promises as fs } from 'node:fs';
2-
import { EOL } from 'node:os';
32
import { dirname, isAbsolute, posix, relative, resolve, sep } from 'node:path';
43

54
import type { DecodedSourceMap, RawSourceMap } from '@ampproject/remapping';
@@ -103,6 +102,13 @@ export const jsMiddleware = async ({
103102
import.meta.pleasantestArgs = [...window._pleasantestArgs]
104103
}`;
105104
const fileSrc = await fs.readFile(file, 'utf8');
105+
let EOL = '\n';
106+
// Find the first line end (\n) and check if the character before is \r
107+
// This tells us whether the file uses \r\n or just \n for line endings.
108+
// Using node:os.EOL is not sufficient because git on windows
109+
// Can be configured to check out files with either kind of line ending.
110+
const firstLineEndPos = fileSrc.indexOf('\n');
111+
if (fileSrc[firstLineEndPos - 1] === '\r') EOL = '\r\n';
106112
const inlineStartIdx = fileSrc.indexOf(code.replaceAll('\n', EOL));
107113
code = injectedArgsCode + code;
108114
if (inlineStartIdx !== -1) {

0 commit comments

Comments
 (0)