@@ -40,8 +40,6 @@ class EntryChunkPlugin {
4040 private shebangInjectedAssets : Set < string > = new Set ( ) ;
4141
4242 private enabledImportMetaUrlShim : boolean ;
43- private importMetaUrlShims : Record < string , { startsWithUseStrict : boolean } > =
44- { } ;
4543
4644 constructor ( {
4745 enabledImportMetaUrlShim = true ,
@@ -89,17 +87,6 @@ class EntryChunkPlugin {
8987 if ( reactDirective ) {
9088 this . reactDirectives [ name ] = reactDirective ;
9189 }
92-
93- // import.meta.url shim
94- if ( this . enabledImportMetaUrlShim ) {
95- this . importMetaUrlShims [ name ] = {
96- startsWithUseStrict :
97- // This is a hypothesis that no comments will occur before "use strict;".
98- // But it should cover most cases.
99- content . startsWith ( 'use strict;' ) ||
100- content . startsWith ( '"use strict";' ) ,
101- } ;
102- }
10390 }
10491 } ) ;
10592
@@ -120,11 +107,6 @@ class EntryChunkPlugin {
120107 if ( reactDirective ) {
121108 this . reactDirectives [ filename ] = reactDirective ;
122109 }
123-
124- const importMetaUrlShimInfo = this . importMetaUrlShims [ name ] ;
125- if ( importMetaUrlShimInfo ) {
126- this . importMetaUrlShims [ filename ] = importMetaUrlShimInfo ;
127- }
128110 } ) ;
129111 } ) ;
130112
@@ -134,24 +116,22 @@ class EntryChunkPlugin {
134116 for ( const name of chunkAsset ) {
135117 if ( this . enabledImportMetaUrlShim ) {
136118 compilation . updateAsset ( name , ( old ) => {
137- const importMetaUrlShimInfo = this . importMetaUrlShims [ name ] ;
138- if ( importMetaUrlShimInfo ) {
139- const replaceSource = new rspack . sources . ReplaceSource ( old ) ;
140-
141- if ( importMetaUrlShimInfo . startsWithUseStrict ) {
142- replaceSource . replace (
143- 0 ,
144- 11 , // 'use strict;'.length,
145- `"use strict";${ os . EOL } ${ importMetaUrlShim } ` ,
146- ) ;
147- } else {
148- replaceSource . insert ( 0 , importMetaUrlShim ) ;
149- }
150-
151- return replaceSource ;
119+ const oldSource = old . source ( ) . toString ( ) ;
120+ const replaceSource = new rspack . sources . ReplaceSource ( old ) ;
121+ if (
122+ oldSource . startsWith ( 'use strict;' ) ||
123+ oldSource . startsWith ( '"use strict";' )
124+ ) {
125+ replaceSource . replace (
126+ 0 ,
127+ 11 , // 'use strict;'.length,
128+ `"use strict";${ os . EOL } ${ importMetaUrlShim } ` ,
129+ ) ;
130+ } else {
131+ replaceSource . insert ( 0 , importMetaUrlShim ) ;
152132 }
153133
154- return old ;
134+ return replaceSource ;
155135 } ) ;
156136 }
157137 }
0 commit comments