@@ -3,6 +3,7 @@ use std::{
33 sync:: { Arc , LazyLock } ,
44} ;
55
6+ use atomic_refcell:: AtomicRefCell ;
67use regex:: Regex ;
78use rspack_collections:: { IdentifierIndexMap , IdentifierSet , UkeyMap } ;
89use rspack_core:: {
@@ -23,7 +24,7 @@ use rspack_plugin_javascript::{
2324 dependency:: ImportDependencyTemplate , parser_and_generator:: JavaScriptParserAndGenerator ,
2425} ;
2526use sugar_path:: SugarPath ;
26- use tokio:: sync:: { OnceCell , RwLock } ;
27+ use tokio:: sync:: RwLock ;
2728
2829use crate :: {
2930 chunk_link:: ChunkLinkContext , dependency:: dyn_import:: DynamicImportDependencyTemplate ,
@@ -39,9 +40,10 @@ pub struct EsmLibraryPlugin {
3940 pub ( crate ) preserve_modules : Option < PathBuf > ,
4041 // module instance will hold this map till compile done, we can't mutate it,
4142 // normal concatenateModule just read the info from it
42- pub ( crate ) concatenated_modules_map_for_codegen : OnceCell < Arc < IdentifierIndexMap < ModuleInfo > > > ,
43+ pub ( crate ) concatenated_modules_map_for_codegen :
44+ AtomicRefCell < Arc < IdentifierIndexMap < ModuleInfo > > > ,
4345 pub ( crate ) concatenated_modules_map : RwLock < Arc < IdentifierIndexMap < ModuleInfo > > > ,
44- pub ( crate ) links : OnceCell < UkeyMap < ChunkUkey , ChunkLinkContext > > ,
46+ pub ( crate ) links : AtomicRefCell < UkeyMap < ChunkUkey , ChunkLinkContext > > ,
4547}
4648
4749impl EsmLibraryPlugin {
@@ -247,10 +249,9 @@ async fn finish_modules(&self, compilation: &mut Compilation) -> Result<()> {
247249
248250 // only used for scope
249251 // we mutably modify data in `self.concatenated_modules_map`
250- self
251- . concatenated_modules_map_for_codegen
252- . set ( Arc :: new ( modules_map. clone ( ) ) )
253- . expect ( "should set concatenated_modules_map_for_codegen" ) ;
252+ let mut map = self . concatenated_modules_map_for_codegen . borrow_mut ( ) ;
253+ * map = Arc :: new ( modules_map. clone ( ) ) ;
254+ drop ( map) ;
254255
255256 * self . concatenated_modules_map . write ( ) . await = Arc :: new ( modules_map) ;
256257 // mark all entry exports as used
@@ -282,10 +283,7 @@ async fn concatenation_scope(
282283 _compilation : & Compilation ,
283284 module : ModuleIdentifier ,
284285) -> Result < Option < ConcatenationScope > > {
285- let modules_map = self
286- . concatenated_modules_map_for_codegen
287- . get ( )
288- . expect ( "should already initialized" ) ;
286+ let modules_map = self . concatenated_modules_map_for_codegen . borrow ( ) ;
289287
290288 let Some ( current_module) = modules_map. get ( & module) else {
291289 return Ok ( None ) ;
0 commit comments