Skip to content

Commit 8db8c13

Browse files
committed
win
www
1 parent 4a7c331 commit 8db8c13

File tree

7 files changed

+35
-34
lines changed

7 files changed

+35
-34
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: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { chmodSync } from 'node:fs';
22
import { createRequire } from 'node:module';
3-
import os from 'node:os';
43
import {
54
type RsbuildConfig,
65
type RsbuildPlugin,
@@ -19,12 +18,10 @@ const require = createRequire(import.meta.url);
1918
const PLUGIN_NAME = 'rsbuild:lib-entry-chunk';
2019
const LOADER_NAME = 'rsbuild:lib-entry-module';
2120

22-
const matchFirstLine = (source: string, regex: RegExp) => {
23-
const [firstLine] = source.split(os.EOL);
24-
if (!firstLine) {
25-
return false;
26-
}
27-
const matched = regex.exec(firstLine);
21+
const matchFirstLine = (source: string, regex: RegExp): string | false => {
22+
const lineBreakPos = source.match(/(\r\n|\n)/);
23+
const firstLineContent = source.slice(0, lineBreakPos?.index);
24+
const matched = regex.exec(firstLineContent);
2825
if (!matched) {
2926
return false;
3027
}
@@ -126,7 +123,7 @@ class EntryChunkPlugin {
126123
replaceSource.replace(
127124
0,
128125
11, // 'use strict;'.length,
129-
`"use strict";${os.EOL}${importMetaUrlShim}`,
126+
`"use strict";\n${importMetaUrlShim}`,
130127
);
131128
} else {
132129
replaceSource.insert(0, importMetaUrlShim);
@@ -154,13 +151,13 @@ class EntryChunkPlugin {
154151
const replaceSource = new rspack.sources.ReplaceSource(old);
155152
// Shebang
156153
if (shebangValue) {
157-
replaceSource.insert(0, `${shebangValue}${os.EOL}`);
154+
replaceSource.insert(0, `${shebangValue}\n`);
158155
this.shebangInjectedAssets.add(name);
159156
}
160157

161158
// React directives
162159
if (reactDirectiveValue) {
163-
replaceSource.insert(0, `${reactDirectiveValue}${os.EOL}`);
160+
replaceSource.insert(0, `${reactDirectiveValue}\n`);
164161
}
165162

166163
return replaceSource;

packages/core/src/plugins/entryModuleLoader.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
1-
import os from 'node:os';
21
import type { LoaderDefinition } from '@rspack/core';
32
import {
43
REACT_DIRECTIVE_REGEX,
54
RSLIB_ENTRY_QUERY,
65
SHEBANG_REGEX,
76
} from '../constant';
87

8+
function splitFromFirstLine(text: string): [string, string] {
9+
const match = text.match(/(\r\n|\n)/);
10+
if (!match) {
11+
return [text, ''];
12+
}
13+
14+
return [text.slice(0, match.index), text.slice(match.index)];
15+
}
16+
917
const loader: LoaderDefinition = function loader(source) {
1018
let result = source;
1119

1220
if (this.resourceQuery === `?${RSLIB_ENTRY_QUERY}`) {
13-
const [firstLine1, ...rest1] = result.split(os.EOL).slice(1);
14-
if (SHEBANG_REGEX.test(firstLine1!)) {
15-
result = rest1.join(os.EOL);
21+
const [firstLine1, rest] = splitFromFirstLine(result);
22+
23+
if (SHEBANG_REGEX.test(firstLine1)) {
24+
result = rest;
1625
}
1726

18-
const [firstLine2, ...rest2] = result.split(os.EOL);
19-
if (REACT_DIRECTIVE_REGEX.test(firstLine2!)) {
20-
result = rest2.join(os.EOL);
27+
const [firstLine2, rest2] = splitFromFirstLine(result);
28+
if (REACT_DIRECTIVE_REGEX.test(firstLine2)) {
29+
result = rest2;
2130
}
2231
}
2332

pnpm-lock.yaml

Lines changed: 4 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integration/directive/react/bundleless/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "directive-react-bundleless-test",
2+
"name": "directive-react-test",
33
"version": "1.0.0",
44
"private": true,
55
"type": "module"

tests/integration/directive/shebang/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "directive-shebang-bundle-test",
2+
"name": "directive-shebang-test",
33
"version": "1.0.0",
44
"private": true,
55
"type": "module"

tests/integration/shims/index.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,11 @@ describe('CJS shims', () => {
9595
const fileUrl = pathToFileURL(entryFiles.cjs).href;
9696
expect(importMetaUrl).toBe(fileUrl);
9797
expect(requiredModule).toBe('ok');
98-
expect(cjsCode.startsWith('"use strict"')).toBe(true);
99-
expect(cjsCode).toContain(
100-
'const __rslib_import_meta_url__ = /*#__PURE__*/ function() {',
101-
);
98+
expect(
99+
cjsCode.startsWith(
100+
`"use strict";\nconst __rslib_import_meta_url__ = /*#__PURE__*/ function() {`,
101+
),
102+
).toBe(true);
102103
});
103104

104105
test('ESM should not be affected by CJS shims configuration', async () => {

0 commit comments

Comments
 (0)