11import { chmodSync } from 'node:fs' ;
22import { createRequire } from 'node:module' ;
3- import os from 'node:os' ;
43import {
54 type RsbuildConfig ,
65 type RsbuildPlugin ,
@@ -19,12 +18,10 @@ const require = createRequire(import.meta.url);
1918const PLUGIN_NAME = 'rsbuild:lib-entry-chunk' ;
2019const 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 ;
0 commit comments