|
1 | | -import os from 'node:os'; |
| 1 | +// import os from 'node:os'; |
2 | 2 | import type { LoaderDefinition } from '@rspack/core'; |
3 | 3 | import { |
4 | 4 | REACT_DIRECTIVE_REGEX, |
5 | 5 | RSLIB_ENTRY_QUERY, |
6 | 6 | SHEBANG_REGEX, |
7 | 7 | } from '../constant'; |
8 | 8 |
|
| 9 | +function splitFirstLine(text: string): [string, string] { |
| 10 | + const match = text.match(/(\r\n|\n)/); |
| 11 | + if (!match) { |
| 12 | + return [text, '']; |
| 13 | + } |
| 14 | + |
| 15 | + return [text.slice(0, match.index), text.slice(match.index)]; |
| 16 | +} |
| 17 | + |
9 | 18 | const loader: LoaderDefinition = function loader(source) { |
10 | 19 | let result = source; |
11 | 20 |
|
12 | 21 | if (this.resourceQuery === `?${RSLIB_ENTRY_QUERY}`) { |
13 | | - console.log('👝', source.includes('\n'), source.includes('\r\n')); |
14 | | - const [firstLine1, ...rest1] = result.split(os.EOL); |
| 22 | + // console.log('👝', source.includes('\n'), source.includes('\r\n')); |
| 23 | + // const [firstLine1, ...rest1] = result.split('\n'); |
| 24 | + const [firstLine1, rest] = splitFirstLine(result); |
15 | 25 |
|
16 | | - if (SHEBANG_REGEX.test(firstLine1!)) { |
17 | | - result = rest1.join('\n'); |
| 26 | + if (SHEBANG_REGEX.test(firstLine1)) { |
| 27 | + result = rest; |
18 | 28 | } |
19 | 29 |
|
20 | | - const [firstLine2, ...rest2] = result.split(os.EOL); |
21 | | - if (REACT_DIRECTIVE_REGEX.test(firstLine2!)) { |
22 | | - result = rest2.join('\n'); |
| 30 | + const [firstLine2, rest2] = splitFirstLine(result); |
| 31 | + if (REACT_DIRECTIVE_REGEX.test(firstLine2)) { |
| 32 | + result = rest2; |
23 | 33 | } |
24 | 34 | } |
25 | 35 |
|
|
0 commit comments