Skip to content

Commit 3f6e249

Browse files
committed
www
1 parent fc198ad commit 3f6e249

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

packages/core/src/constant.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const DEFAULT_CONFIG_EXTENSIONS = [
1212
export const SWC_HELPERS = '@swc/helpers';
1313
export const RSLIB_ENTRY_QUERY = '__rslib_entry__';
1414
export const SHEBANG_PREFIX = '#!';
15-
export const SHEBANG_REGEX: RegExp = /#!.*[\s\n\r]*/;
15+
export const SHEBANG_REGEX: RegExp = /#!.*[\s\n\r]*$/;
1616
export const REACT_DIRECTIVE_REGEX: RegExp =
1717
/^['"]use (client|server)['"](;?)[\s\n\r]*$/;
1818

packages/core/src/plugins/EntryChunkPlugin.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { chmodSync } from 'node:fs';
22
import { createRequire } from 'node:module';
3-
import os from 'node:os';
3+
// import os from 'node:os';
44
import {
55
type RsbuildConfig,
66
type RsbuildPlugin,
@@ -19,8 +19,9 @@ const require = createRequire(import.meta.url);
1919
const PLUGIN_NAME = 'rsbuild:lib-entry-chunk';
2020
const LOADER_NAME = 'rsbuild:lib-entry-module';
2121

22-
const matchFirstLine = (source: string, regex: RegExp) => {
23-
const [firstLine] = source.split(os.EOL);
22+
const matchFirstLine = (source: string, regex: RegExp): string | false => {
23+
const lineBreak = source.match(/(\r\n|\n)/);
24+
const firstLine = source.slice(0, lineBreak?.index);
2425
if (!firstLine) {
2526
return false;
2627
}

packages/core/src/plugins/entryModuleLoader.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
1-
import os from 'node:os';
1+
// import os from 'node:os';
22
import type { LoaderDefinition } from '@rspack/core';
33
import {
44
REACT_DIRECTIVE_REGEX,
55
RSLIB_ENTRY_QUERY,
66
SHEBANG_REGEX,
77
} from '../constant';
88

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+
918
const loader: LoaderDefinition = function loader(source) {
1019
let result = source;
1120

1221
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);
1525

16-
if (SHEBANG_REGEX.test(firstLine1!)) {
17-
result = rest1.join('\n');
26+
if (SHEBANG_REGEX.test(firstLine1)) {
27+
result = rest;
1828
}
1929

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;
2333
}
2434
}
2535

0 commit comments

Comments
 (0)